从set转换时的空列表

时间:2013-11-05 04:53:23

标签: python

我正在尝试将我已定义的集合转换为列表,以便我可以将其用于索引。

seen = set()
for line in p:
   for word in line.split():
       if word not in seen and not word.isdigit():
           seen.add(word)          
been = list(seen)

该套装好像包含物品。但是,当我在变量资源管理器中监视其值时(以及稍后调用索引函数时),列表始终为空。

我做错了什么?

编辑:这是整个代码。我试图在'o'中的'p'中找到单词的位置,并在一行中绘制出现次数。这是一个庞大的单词列表,因此手动输入任何内容都是不可能的。

p = open("p.txt", 'r')
o = open("o.txt", 'r')
t = open("t.txt", 'w')
lines = p.readlines()
vlines = o.readlines()
seen = set()
for line in p:
   for word in line.split():
       if word not in seen and not word.isdigit():
           seen.add(word)          
been = list(seen)
for i in lines:
        thisline = i.split();
        thisline[:] = [word for word in thisline if not word.isdigit()]
        count = len(thisline)
        j = []
        j.append(count)
        for sword in thisline:
             num = thisline.count(sword)
             #index=0
             #for m in vlines:
                 #if word is not m:
                 #index+=1
            ix = been.index(sword)
            j.append(' ' + str(ix) + ':' + str(num))
        j.append('\n')
for item in j:
  t.write("%s" % item)

输出格式应为'(行中的项目总数)(索引):(出现次数)'。 我觉得我很接近,但这部分让我烦恼。

2 个答案:

答案 0 :(得分:2)

您的代码运行正常。

>>> p = '''
the 123 dogs
chased 567 cats
through 89 streets'''.splitlines()
>>> seen = set()
>>> for line in p:
       for word in line.split():
           if word not in seen and not word.isdigit():
               seen.add(word)


>>> been = list(seen)
>>> 
>>> seen
set(['streets', 'chased', 'cats', 'through', 'the', 'dogs'])
>>> been
['streets', 'chased', 'cats', 'through', 'the', 'dogs']

答案 1 :(得分:0)

除非您想要逐行阅读,否则您可以简单地替换它:

seen = set()
for line in p:
   for word in line.split():
       if word not in seen and not word.isdigit():
           seen.add(word)          
been = list(seen)

使用:

been = list(set([w for w in open('p.txt', 'r').read().split() if not w.isdigit()]))