问题:如何在python中构造一组列表项?

list在python中有一个文件名,我想set从所有文件名中构造一个。

filelist=[]
for filename in filelist:
    set(filename)

这似乎不起作用。怎么办

I have a list of filenames in python and I would want to construct a set out of all the filenames.

filelist=[]
for filename in filelist:
    set(filename)

This does not seem to work. How can do this?


回答 0

如果您具有可散列对象的列表(文件名可能是字符串,那么它们应该算在内):

lst = ['foo.py', 'bar.py', 'baz.py', 'qux.py', Ellipsis]

您可以直接构造集合:

s = set(lst)

实际上,set将这种方式与任何可迭代对象一起使用! (鸭子打字不好吗?)


如果要迭代进行:

s = set()
for item in iterable:
    s.add(item)

但是很少需要这样做。我只提到它是因为该set.add方法非常有用。

If you have a list of hashable objects (filenames would probably be strings, so they should count):

lst = ['foo.py', 'bar.py', 'baz.py', 'qux.py', Ellipsis]

you can construct the set directly:

s = set(lst)

In fact, set will work this way with any iterable object! (Isn’t duck typing great?)


If you want to do it iteratively:

s = set()
for item in iterable:
    s.add(item)

But there’s rarely a need to do it this way. I only mention it because the set.add method is quite useful.


回答 1

最直接的解决方案是:

 s = set(filelist)

原始代码中的问题是未将值分配给集合。这是您的代码的固定版本:

 s = set()
 for filename in filelist:
     s.add(filename)
 print(s)

The most direct solution is this:

s = set(filelist)

The issue in your original code is that the values weren’t being assigned to the set. Here’s the fixed-up version of your code:

s = set()
for filename in filelist:
    s.add(filename)
print(s)

回答 2

你可以做

my_set = set(my_list)

或者,对于Python 3,

my_set = {*my_list}

从列表创建一个集合。相反,您也可以

my_list = list(my_set)

或者,对于Python 3,

my_list = [*my_set]

从集合创建列表。

只需注意,将列表转换为集合时,列表中元素的顺序通常会丢失,因为集合本质上是无序的。(不过,CPython中的一个exceptions似乎是如果列表仅包含非负整数,但是我认为这是CPython中集合实现的结果,并且这种行为在不同的Python实现之间会有所不同。)

You can do

my_set = set(my_list)

or, in Python 3,

my_set = {*my_list}

to create a set from a list. Conversely, you can also do

my_list = list(my_set)

or, in Python 3,

my_list = [*my_set]

to create a list from a set.

Just note that the order of the elements in a list is generally lost when converting the list to a set since a set is inherently unordered. (One exception in CPython, though, seems to be if the list consists only of non-negative integers, but I assume this is a consequence of the implementation of sets in CPython and that this behavior can vary between different Python implementations.)


回答 3

这是另一种解决方案:

>>>list1=["C:\\","D:\\","E:\\","C:\\"]
>>>set1=set(list1)
>>>set1
set(['E:\\', 'D:\\', 'C:\\'])

在这段代码中,我使用了set方法,以便将其变成一个集合,然后从列表中删除了所有重复的值

Here is another solution:

>>>list1=["C:\\","D:\\","E:\\","C:\\"]
>>>set1=set(list1)
>>>set1
set(['E:\\', 'D:\\', 'C:\\'])

In this code I have used the set method in order to turn it into a set and then it removed all duplicate values from the list


回答 4

一种像这样的迭代方式构造集合的一般方法:

aset = {e for e in alist}

One general way to construct set in iterative way like this:

aset = {e for e in alist}

回答 5

简单地说:

new_list = set(your_list)

Simply put the line:

new_list = set(your_list)

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