Python 设置为列表

发布于 2024-11-26 20:53:43 字数 178 浏览 0 评论 0原文

如何在 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

可可 2024-12-03 20:53:43

您的代码确实工作(使用 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
南街女流氓 2024-12-03 20:53:43

您不小心将内置集用作变量名,从而掩盖了内置集,这是复制错误的简单方法

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

第一行将集重新绑定到集的实例。第二行尝试调用实例,这当然会失败。

这是一个不太令人困惑的版本,为每个变量使用不同的名称。使用新的解释器

>>> 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

佞臣 2024-12-03 20:53:43

在你写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)
淤浪 2024-12-03 20:53:43

这将起作用:

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

但是,如果您使用“list”或“set”作为变量名称,您将得到:

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

如果您使用“list”作为变量名称,则会发生相同的错误。

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.

兰花执着 2024-12-03 20:53:43
s = set([1,2,3])
print [ x for x in iter(s) ]
s = set([1,2,3])
print [ x for x in iter(s) ]
青衫负雪 2024-12-03 20:53:43

您的代码可以在 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'>
暖伴 2024-12-03 20:53:43

尝试使用 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']) )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文