Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 创建一个程序,搜索一个单词是否重复以及它在列表中出现的次数,_Python_List - Fatal编程技术网

Python 创建一个程序,搜索一个单词是否重复以及它在列表中出现的次数,

Python 创建一个程序,搜索一个单词是否重复以及它在列表中出现的次数,,python,list,Python,List,到目前为止,我已经创建了以下内容: a = str(input("Word 1 = ")) b = str(input("Word 2 = ")) c = str(input("Word 3 = ")) d = str(input("Word 4 = ")) e = str(input("Word 5 = ")) f = str(input("Word 6 = ")) words = [a, b, c, d, e, f] def count_w

到目前为止,我已经创建了以下内容:

    a = str(input("Word 1 = "))
    b = str(input("Word 2 = "))
    c = str(input("Word 3 = "))
    d = str(input("Word 4 = "))
    e = str(input("Word 5 = "))
    f = str(input("Word 6 = "))

words = [a, b, c, d, e, f]

def count_words(words):
    for i in words:
        wordscount = {i:words.count(i)}
        print(wordscount)

count_words(words)
结果是:

{'word': 6}
{'word': 6}
{'word': 6}
{'word': 6}
{'word': 6}
{'word': 6}
我的问题是,如果列表中已经有密钥,我如何使其不打印,例如,不是上面的,而是下面的:

{'word': 6}

您应该对数组进行切片,并检查要打印的单词是否尚未被检查

def count_words(words):
    for index, i in enumerate(words):
        wordscount = {i:words.count(i)}
        if i not in words[0:index]:
            print(wordscount)

另请参见,我使用了
enumerate()
来跟踪循环中的索引。

您应该对数组进行切片,并检查要打印的单词是否尚未被检查

def count_words(words):
    for index, i in enumerate(words):
        wordscount = {i:words.count(i)}
        if i not in words[0:index]:
            print(wordscount)

另请参见我使用
enumerate()
跟踪循环中的索引。

首先,欢迎使用堆栈溢出

解决问题的方法是初始化循环上方的另一个列表,可能称为
words\u
,并将已打印的单词添加到该列表中。如果单词在你提到的单词中,不要打印它。最终代码如下所示:

a = str(input("Word 1 = "))
b = str(input("Word 2 = "))
c = str(input("Word 3 = "))
d = str(input("Word 4 = "))
e = str(input("Word 5 = "))
f = str(input("Word 6 = "))

words = [a, b, c, d, e, f]
words_mentioned = []

def count_words(words):
    for i in words:
        wordscount = {i:words.count(i)}
        if i not in words_mentioned:
            print(wordscount)
            words_mentioned.append(i)

count_words(words)

首先,欢迎来到堆栈溢出

解决问题的方法是初始化循环上方的另一个列表,可能称为
words\u
,并将已打印的单词添加到该列表中。如果单词在你提到的单词中,不要打印它。最终代码如下所示:

a = str(input("Word 1 = "))
b = str(input("Word 2 = "))
c = str(input("Word 3 = "))
d = str(input("Word 4 = "))
e = str(input("Word 5 = "))
f = str(input("Word 6 = "))

words = [a, b, c, d, e, f]
words_mentioned = []

def count_words(words):
    for i in words:
        wordscount = {i:words.count(i)}
        if i not in words_mentioned:
            print(wordscount)
            words_mentioned.append(i)

count_words(words)

为了不超过一次计数单词,您可以在
for i In words:
循环中使用集合:将其替换为
for i In set(单词):

您还可以使用iterTools中的Counter()类:

from itertools import Counter
print( Counter(words) )

为了不超过一次计数单词,您可以在
for i In words:
循环中使用集合:将其替换为
for i In set(单词):

您还可以使用iterTools中的Counter()类:

from itertools import Counter
print( Counter(words) )

非常感谢,我是一名初学者,还在学习,我非常感谢你的回答。如果你能点击我答案旁边的灰色勾号,将我的答案标记为正确,我将不胜感激。这真的很有帮助!非常感谢,我是一名初学者,还在学习,我非常感谢你的回答。如果你能点击我答案旁边的灰色勾号,将我的答案标记为正确,我将不胜感激。这真的很有帮助!