' NoneType'对象没有属性' count'

时间:2018-03-19 02:02:13

标签: python-3.x list attributeerror

我有错误,但我不知道为什么。这是我的代码:

import itertools
word = 'stop'
#input('Word [ 2-5 letters recommended ] :').lower()
word = list(str(word))
anagrams = ["".join(perm) for perm in itertools.permutations(word)]
file = open('wordlist.txt', 'r')
lines = file.read().split('\n')
for n, i in enumerate(anagrams):
            if i not in lines:
                    anagrams[n] = '/'
for i in enumerate(anagrams):
    if '/' in anagrams:
        while anagrams.count('/') > 0:
            anagrams = anagrams.remove('/')

anagrams = '\n'.join(map(str,anagrams))
print(anagrams.strip(''))

这是错误:

Traceback (most recent call last):
  File "main.py", line 13, in <module>
    while anagrams.count('/') > 0:
AttributeError: 'NoneType' object has no attribute 'count'

我有什么问题吗?

1 个答案:

答案 0 :(得分:0)

刚想出怎么做:

dict

输入:

import itertools
word = input('Word [ 2-5 letters recommended ] :').lower()
word = list(str(word))
anagrams = ["".join(perm) for perm in itertools.permutations(word)]
file = open('wordlist.txt', 'r')
lines = file.read().split('\n')
for n, i in enumerate(anagrams):
            if i not in lines:
                    anagrams[n] = '/'
if str('/') in anagrams:
    anagrams = [x for x in anagrams if x != '/']

anagrams = '\n'.join(map(str,anagrams))
print(anagrams)

输出:

Word [ 2-5 letters recommended ] : stop