问题:Python设置为列表

如何在Python中将集合转换为列表?使用

a = set(["Blah", "Hello"])
a = list(a)

不起作用。它给了我:

TypeError: 'set' object is not callable

How can I convert a set to a list in Python? Using

a = set(["Blah", "Hello"])
a = list(a)

doesn’t work. It gives me:

TypeError: 'set' object is not callable

回答 0

您的代码可以正常工作(在cpython 2.4、2.5、2.6、2.7、3.1和3.2上进行了测试):

>>> a = set(["Blah", "Hello"])
>>> a = list(a) # You probably wrote a = list(a()) here or list = set() above
>>> a
['Blah', 'Hello']

检查您是否没有list意外覆盖:

>>> assert list == __builtins__.list

Your code does work (tested with cpython 2.4, 2.5, 2.6, 2.7, 3.1 and 3.2):

>>> a = set(["Blah", "Hello"])
>>> a = list(a) # You probably wrote a = list(a()) here or list = set() above
>>> a
['Blah', 'Hello']

Check that you didn’t overwrite list by accident:

>>> assert list == __builtins__.list

回答 1

您无意间使用了内置集作为变量名,从而掩盖了它,这是一种复制错误的简单方法

>>> set=set()
>>> set=set()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'set' object is not callable

第一行将set重新绑定到set的实例。第二行试图调用该实例,该实例当然会失败。

这是一个不太混乱的版本,每个变量使用不同的名称。使用新鲜的口译员

>>> a=set()
>>> b=a()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'set' object is not callable

希望很明显,调用a是一个错误

You’ve shadowed the builtin set by accidentally using it as a variable name, here is a simple way to replicate your error

>>> set=set()
>>> set=set()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'set' object is not callable

The first line rebinds set to an instance of set. The second line is trying to call the instance which of course fails.

Here is a less confusing version using different names for each variable. Using a fresh interpreter

>>> a=set()
>>> b=a()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'set' object is not callable

Hopefully it is obvious that calling a is an error


回答 2

在编写之前,set(XXXXX) 您已经使用“ set”作为变量,例如

set = 90 #you have used "set" as an object


a = set(["Blah", "Hello"])
a = list(a)

before you write set(XXXXX) you have used “set” as a variable e.g.

set = 90 #you have used "set" as an object
…
…
a = set(["Blah", "Hello"])
a = list(a)

回答 3

这将起作用:

>>> t = [1,1,2,2,3,3,4,5]
>>> print list(set(t))
[1,2,3,4,5]

但是,如果将“列表”或“集合”用作变量名,则会得到:

TypeError: 'set' object is not callable

例如:

>>> set = [1,1,2,2,3,3,4,5]
>>> print list(set(set))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable

如果将“列表”用作变量名,则会发生相同的错误。

This will work:

>>> t = [1,1,2,2,3,3,4,5]
>>> print list(set(t))
[1,2,3,4,5]

However, if you have used “list” or “set” as a variable name you will get the:

TypeError: 'set' object is not callable

eg:

>>> set = [1,1,2,2,3,3,4,5]
>>> print list(set(set))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable

Same error will occur if you have used “list” as a variable name.


回答 4

s = set([1,2,3])
print [ x for x in iter(s) ]
s = set([1,2,3])
print [ x for x in iter(s) ]

回答 5

您的代码在Win7 x64上与Python 3.2.1兼容

a = set(["Blah", "Hello"])
a = list(a)
type(a)
<class 'list'>

Your code works with Python 3.2.1 on Win7 x64

a = set(["Blah", "Hello"])
a = list(a)
type(a)
<class 'list'>

回答 6

尝试结合使用map和lambda函数:

aList = map( lambda x: x, set ([1, 2, 6, 9, 0]) )

如果您在字符串中有一组数字并将其转换为整数列表,则这是一种非常方便的方法:

aList = map( lambda x: int(x), set (['1', '2', '3', '7', '12']) )

Try using combination of map and lambda functions:

aList = map( lambda x: x, set ([1, 2, 6, 9, 0]) )

It is very convenient approach if you have a set of numbers in string and you want to convert it to list of integers:

aList = map( lambda x: int(x), set (['1', '2', '3', '7', '12']) )

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。