找到你要的答案

Q:How can I put different lists in a list or tuple?

Q:如何在列表或元组中放置不同的列表?

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

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():

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

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

我已经有这个:

对于比赛,在iteritems()数据D.:

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']]
answer1: 回答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.

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

你需要解决一个团队关键的表演好,虽然。

answer2: 回答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
new_list = []   
对于比赛,在iteritems()数据D.:
    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
answer3: 回答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.

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

你可以使用列表理解:

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

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

items()取代D. D. iteritems()如果你使用Python 2,但我建议切换到Python 3。

python  dictionary  tuples