人1酒2
人1酒1
人2酒1
人2酒2
人2酒3
人2酒4
人2酒5
以上是我在单独的文本文件中的输入。我想在 Python 中创建以下输出字典:
输出 = {'wine1': ['person1', 'person2'], 'wine2':['person1','person2'], 'wine3':['person2'], 'wine4':['person2'] , 'wine5':['person2'] }
如何创建此输出?
我做错了什么:
def read(text):
wine_wishes = dict()
final_assignments = dict()
with open(text) as infile:
for line in infile:
line = line.split()
try:
wine_wishes[line[1]]
except:
wine_wishes[line[1]] = [line[0]]
结果我得到了不正确的输出: {'wine4': ['person2'], 'wine5': ['person2'], 'wine1': ['person1'], 'wine2': ['person1'], '酒3':['person2']}