Python 将元组转换为列表并返回
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16296643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert tuple to list and back
提问by user2133308
I'm currently working on a map editor for a game in pygame, using tile maps. The level is built up out of blocks in the following structure (though much larger):
我目前正在使用平铺地图为 pygame 中的游戏开发地图编辑器。该级别由以下结构的块组成(尽管要大得多):
level1 = (
(1,1,1,1,1,1)
(1,0,0,0,0,1)
(1,0,0,0,0,1)
(1,0,0,0,0,1)
(1,0,0,0,0,1)
(1,1,1,1,1,1))
where "1" is a block that's a wall and "0" is a block that's empty air.
其中“1”是一个墙块,“0”是一个空块。
The following code is basically the one handling the change of block type:
以下代码基本上是处理块类型更改的代码:
clicked = pygame.mouse.get_pressed()
if clicked[0] == 1:
currLevel[((mousey+cameraY)/60)][((mousex+cameraX)/60)] = 1
But since the level is stored in a tuple, I'm unable to change the values of the different blocks. How do I go about changing the different values in the level in an easy manner?
但是由于级别存储在一个元组中,我无法更改不同块的值。如何以简单的方式更改关卡中的不同值?
回答by eumiro
You can have a list of lists. Convert your tuple of tuples to a list of lists using:
你可以有一个列表列表。使用以下方法将元组元组转换为列表列表:
level1 = [list(row) for row in level1]
or
或者
level1 = map(list, level1)
and modify them accordingly.
并相应地修改它们。
But a numpy arrayis cooler.
但是numpy 数组更酷。
回答by pradyunsg
You have a tuple of tuples.
To convert every tuple to a list:
你有一个元组元组。
要将每个元组转换为列表:
[list(i) for i in level] # list of lists
--- OR ---
- - 或者 - -
map(list, level)
And after you are done editing, just convert them back:
完成编辑后,只需将它们转换回来:
tuple(tuple(i) for i in edited) # tuple of tuples
--- OR --- (Thanks @jamylak)
--- 或 --- (感谢@jamylak)
tuple(itertools.imap(tuple, edited))
You can also use a numpy array:
您还可以使用 numpy 数组:
>>> a = numpy.array(level1)
>>> a
array([[1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1]])
For manipulating:
用于操纵:
if clicked[0] == 1:
x = (mousey + cameraY) // 60 # For readability
y = (mousex + cameraX) // 60 # For readability
a[x][y] = 1
回答by tanzil
Both the answers are good, but a little advice:
两个答案都很好,但有一点建议:
Tuples are immutable, which implies that they cannot be changed. So if you need to manipulate data, it is better to store data in a list, it will reduce unnecessary overhead.
元组是不可变的,这意味着它们不能改变。所以如果需要操作数据,最好将数据存放在一个列表中,这样会减少不必要的开销。
In your case extract the data to a list, as shown by eumiro, and after modifying create a similar tuple of similar structure as answer given by Schoolboy.
在您的情况下,将数据提取到列表中,如 eumiro 所示,并在修改后创建一个类似结构的类似元组作为 Schoolboy 给出的答案。
Also as suggested using numpy array is a better option
同样建议使用 numpy array 是更好的选择
回答by Alfe
You could dramatically speed up your stuff if you used just one list instead of a list of lists. This is possible of course only if all your inner lists are of the same size (which is true in your example, so I just assume this).
如果您只使用一个列表而不是列表列表,则可以显着加快您的工作速度。当然,只有当所有内部列表的大小相同时才可能这样做(在您的示例中是这样,所以我只是假设这一点)。
WIDTH = 6
level1 = [ 1,1,1,1,1,1,
1,0,0,0,0,1,
1,0,0,0,0,1,
1,0,0,0,0,1,
1,0,0,0,0,1,
1,1,1,1,1,1 ]
print level1[x + y*WIDTH] # print value at (x,y)
And you could be even faster if you used a bitfield instead of a list:
如果您使用位域而不是列表,则速度可能会更快:
WIDTH = 8 # better align your width to bytes, eases things later
level1 = 0xFC84848484FC # bit field representation of the level
print "1" if level1 & mask(x, y) else "0" # print bit at (x, y)
level1 |= mask(x, y) # set bit at (x, y)
level1 &= ~mask(x, y) # clear bit at (x, y)
with
和
def mask(x, y):
return 1 << (WIDTH-x + y*WIDTH)
But that's working only if your fields just contain 0 or 1 of course. If you need more values, you'd have to combine several bits which would make the issue much more complicated.
但这仅在您的字段只包含 0 或 1 时才有效。如果您需要更多值,则必须组合多个位,这会使问题变得更加复杂。
回答by Khonix
Convert tuple to list:
将元组转换为列表:
>>> t = ('my', 'name', 'is', 'mr', 'tuple')
>>> t
('my', 'name', 'is', 'mr', 'tuple')
>>> list(t)
['my', 'name', 'is', 'mr', 'tuple']
Convert list to tuple:
将列表转换为元组:
>>> l = ['my', 'name', 'is', 'mr', 'list']
>>> l
['my', 'name', 'is', 'mr', 'list']
>>> tuple(l)
('my', 'name', 'is', 'mr', 'list')
回答by TheRedstoneLemon
Why don't you try converting its type from a tuple to a list and vice versa.
为什么不尝试将其类型从元组转换为列表,反之亦然。
level1 = (
(1,1,1,1,1,1)
(1,0,0,0,0,1)
(1,0,0,0,0,1)
(1,0,0,0,0,1)
(1,0,0,0,0,1)
(1,1,1,1,1,1))
print(level1)
level1 = list(level1)
print(level1)
level1 = tuple(level1)
print(level1)
回答by Aravind Krishnakumar
To convert tuples to list
将元组转换为列表
(Commas were missing between the tuples in the given question, it was added to prevent error message)
(给定问题中元组之间缺少逗号,添加它是为了防止出现错误消息)
Method 1:
方法一:
level1 = (
(1,1,1,1,1,1),
(1,0,0,0,0,1),
(1,0,0,0,0,1),
(1,0,0,0,0,1),
(1,0,0,0,0,1),
(1,1,1,1,1,1))
level1 = [list(row) for row in level1]
print(level1)
Method 2:
方法二:
level1 = map(list,level1)
print(list(level1))
Method 1 took --- 0.0019991397857666016 seconds ---
方法 1 耗时 --- 0.0019991397857666016 秒 ---
Method 2 took --- 0.0010001659393310547 seconds ---
方法 2 耗时 --- 0.0010001659393310547 秒 ---
回答by Vijay Rangarajan
List to Tuple and back can be done as below
列表到元组并返回可以如下完成
import ast, sys
input_str = sys.stdin.read()
input_tuple = ast.literal_eval(input_str)
l = list(input_tuple)
l.append('Python')
#print(l)
tuple_2 = tuple(l)
# Make sure to name the final tuple 'tuple_2'
print(tuple_2)

