如何在python中将集合转换为列表?

问题:如何在python中将集合转换为列表?

我正在尝试将一组转换为Python 2.6中的列表。我正在使用以下语法:

first_list = [1,2,3,4]
my_set=set(first_list)
my_list = list(my_set)

但是,我得到以下堆栈跟踪:

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

我怎样才能解决这个问题?

I am trying to convert a set to a list in Python 2.6. I’m using this syntax:

first_list = [1,2,3,4]
my_set=set(first_list)
my_list = list(my_set)

However, I get the following stack trace:

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

How can I fix this?


回答 0

已经是清单了

type(my_set)
>>> <type 'list'>

你想要类似的东西吗

my_set = set([1,2,3,4])
my_list = list(my_set)
print my_list
>> [1, 2, 3, 4]

编辑:您的最后评论的输出

>>> my_list = [1,2,3,4]
>>> my_set = set(my_list)
>>> my_new_list = list(my_set)
>>> print my_new_list
[1, 2, 3, 4]

我想知道您是否做了这样的事情:

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

It is already a list

type(my_set)
>>> <type 'list'>

Do you want something like

my_set = set([1,2,3,4])
my_list = list(my_set)
print my_list
>> [1, 2, 3, 4]

EDIT : Output of your last comment

>>> my_list = [1,2,3,4]
>>> my_set = set(my_list)
>>> my_new_list = list(my_set)
>>> print my_new_list
[1, 2, 3, 4]

I’m wondering if you did something like this :

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

回答 1

代替:

first_list = [1,2,3,4]
my_set=set(first_list)
my_list = list(my_set)

为什么不简化该过程:

my_list = list(set([1,2,3,4])

这将从您的列表中删除重复对象,并将列表返回给您。

Instead of:

first_list = [1,2,3,4]
my_set=set(first_list)
my_list = list(my_set)

Why not shortcut the process:

my_list = list(set([1,2,3,4])

This will remove the dupes from you list and return a list back to you.


回答 2

[编辑]似乎您之前已经重新定义了“列表”,将其用作变量名,如下所示:

list = set([1,2,3,4]) # oops
#...
first_list = [1,2,3,4]
my_set=set(first_list)
my_list = list(my_set)

你会得到

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

[EDITED] It’s seems you earlier have redefined “list”, using it as a variable name, like this:

list = set([1,2,3,4]) # oops
#...
first_list = [1,2,3,4]
my_set=set(first_list)
my_list = list(my_set)

And you’l get

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

回答 3

每当遇到此类问题时,请尝试使用以下方法查找要首先转换的元素的数据类型:

type(my_set)

然后,使用:

  list(my_set) 

将其转换为列表。您现在可以像使用python中的任何普通列表一样使用新建列表。

Whenever you are stuck in such type of problems, try to find the datatype of the element you want to convert first by using :

type(my_set)

Then, Use:

  list(my_set) 

to convert it to a list. You can use the newly built list like any normal list in python now.


回答 4

只需键入:

list(my_set)

这会将格式为{‘1’,’2’}的集合转换为格式为[‘1’,’2’]的列表。

Simply type:

list(my_set)

This will turn a set in the form {‘1′,’2’} into a list in the form [‘1′,’2’].


回答 5

查看您的第一行。您的堆栈跟踪信息显然不是来自您在此处粘贴的代码,因此我不知道您到底做了什么。

>>> my_set=([1,2,3,4])
>>> my_set
[1, 2, 3, 4]
>>> type(my_set)
<type 'list'>
>>> list(my_set)
[1, 2, 3, 4]
>>> type(_)
<type 'list'>

你想要的是set([1, 2, 3, 4])

>>> my_set = set([1, 2, 3, 4])
>>> my_set
set([1, 2, 3, 4])
>>> type(my_set)
<type 'set'>
>>> list(my_set)
[1, 2, 3, 4]
>>> type(_)
<type 'list'>

“不可调用”异常表示您正在执行类似操作set()()-尝试调用set实例。

Review your first line. Your stack trace is clearly not from the code you’ve pasted here, so I don’t know precisely what you’ve done.

>>> my_set=([1,2,3,4])
>>> my_set
[1, 2, 3, 4]
>>> type(my_set)
<type 'list'>
>>> list(my_set)
[1, 2, 3, 4]
>>> type(_)
<type 'list'>

What you wanted was set([1, 2, 3, 4]).

>>> my_set = set([1, 2, 3, 4])
>>> my_set
set([1, 2, 3, 4])
>>> type(my_set)
<type 'set'>
>>> list(my_set)
[1, 2, 3, 4]
>>> type(_)
<type 'list'>

The “not callable” exception means you were doing something like set()() – attempting to call a set instance.


回答 6

我不确定您是否正在使用此([1, 2])语法创建一个集合,而不是一个列表。要创建集合,您应该使用set([1, 2])

这些括号只是包裹住您的表达方式,就像您会写的那样:

if (condition1
    and condition2 == 3):
    print something

并没有真正被忽略,但是对您的表情什么也不做。

注意:(something, something_else)将创建一个元组(但仍然没有列表)。

I’m not sure that you’re creating a set with this ([1, 2]) syntax, rather a list. To create a set, you should use set([1, 2]).

These brackets are just envelopping your expression, as if you would have written:

if (condition1
    and condition2 == 3):
    print something

There’re not really ignored, but do nothing to your expression.

Note: (something, something_else) will create a tuple (but still no list).


回答 7

Python是一种动态类型化的语言,这意味着您不能像在C或C ++中那样定义变量的类型:

type variable = value

要么

type variable(value)

在Python中,如果更改类型或类型的初始化函数(构造函数)来声明类型的变量,则可以使用强制转换:

my_set = set([1,2,3])
type my_set

会给你<type 'set'>答案。

如果有列表,请执行以下操作:

my_list = [1,2,3]
my_set = set(my_list)

Python is a dynamically typed language, which means that you cannot define the type of the variable as you do in C or C++:

type variable = value

or

type variable(value)

In Python, you use coercing if you change types, or the init functions (constructors) of the types to declare a variable of a type:

my_set = set([1,2,3])
type my_set

will give you <type 'set'> for an answer.

If you have a list, do this:

my_list = [1,2,3]
my_set = set(my_list)

回答 8

嗯,我敢打赌,在前面的几行中,您会看到以下内容:

list = set(something)

我错了吗 ?

Hmmm I bet that in some previous lines you have something like:

list = set(something)

Am I wrong ?