Python 将元组附加到列表 - 两种方式有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31175223/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:36:43  来源:igfitidea点击:

Append a tuple to a list - what's the difference between two ways?

pythonlisttypesappendtuples

提问by Skywalker326

I wrote my first "Hello World" 4 months ago. Since then, I have been following a Coursera Python course provided by Rice University. I recently worked on a mini-project involving tuples and lists. There is something strange about adding a tuple into a list for me:

4 个月前我写了我的第一个“Hello World”。从那以后,我一直在学习莱斯大学提供的 Coursera Python 课程。我最近参与了一个涉及元组和列表的小型项目。对我来说,将元组添加到列表中有些奇怪:

a_list = []
a_list.append((1, 2))       # Succeed! Tuple (1, 2) is appended to a_list
a_list.append(tuple(3, 4))  # Error message: ValueError: expecting Array or iterable

It's quite confusing for me. Why specifying the tuple to be appended by using "tuple(...)" instead of simple "(...)" will cause a ValueError?

这对我来说很混乱。为什么通过使用“tuple(...)”而不是简单的“(...)”来指定要附加的元组会导致ValueError?

BTW: I used CodeSkulptorcoding tool used in the course

BTW:我使用了课程中使用的CodeSkulptor编码工具

采纳答案by Bhargav Rao

The tuplefunction takes only one argument which has to be an iterable

tuple函数只接受一个必须是可迭代的参数

tuple([iterable])

Return a tuple whose items are the same and in the same order as iterable‘s items.

tuple([iterable])

返回一个元组,其项目与可迭代的项目相同且顺序相同。

Try making 3,4an iterable by either using [3,4](a list) or (3,4)(a tuple)

尝试3,4使用[3,4](列表)或(3,4)(元组)制作可迭代对象

For example

例如

a_list.append(tuple((3, 4)))

will work

将工作

回答by 101

There should be no difference, but your tuple method is wrong, try:

应该没有区别,但是你的元组方法是错误的,试试:

a_list.append(tuple([3, 4]))

回答by BrenBarn

It has nothing to do with append. tuple(3, 4)all by itself raises that error.

它与append. tuple(3, 4)所有本身都会引发该错误。

The reason is that, as the error message says, tupleexpects an iterable argument. You can make a tuple of the contentsof a singleobject by passing that single object to tuple. You can't make a tuple of two things by passing them as separate arguments.

原因是,正如错误消息所说,tuple需要一个可迭代的参数。您可以通过将单个对象传递给元组来创建单个对象内容的元组。您不能通过将它们作为单独的参数传递来创建两个事物的元组。

Just do (3, 4)to make a tuple, as in your first example. There's no reason not to use that simple syntax for writing a tuple.

就像(3, 4)在你的第一个例子中那样做一个元组。没有理由不使用这种简单的语法来编写元组。

回答by brainless coder

Because tuple(3, 4)is not the correct syntax to create a tuple. The correct syntax is -

因为tuple(3, 4)不是创建元组的正确语法。正确的语法是——

tuple([3, 4])

or

或者

(3, 4)

You can see it from here - https://docs.python.org/2/library/functions.html#tuple

你可以从这里看到它 - https://docs.python.org/2/library/functions.html#tuple

回答by Ashwin Reddy

I believe tuple()takes a list as an argument For example,

我相信tuple()将列表作为参数例如,

tuple([1,2,3]) # returns (1,2,3)

see what happens if you wrap your array with brackets

看看如果你用括号包裹你的数组会发生什么