如何在python中将集合转换为列表? [英] How to convert a set to a list in python?
本文介绍了如何在python中将集合转换为列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将一组转换为Python 2.6中的列表.我正在使用以下语法:
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
我该如何解决?
推荐答案
已经是列表
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]
我想知道你是否做了这样的事情:
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
这篇关于如何在python中将集合转换为列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文