[英]Replacing string with tuple in list of tuples
I am very sorry to be asking this kind of newbie question but here it goes. 很抱歉问这样的新手问题,但是事情就这样了。 So I basically have this returned as levels:
所以我基本上将此作为级别返回:
levels = [(1, 210, 30, 500, 500, 'white'),(1, 210, 30, 200, 400, 'white'),(1, 210, 30, 600, 300, 'white')]
And I want to iterate through it and replace 'white' with white which is simply (255,255,255). 我想遍历它,并将“ white”替换为简单的白色(255,255,255)。 Python complained about tuples not being changeable, so I would need to create a new list with the tuples instead of white.
Python抱怨元组不可更改,因此我需要用元组而不是白色创建一个新列表。 Is there a quick way to do this?
有快速的方法吗?
just do 做就是了
levelsList = [list(x) for x in level]
After that you can change the 'white' string 之后,您可以更改“白色”字符串
If you want to keep it as a list of tuples, you can try: 如果要将其保留为元组列表,可以尝试:
replacements = {"white": (255, 255, 255)}
levelsList = [(a, b, c, d, e, replacements.get(f, f)) for a, b, c, d, e, f in levels]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.