Python 在不使用枚举的情况下,向for循环中的前x个项添加不同的特定项
我遇到了一个问题,如何在列表的前4项中添加一个特定的符号/文本/单词,这是最好/最有效的方法Python 在不使用枚举的情况下,向for循环中的前x个项添加不同的特定项,python,python-3.x,python-2.7,Python,Python 3.x,Python 2.7,我遇到了一个问题,如何在列表的前4项中添加一个特定的符号/文本/单词,这是最好/最有效的方法 for list_item in list["rows"][:6]: list_name = get_team_name(list_item) list_score = list_item['list_score'] full_list.append('{} team with a score of {:.2f}'.format(list_name, list_score))
for list_item in list["rows"][:6]:
list_name = get_team_name(list_item)
list_score = list_item['list_score']
full_list.append('{} team with a score of {:.2f}'.format(list_name, list_score))
例如,我当前的输出是:
Gold team with a score of 23.22
Yellow team with a score of 18.90
Red team with a score of 15.05
Blue team with a score of 13.29
Green team with a score of 10.93
Teal team with a score of 10.30
我想使输出类似于:
You can create an iterator from the emoji list and then use the next
function to fetch its values sequentially:
emojis = iter(emoji_list)
for list_item in list["rows"][:6]:
list_name = get_team_name(list_item)
list_score = list_item['list_score']
full_list.append('{} {} team with a score of {:.2f}'.format(next(emojis, ''), list_name, list_score))
您可以从表情符号列表中创建迭代器,然后使用next
函数按顺序获取其值:
emojis = emoji_list[::-1]
for list_item in list["rows"][:6]:
list_name = get_team_name(list_item)
list_score = list_item['list_score']
full_list.append('{}{} team with a score of {:.2f}'.format(emojis.pop() + ' ' if emojis else '', list_name, list_score))
或者,如果您不希望没有表情符号的列组的前导空格,您可以使用反向表情符号列表,并在迭代时将其弹出,然后使用三元运算符决定输出什么:
您可以从表情符号列表中创建迭代器,然后使用next
函数按顺序获取其值:
emojis = emoji_list[::-1]
for list_item in list["rows"][:6]:
list_name = get_team_name(list_item)
list_score = list_item['list_score']
full_list.append('{}{} team with a score of {:.2f}'.format(emojis.pop() + ' ' if emojis else '', list_name, list_score))
或者,如果您不希望没有表情符号的列组的前导空格,您可以使用反向表情符号列表,并在迭代时将其弹出,然后使用三元运算符决定输出什么:
你可以使用zip_
从itertools导入zip\u
对于列表项,zip中的表情符号最长(列表[“行”][:6],表情符号列表):
列表名称=获取团队名称(列表项)
列表分数=列表项目['list\u score']
空格=“”如果表情符号为else“”
完整列表。追加({}{}{}{}团队,分数为{:.2f})。格式(表情符号、空格、列表名称、列表分数))
如果您想将默认表情符号添加到列表的其余部分,请在zip\u longest
中使用fillvalue=
,您可以使用zip\u longest
从itertools导入zip\u
对于列表项,zip中的表情符号最长(列表[“行”][:6],表情符号列表):
列表名称=获取团队名称(列表项)
列表分数=列表项目['list\u score']
空格=“”如果表情符号为else“”
完整列表。追加({}{}{}{}团队,分数为{:.2f})。格式(表情符号、空格、列表名称、列表分数))
如果要将默认表情符号添加到列表的其余部分,请使用zip\u longest
中的fillvalue=
Python 2.x和3.x提供了zip()
函数将两个列表映射到单个迭代器中。虽然zip()
截断为最短的列表大小,但是itertools模块提供了“izip_longest()”以填充到最长的长度
下面的代码片段为您提供了确切的输出
对于表情符号,列出izip中最长的项(表情符号列表,列表[“行”][:6]):
列表名称=获取团队名称(列表项)
列表分数=列表项目['list\u score']
完整列表。追加({}{}{}团队,得分为{:.2f})。格式(表情符号或“”,列表名称,列表得分))
注意:emoji或“
用于防止format()
打印无
在Python3.x中,itertools提供了zip_longest()
而不是izip_longest
另外,建议使用list关键字作为变量名。这将使用存储团队详细信息的变量覆盖list()
函数(如list[“rows”]
) Python2.x和3.x提供了zip()
函数将两个列表映射到一个迭代器中。虽然zip()
截断为最短的列表大小,但是itertools模块提供了“izip_longest()”以填充到最长的长度
下面的代码片段为您提供了确切的输出
对于表情符号,列出izip中最长的项(表情符号列表,列表[“行”][:6]):
列表名称=获取团队名称(列表项)
列表分数=列表项目['list\u score']
完整列表。追加({}{}{}团队,得分为{:.2f})。格式(表情符号或“”,列表名称,列表得分))
注意:emoji或“
用于防止format()
打印无
在Python3.x中,itertools提供了zip_longest()
而不是izip_longest
另外,建议使用list关键字作为变量名。这将使用存储团队详细信息的变量覆盖list()
函数(如list[“rows”]
) 另一种方法是使用lstrip()解包:
emoji_list=[”另一种方法是使用lstrip()解包:
emoji_列表=[”您当前的方法有什么问题?看起来很好!@user5173426没有问题,只是必须有一种更快、更有效的方法来做…您当前的方法有什么问题?看起来很好!@user5173426没有问题,只是必须有一种更快、更有效的方法来做…问题是第5项和第6项会有一个问题它们开头的空格。如果我删除两个{}
之间的空格,并在每个表情符号的末尾添加空格,那就行了。问题是第5个和第6个项目的开头会有空格。如果我删除两个{}
之间的空格,并在每个表情符号的末尾添加空格,那就行了。@Ryflex,(我没有声誉可以添加评论。)Answer@blhsing在我看来已经足够接近了。你可以从我的回答中添加emoji=next(emojis.)
和nextspace=…
。@Ryflex,(我没有声誉可以添加评论)。Answer@blhsing在我看来已经足够接近了。你可以添加emoji=next(emojis.))
和下一个空格=…
来自我的答案。