How to get from this:
col = [('red', '132', '234'), ('green', '236', '434'), ('brown', '542', '457')]
to this:
EDIT (without any quotes)
1 red, 2 green, 3 brown # enumerate and get the first item of each tuple.
I tried this, but it doesn't work:
[zip(((enumerate(col),1),i[0])) for i in col]
Only built-in functions please.
You can use list comprehension.
>>> col = [('red', '132', '234'), ('green', '236', '434'), ('brown', '542', '457')]
>>>
>>> ["{} {}".format(index, first) for index, (first, *_) in enumerate(col, start=1)]
['1 red', '2 green', '3 brown']
First use enumerate
with an appropriate starting value. Next, unpack the resulting tuple to get just the values you need. Finally, use an f-string to create the desired string from the number and the color name.
[f"{i} {color}" for i, (color, _, _) in enumerate(col, 1)]
print([f"{i + 1} {each[0]}" for i, each in enumerate(col)])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.