[英]Python : Printing lists side by side
我想知道如何將兩個相同大小的列表彼此相鄰打印。
我從嵌套字典中獲得了這些列表。
以下是列表:
[
[
"Chelsea",
"Liverpool",
"ManCity",
"Arsenal",
"Spurs",
"ManU",
"Southampton",
"West Bromwich",
"Everton",
"Bournemouth",
"Stoke",
"Watford",
"West Ham",
"Middlesbrough",
"Foxes",
"Burnley",
"Crystal",
"Sunderland",
"Swans",
"Hull"
],
[
43,
37,
36,
34,
33,
30,
24,
23,
23,
21,
21,
21,
19,
18,
17,
17,
15,
14,
12,
12
]
]
本質上,我希望列表為:
切爾西:44利物浦:37
等等...
這是我的python代碼:
@app.route('/League Standing', methods=['GET','POST'])
def show_league():
text = request.form['league']
connection = httplib.HTTPConnection('api.football-data.org')
headers = {'X-Auth-Token': 'key', 'X-Response-Control': 'minified'}
connection.request('GET', '/v1/competitions/'+text+'/leagueTable', None, headers)
response = json.loads(connection.getresponse().read().decode())
teamnames = [r['team'] for r in response['standing']]
points = [r['points'] for r in response['standing']]
for teamname, point in zip(teamnames, points):
print('{}: {}'.format(teamname, point))
我嘗試使用%格式化,但是什么也沒得到!
您可以在此處使用zip
作為:
# v unpack content of list
for a, b in zip(*my_list):
print '{}: {}'.format(a, b)
其中my_list
是問題中提到的列表。 為了將這些條目的形式映射dict
,你必須類型轉換的zip
ED值dict
為:
dict(zip(*my_list))
它將返回:
{'ManU': 30, 'Liverpool': 37, 'Burnley': 17, 'West Bromwich': 23, 'Middlesbrough': 18, 'Chelsea': 43, 'West Ham': 19, 'Hull': 12, 'Bournemouth': 21, 'Stoke': 21, 'Foxes': 17, 'Arsenal': 34, 'Southampton': 24, 'Watford': 21, 'Crystal': 15, 'Sunderland': 14, 'Everton': 23, 'Swans': 12, 'ManCity': 36, 'Spurs': 33}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.