如何在python中构建一个列表项集?

时间:2023-02-02 00:02:10

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

我在python中有一个文件名列表,我想构建一个所有文件名的集合。

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

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

这似乎不起作用。怎么办呢?

4 个解决方案

#1


160  

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

事实上,set将以任何可迭代对象的方式工作! (鸭子打字不是很棒吗?)


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.

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

#2


7  

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)

#3


2  

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

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

#4


2  

You can do

你可以做

my_set = set(my_list)

or, for Python 3,

或者,对于Python 3,

my_set = {*my_list}

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

从列表中创建集合。相反,你也可以这样做

my_list = list(my_set)

or, for Python 3,

或者,对于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.)

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

#1


160  

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

事实上,set将以任何可迭代对象的方式工作! (鸭子打字不是很棒吗?)


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.

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

#2


7  

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)

#3


2  

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

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

#4


2  

You can do

你可以做

my_set = set(my_list)

or, for Python 3,

或者,对于Python 3,

my_set = {*my_list}

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

从列表中创建集合。相反,你也可以这样做

my_list = list(my_set)

or, for Python 3,

或者,对于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.)

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