Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.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_List_Tuples - Fatal编程技术网

Python 向列表追加元组

Python 向列表追加元组,python,list,tuples,Python,List,Tuples,将重新组合的元组添加到列表中的正确语法是什么 例如,如果我有两个列表: >>> a = [(1,2,3),(4,5,6)] >>> b = [(0,0)] 那么,我希望以下措施能够奏效: >>> b.append((a[0][0],a[0,2])) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: l

将重新组合的元组添加到列表中的正确语法是什么

例如,如果我有两个列表:

>>> a = [(1,2,3),(4,5,6)]
>>> b = [(0,0)]
那么,我希望以下措施能够奏效:

>>> b.append((a[0][0],a[0,2]))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list indices must be integers, not tuple

您必须尝试这样做:

(a[0][0],a[0,2])
           ^^^
这就像做:

(a[0][0],a[(0,2)])
正如错误所说:列表索引必须是整数,而不是元组

如果我没弄错的话,我想你想做:

b.append((a[0][0],a[0][2]))

[0,2]是您的问题

它不是在抱怨追加,而是告诉您[0,2]不能用作列表a的索引。

您的问题是:

b.append((a[0][0],a[0,2]))
                     ^

您试图使用不存在的元组索引
[0,2]
,意思是
[0][2]

索引必须是整数。这只是一个打字错误,你有
a[0,2]
而不是
a[0][2]
[0,2]
试图通过元组进行索引。

您能否澄清您的编辑,说明打字错误是根本问题?目前还不清楚您的编辑是否澄清了问题或确定了错误的根本原因。
b.append((a[0][0],a[0,2]))
                     ^