假設輸入文件名爲predators.txt
並具有下列內容:
lion eats zebra
zombie eats snow lion
lion eats gazelle
lion eats wildebeest
zombie eats brains
下面的腳本生成的輸出:
lion eats zebra, gazelle, and wildebeest
zombie eats snow lion, and brains
如果這是你所需要的輸出,你可以欣然提交以下腳本作爲您的功課:
#! /usr/bin/python3
print((lambda a,_: '\n'.join
('{} eats {}'.format(b, ', '
.join(c)[::-1].replace(',',
'dna ,',1)[::-1])for b,c in
a.items()))(*(lambda a,b:(a,
[a[c].append(d)if c in a else
a.update({c:[d]})for c,_,d in
b]))({},[a.strip().split(
maxsplit=2)for a in open
('predators.txt')])))
具體與您的要求是什麼你想知道,邏輯代碼等? –
我們不是家庭作業服務。發佈你先試過的代碼。 – IanAuld