Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python—如何在元组中存储元素_Python_Store_Tuples - Fatal编程技术网

python—如何在元组中存储元素

python—如何在元组中存储元素,python,store,tuples,Python,Store,Tuples,如何在元组中存储元素 我这样做: for i in range (0, capacity): for elements in self.table[i]: # STORE THE ALL THE ELEMENTS IN THE TUPLE 它和t=tupleself.table[i]一样简单,我想这就是你想要的 x = [] for i in range (0, capacity): for elements in self.table[i]: x.appe

如何在元组中存储元素

我这样做:

for i in range (0, capacity):
    for elements in self.table[i]:
        # STORE THE ALL THE ELEMENTS IN THE TUPLE

它和t=tupleself.table[i]一样简单,我想这就是你想要的

x = []
for i in range (0, capacity):
  for elements in self.table[i]:
    x.append(elements)
t = tuple(x)

我想这就是你想要的

x = []
for i in range (0, capacity):
  for elements in self.table[i]:
    x.append(elements)
t = tuple(x)

元组是不可变的。您可以创建一个元组。但不能将元素存储到已创建的元组中。创建或将元素列表转换为元组。把它分成两组

在你的情况下是这样的

t = tuple(self.table[:capacity])

元组是不可变的。您可以创建一个元组。但不能将元素存储到已创建的元组中。创建或将元素列表转换为元组。把它分成两组

在你的情况下是这样的

t = tuple(self.table[:capacity])

既然你们还没告诉我们,我们只能猜测这张桌子是什么样子 例如,如果它是一个列表列表,您可以这样做来获得一个元组的元组

>>> table =[[1,2,3],[4,5,6],[7,8,9]]
>>> tuple(map(tuple, table))
((1, 2, 3), (4, 5, 6), (7, 8, 9))

>>> capacity=2
>>> tuple(map(tuple, table[:capacity]))
((1, 2, 3), (4, 5, 6))

既然你们还没告诉我们,我们只能猜测这张桌子是什么样子 例如,如果它是一个列表列表,您可以这样做来获得一个元组的元组

>>> table =[[1,2,3],[4,5,6],[7,8,9]]
>>> tuple(map(tuple, table))
((1, 2, 3), (4, 5, 6), (7, 8, 9))

>>> capacity=2
>>> tuple(map(tuple, table[:capacity]))
((1, 2, 3), (4, 5, 6))