将列表转换为四元组序列

时间:2016-01-16 20:08:55

标签: python list tuples

基于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的方法吗?

1 个答案:

答案 0 :(得分:2)

[(y.upper(), y, "", x) for x, y in enumerate(["Red", "Green", "Blue"])]