我可以把一个tuple放到python中的数组中吗?

时间:2022-05-06 00:31:18

I am wondering how to put a tuple into an array? or is it better to use arrays in array for the design of the program rather than a tuple in an array? please advice me. thank you

我想知道如何将一个元组放入数组中?还是在程序的设计中使用数组而不是数组中的元组更好?请建议我。谢谢你!

3 个解决方案

#1


15  

One thing to keep in mind is that a tuple is immutable. This means that once it's created, you can't modify it in-place. A list, on the other hand, is mutable -- meaning you can add elements, remove elements, and change elements in-place. A list has extra overhead, so only use a list if you need to modify the values.

需要记住的一点是元组是不可变的。这意味着一旦它被创建,您就不能在适当的地方修改它。另一方面,列表是可变的——这意味着您可以添加元素、删除元素和替换元素。列表有额外的开销,所以只有在需要修改值时才使用列表。

You can create a list of tuples:

您可以创建一个元组列表:

>>> list_of_tuples = [(1,2),(3,4)]
>>> list_of_tuples
[(1, 2), (3, 4)]

or a list of lists:

或一份清单:

>>> list_of_lists = [[1, 2], [3, 4]]
>>> list_of_lists
[[1, 2], [3, 4]]

The difference is that you can modify the elements in the list of lists:

不同的是你可以修改列表中的元素:

>>> list_of_lists[0][0] = 7
>>> list_of_lists
[[7, 2], [3, 4]]

but not with the list of tuples:

但是没有元组列表:

>>> list_of_tuples[0][0] = 7
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

To iterate over a list of tuples:

遍历元组列表:

>>> for (x,y) in list_of_tuples:
...    print x,y
... 
1 2
3 4

#2


6  

if you are talking about list, you can put anything into it, even different types:

如果你说的是列表,你可以在里面放任何东西,甚至是不同的类型:

l=[10,(10,11,12),20,"test"]

l[0] = (1,2,3)
l.append((4,5))
l.extend((21,22)) #this one adds each element from the tuple

if you mean array, no python arrays don't support tuples.

如果您是指数组,则没有python数组不支持元组。

#3


1  

a = [ ('b', i , "ff" ) for i in range(1,5)]  

#1


15  

One thing to keep in mind is that a tuple is immutable. This means that once it's created, you can't modify it in-place. A list, on the other hand, is mutable -- meaning you can add elements, remove elements, and change elements in-place. A list has extra overhead, so only use a list if you need to modify the values.

需要记住的一点是元组是不可变的。这意味着一旦它被创建,您就不能在适当的地方修改它。另一方面,列表是可变的——这意味着您可以添加元素、删除元素和替换元素。列表有额外的开销,所以只有在需要修改值时才使用列表。

You can create a list of tuples:

您可以创建一个元组列表:

>>> list_of_tuples = [(1,2),(3,4)]
>>> list_of_tuples
[(1, 2), (3, 4)]

or a list of lists:

或一份清单:

>>> list_of_lists = [[1, 2], [3, 4]]
>>> list_of_lists
[[1, 2], [3, 4]]

The difference is that you can modify the elements in the list of lists:

不同的是你可以修改列表中的元素:

>>> list_of_lists[0][0] = 7
>>> list_of_lists
[[7, 2], [3, 4]]

but not with the list of tuples:

但是没有元组列表:

>>> list_of_tuples[0][0] = 7
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

To iterate over a list of tuples:

遍历元组列表:

>>> for (x,y) in list_of_tuples:
...    print x,y
... 
1 2
3 4

#2


6  

if you are talking about list, you can put anything into it, even different types:

如果你说的是列表,你可以在里面放任何东西,甚至是不同的类型:

l=[10,(10,11,12),20,"test"]

l[0] = (1,2,3)
l.append((4,5))
l.extend((21,22)) #this one adds each element from the tuple

if you mean array, no python arrays don't support tuples.

如果您是指数组,则没有python数组不支持元组。

#3


1  

a = [ ('b', i , "ff" ) for i in range(1,5)]