Python 将列表转换为四元组序列

Python 将列表转换为四元组序列,python,list,tuples,Python,List,Tuples,基于,我对转换如下列表感兴趣: ["Red", "Green", "Blue"] 转换为字符串三元组序列,并添加计数器(整数): 通常我会编写一个方法,如: def list_to_items(lst): items = [] for i,j in enumerate(lst): items.append((j.upper(), j, "", i)) return items 是否有更好的/更具python风格的方法来实现这一点?只是将list更改为t

基于,我对转换如下列表感兴趣:

["Red", "Green", "Blue"]
转换为字符串三元组序列,并添加计数器(整数):

通常我会编写一个方法,如:

def list_to_items(lst):
    items = []
    for i,j in enumerate(lst):
        items.append((j.upper(), j, "", i))
    return items

是否有更好的/更具python风格的方法来实现这一点?

只是将
list
更改为
tuple
,答案是一样的。答案是一样的,因为答案并不是试图为您解决问题,而是试图解释这种机制是如何工作的。基于这些答案,你应该能够做到这一点。如果唯一的问题是计算,你可以单独问。我喜欢这个,只是我可能会使用
y.title()
作为第二个元素,以防用户提供输入。
[(y.upper(), y, "", x) for x, y in enumerate(["Red", "Green", "Blue"])]
[(y.upper(), y, "", x) for x, y in enumerate(["Red", "Green", "Blue"])]