如何将不同的列表放在列表或元组中?

时间:2021-09-13 07:33:55

I want to create a tuple or a list of each match (for example LB) with its data.

我想用它的数据创建一个元组或每个匹配的列表(例如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}}

I already have this:

我已经有了这个:

for match, data in d.iteritems():

匹配,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']

How can I put each list in one big tuple or list like:

如何将每个列表放在一个大元组或列表中:

(['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'])

or

[['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']]

3 个解决方案

#1


[[data[key] for key in 'team1 length team2 speed'.split()] + [match]
 for match, data in d.items()]

You need to fix that one team key to team1, though.

但是,您需要将一个团队密钥修复为team1。

#2


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

#3


You can use list comprehension:

您可以使用列表理解:

result = [[data['team1'],data['length'],data['team2'],data['speed'],match] for match, data in d.items()]

What this does, is that for each (match,data) in d.items(), it will add [data['team1'],data['length'],data['team2'],data['speed'],match] to the list result.

这样做,是对于d.items()中的每个(匹配,数据),它将添加[data ['team1'],data ['length'],data ['team2'],data ['speed' ],匹配]到列表结果。

Replace d.items() with d.iteritems() if you are using Python 2, but I recommend switching to Python 3.

如果您使用的是Python 2,请将d.items()替换为d.iteitems(),但我建议您切换到Python 3。

#1


[[data[key] for key in 'team1 length team2 speed'.split()] + [match]
 for match, data in d.items()]

You need to fix that one team key to team1, though.

但是,您需要将一个团队密钥修复为team1。

#2


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

#3


You can use list comprehension:

您可以使用列表理解:

result = [[data['team1'],data['length'],data['team2'],data['speed'],match] for match, data in d.items()]

What this does, is that for each (match,data) in d.items(), it will add [data['team1'],data['length'],data['team2'],data['speed'],match] to the list result.

这样做,是对于d.items()中的每个(匹配,数据),它将添加[data ['team1'],data ['length'],data ['team2'],data ['speed' ],匹配]到列表结果。

Replace d.items() with d.iteritems() if you are using Python 2, but I recommend switching to Python 3.

如果您使用的是Python 2,请将d.items()替换为d.iteitems(),但我建议您切换到Python 3。