基于this question,我有兴趣转换如下列表:
["Red", "Green", "Blue"]
进入字符串三元组的元组序列和添加计数器(整数):
[("RED", "Red", "", 0), ("GREEN", "Green", "", 1), ("BLUE", "Blue", "", 2)]
通常我会写一个像:
这样的方法def list_to_items(lst):
items = []
for i,j in enumerate(lst):
items.append((j.upper(), j, "", i))
return items
这样做有更好/更pythonic的方法吗?
答案 0 :(得分:2)
[(y.upper(), y, "", x) for x, y in enumerate(["Red", "Green", "Blue"])]