python set转为list_如何将一个集合转换成python中的列表?(How to convert a set to a list in python?)...

如何将一个集合转换成python中的列表?(How to convert a set to a list in 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 "", line 1, in

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 "", line 1, in

TypeError: 'set' object is not callable

How can I fix this?

原文:https://stackoverflow.com/questions/6593979

更新时间:2019-07-10 19:08

最满意答案

它已经是一个列表

type(my_set)

>>>

你想要这样的东西吗

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 "", line 1, in

TypeError: 'set' object is not callable

It is already a list

type(my_set)

>>>

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 "", line 1, in

TypeError: 'set' object is not callable

2011-07-06

相关问答

可以使用字符串的join方法,可以把列表的各个元素连接起来,字符串就是连个列表各元素的连接符。 >>> l = ['I', 'want', 'a', 'apple', 'pi'] >>> ' '.join(l) 'I want a apple pi'' '代表使用空格连接字符串的各个元素。 我们甚至可以使用空字符串来连接列表的各个元素,这样连接后,列表的各个元素间将不会有间隔。 >>> l2 = ['1','2','3','4','5'] >>> ''.join(l2) '12345'

可以使用字符串的join方法,可以把列表的各个元素连接起来,字符串就是连个列表各元素的连接符。>>> l = ['I', 'want', 'a', 'apple', 'pi'] >>> ' '.join(l) 'I want a apple pi'' '代表使用空格连接字符串的各个元素。我们甚至可以使用空字符串来连接列表的各个元素,这样连接后,列表的各个元素间将不会有间隔。>>> l2 = ['1','2','3','4','5'] >>> ''.join(l2) '12345'

使用json模块生成JSON输出: import json

with open(outputfilename, 'wb') as outfile:

json.dump(row, outfile)

这将JSON结果直接写入文件(如果文件已经存在,则替换之前的任何内容)。 如果你需要Python本身的JSON结果字符串,请使用json.dumps() (为'string'添加s ): json_string = json.dumps(row)

L只是一个长整型值的Python语法; js

...

由于列表是可变的,它们不能被散列 。 最好的选择是将它们转换为一个元组并形成一个集合,就像这样 >>> mat = [[1,2,3],[4,5,6],[1,2,3],[7,8,9],[4,5,6]]

>>> set(tuple(row) for row in mat)

set([(4, 5, 6), (7, 8, 9), (1, 2, 3)])

我们遍历mat ,每次一个列表,将它转换为一个元组(这是不可变的,因此set s很酷),并且生成器被发送到set函数。 如果您希望将结果作为列表的列表,

...

它已经是一个列表 type(my_set)

>>>

你想要这样的东西吗 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,

...

b = dict(zip(a[0::2], a[1::2]))

如果a很大,你可能会想要做如下的事情,这样做不会像上面那样做任何临时列表。 from itertools import izip

i = iter(a)

b = dict(izip(i, i))

在Python 3中,您也可以使用字典理解,但是讽刺的是,我认为最简单的方法是使用range()和len() ,这通常是一个代码气味。 b = {a[i]: a[i+1] for i in range(0, len(a), 2)}

所以

...

使用tolist() : import numpy as np

>>> np.array([[1,2,3],[4,5,6]]).tolist()

[[1, 2, 3], [4, 5, 6]]

请注意,这将将值从任何可能的numpy类型(例如np.int32或np.float32)转换为“最近兼容的Python类型”(在列表中)。 如果要保留numpy数据类型,可以调用数组的list(),而最后会出现一个numpy标量列表。 (感谢Mr_and_Mrs_D在评论中指出) Use tolist():

...

只需对列表推导进行一些小调整,然后将x包装在括号中,将每个元素放入列表中。 >>> p = [1, 2, 3, 4]

>>> v = [[x] for x in p]

>>> v

[[1], [2], [3], [4]]

Just make a small tweak to your list comprehension, and wrap the x in brackets to put each element in a list. >>> p = [1, 2, 3, 4]

>>> v =

...

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值