How to construct a set out of list items in python?

前端 未结 6 1945
无人共我
无人共我 2020-11-27 12:25

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 file         


        
6条回答
  •  情书的邮戳
    2020-11-27 12:34

    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)
    

提交回复
热议问题