Python 你如何创建一个空的元组列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30408414/
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
How do you create an empty list of tuples?
提问by cdamayor
I'm trying to create a new empty list that will contain tuples when calling extend. Here's where I declare the list:
我正在尝试创建一个新的空列表,该列表在调用扩展时将包含元组。这是我声明列表的地方:
ticketData = list()
And I loop through another list to add tuples to this list:
我遍历另一个列表以将元组添加到此列表中:
data = (0, requestor, subject, thetime)
ticketData.extend(data)
When I output the result it shows this:
当我输出结果时,它显示:
[0, 'Name', 'Test Subject', '03:31:12']
I need it to be a list of tuples, not just a list so that I can use sqlite's executemany function.
我需要它是一个元组列表,而不仅仅是一个列表,以便我可以使用 sqlite 的 executemany 函数。
It seems straight forward, but I haven't been able to find an answer on here. Thanks!
看起来很简单,但我无法在这里找到答案。谢谢!
采纳答案by Eric Renouf
Just append it to the list, so that the tuple will be added instead of having the elements of the tuple extend the list.
只需将它附加到列表中,这样元组将被添加而不是让元组的元素扩展列表。
ticketData.append(data)
will add data
to the list ticketData
将添加data
到列表中ticketData
回答by El Sholz
Although this question has been answered already, I thought it could be useful to know about how to construct an iterable that should contain an empty iterable. In Python, constructors like list(), set(), tuple() etc. take an iterable as input. Hence they iterate through the iterable and somthing like list(tuple()) does not return a list with an empty tuple. Instead the list constructor iterates through the tuples, and since it has no items in it it results in an empty list.
虽然已经回答了这个问题,但我认为了解如何构造一个应该包含空迭代的迭代可能会很有用。在 Python 中,像 list()、set()、tuple() 等构造函数接受一个可迭代对象作为输入。因此,它们遍历可迭代对象,而 list(tuple()) 之类的东西不会返回带有空元组的列表。相反,列表构造函数遍历元组,并且由于其中没有项目,因此会产生一个空列表。
To construct a list that contains an empty tuple do the following:
要构造一个包含空元组的列表,请执行以下操作:
Instead of my_list = list(tuple()) // returns [] or list()
代替 my_list = list(tuple()) // returns [] or list()
Do: my_list = [tuple()]
做: my_list = [tuple()]
Or: my_list = list([tuple()]) //constructor iterates through the list containing the empty tuple
或者: my_list = list([tuple()]) //constructor iterates through the list containing the empty tuple
Analogously, if you want to make a set containing an empty tuple do the following:
类似地,如果要创建一个包含空元组的集合,请执行以下操作:
Instead of my_set = set(tuple()) // returns set()
代替 my_set = set(tuple()) // returns set()
Do: my_set = {tuple()}
做: my_set = {tuple()}
Or: my_set = set([tuple()]) // constructor iterates through the list, containing the empty tuple
或者: my_set = set([tuple()]) // constructor iterates through the list, containing the empty tuple
This also applies to other iterables. I am writing this, because I myself have had problems using these constructors to return e.g. a list containing another empty iterable.
这也适用于其他可迭代对象。我写这篇文章是因为我自己在使用这些构造函数返回例如一个包含另一个空迭代的列表时遇到了问题。