Python:按sid打印列表

2024-06-16 13:24:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道怎样把两张同样大小的单子并排打印。在

我从这些字典中得到了嵌套的列表

以下是列表:

[
  [
    "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代码:

^{pr2}$

我尝试过用%格式化,但没有得到任何结果!在


Tags: 列表字典单子arsenalwestchelsealiverpoolmancity
1条回答
网友
1楼 · 发布于 2024-06-16 13:24:05

您可以在这里使用^{}作为:

#               v  unpack content of list
for a, b in zip(*my_list):
    print '{}: {}'.format(a, b)

其中my_list是问题中提到的列表。为了以dict的形式映射这些条目,必须将ziped值转换为dict为:

^{pr2}$

将返回:

{'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}

相关问题 更多 >