簡體   English   中英

在元組列表中用元組替換字符串

[英]Replacing string with tuple in list of tuples

很抱歉問這樣的新手問題,但是事情就這樣了。 所以我基本上將此作為級別返回:

levels = [(1, 210, 30, 500, 500, 'white'),(1, 210, 30, 200, 400, 'white'),(1, 210, 30, 600, 300, 'white')]

我想遍歷它,並將“ white”替換為簡單的白色(255,255,255)。 Python抱怨元組不可更改,因此我需要用元組而不是白色創建一個新列表。 有快速的方法嗎?

做就是了

levelsList = [list(x) for x in level]

之后,您可以更改“白色”字符串

如果要將其保留為元組列表,可以嘗試:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM