我想用它的数据创建一个元组或每个匹配的列表(例如LB)。
d ={'LB': {'team1': 'leuven', 'length': 27.0, 'team2': 'brussel', 'speed': 120}, 'HL': {'team1': 'hasselt', 'length': 60.0, 'team2': 'leuven', 'speed': 120}, 'LA': {'team1': 'leuven', 'length': 61.0, 'team2': 'antwerpen', 'speed': 120}, 'BL': {'team': 'brussel', 'length': 30.0, 'team2': 'leuven', 'speed': 120}, 'AH': {'team1': 'antwerpen', 'length': 78.0, 'team2': 'hasselt', 'speed': 120}, 'BO': {'team1': 'brussel', 'length': 110.0, 'team2': 'oostende', 'speed': 120}, 'AL': {'team1': 'antwerpen', 'length': 63.0, 'team2': 'leuven', 'speed': 120}, 'OA': {'team1': 'oostende', 'length': 120.0, 'team2': 'antwerpen', 'speed': 120}}
我已经有了这个:
for match, data in d.iteritems():
strings = []
teams1= str(data['team1'])
lengths = str(data['length'])
teams2= str(data['team2'])
speeds = str(data['speed'])
strings.append (teams1)
strings.append(lengths)
strings.append(teams2)
strings.append (speeds)
strings.append (match)
print strings
['leuven', '27.0', 'brussel', '120', 'LB']
['hasselt', '60.0', 'leuven', '120', 'HL']
['leuven', '61.0', 'antwerpen', '120', 'LA']
['brussel', '30.0', 'leuven', '120', 'BL']
['antwerpen', '78.0', 'hasselt', '120', 'AH']
['brussel', '110.0', 'oostende', '120', 'BO']
['antwerpen', '63.0', 'leuven', '120', 'AL']
['oostende', '120.0', 'antwerpen', '120', 'OA']
如何将每个列表放在一个大元组或列表中,如:
(['leuven', '27.0', 'brussel', '120', 'LB'],
['hasselt', '60.0', 'leuven', '120', 'HL'],
['leuven', '61.0', 'antwerpen', '120', 'LA'],
['brussel', '30.0', 'leuven', '120', 'BL'],
['antwerpen', '78.0', 'hasselt', '120', 'AH'],
['brussel', '110.0', 'oostende', '120', 'BO'],
['antwerpen', '63.0', 'leuven', '120', 'AL'],
['oostende', '120.0', 'antwerpen', '120', 'OA'])
或
[['leuven', '27.0', 'brussel', '120', 'LB'],
['hasselt', '60.0', 'leuven', '120', 'HL'],
['leuven', '61.0', 'antwerpen', '120', 'LA'],
['brussel', '30.0', 'leuven', '120', 'BL'],
['antwerpen', '78.0', 'hasselt', '120', 'AH'],
['brussel', '110.0', 'oostende', '120', 'BO'],
['antwerpen', '63.0', 'leuven', '120', 'AL'],
['oostende', '120.0', 'antwerpen', '120', 'OA']]
答案 0 :(得分:2)
[[data[key] for key in 'team1 length team2 speed'.split()] + [match]
for match, data in d.items()]
但是,您需要将team
一个密钥修改为team1
。
答案 1 :(得分:1)
new_list = []
for match, data in d.iteritems():
new_list.append([str(data['team1']), str(data['length']), str(data['team2']), str(data['speed']), match])
print new_list
new_tuple = tuple(new_list)
print new_tuple
答案 2 :(得分:0)
您可以使用列表理解:
f.cktext_area
这是做什么的,对于result = [[data['team1'],data['length'],data['team2'],data['speed'],match] for match, data in d.items()]
中的每个(match,data)
,它会将d.items()
添加到列表[data['team1'],data['length'],data['team2'],data['speed'],match]
。
如果您使用的是Python 2,请将result
替换为d.items()
,但我建议您切换到Python 3。