繁体   English   中英

如何找到一个单词(字符串)是否在 python 的输入/列表中出现多次

[英]How can I find if a word (string) occurs more than once in an input/list in python

例如,如果示例输入是:不要问你的国家能为你做什么,问你能为你的国家做什么

我的程序必须返回:“国家”一词出现在第 5 位和第 17 位。

我只需要帮助查找字符串是否出现多次。

到目前为止,这是我的尝试,我是 Python 新手,如果我的问题似乎太容易回答,我很抱歉。

# wordsList=[]
words=input("Enter a sentence without punctuation:\n")
# wordsList.append(words)
# print(wordsList)
for i in words:
    if i in words>1:
        print(words)
# words.split("  ")
# print(words[0])

查找出现次数

可能有几种方法可以做到。 一种简单的方法是将您的句子拆分为一个列表并找出出现的次数。

sentence = "ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY" 
words_in_a_list = sentence.split(" ")
words_in_a_list.count("COUNTRY")

您也可以使用正则表达式,而且也很容易做到。

import re

m = re.findall("COUNTRY", sentence)

查找每次出现的位置

可能你想阅读这篇文章 您也可以使用返回跨度的search 并编写一个循环来查找所有这些。 知道第一个的位置后,开始进一步从这么多字符中搜索字符串。

def count_num_occurences(word, sentence):
    start = 0
    pattern = re.compile(word)
    start_locations = []
    while True:
        match_object = there.search(sentence, start)

        if match_object is not None:
            start_locations.append(match_object.start())
            start = 1 + match_object.start()
        else:
            break
    return start_locations
str = 'ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY'

# split your sentence and make it a set to get the unique parts
# then make it a list so you ca iterate
parts = list(set(str.split(' ')))

# you count to get the nr of occurences of parts in the str
for part in parts:
    print(f'{part} {str.count(part)}x')

结果

COUNTRY 2x
YOU 4x
ASK 2x
YOUR 2x
CAN 2x
NOT 1x
DO 2x
WHAT 2x
FOR 2x

或与职位

import re

str = 'ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR DO YOUR COUNTRY'

# split your sentence and make it a set to get the unique parts
# then make it a list so you ca iterate
parts = list(set(str.split(' ')))

# you count to get the nr of occurences of parts in the str
for part in parts:
    test = re.findall(part, str)
    print(f'{part} {str.count(part)}x')
    for m in re.finditer(part, str):
        print('     found at', m.start())

结果

DO 3x
     found at 30
     found at 58
     found at 65
ASK 2x
     found at 0
     found at 41
COUNTRY 2x
     found at 18
     found at 73
YOUR 2x
     found at 13
     found at 68
WHAT 2x
     found at 8
     found at 45
YOU 4x
     found at 13
     found at 37
     found at 50
     found at 68
NOT 1x
     found at 4
FOR 2x
     found at 33
     found at 61
CAN 2x
     found at 26
     found at 54

如果您只想要出现多次的单词:

words=input("Enter a sentence without punctuation:\n").strip().split()
word_counts = {}

for word in words:
    if word in word_counts:
        word_counts[word] += 1
    else:
        word_counts[word] = 1

for word in word_counts.keys():
    if word_counts[word] > 1:
        print(word)

只需将所有计数存储在字典中,然后遍历字典以打印出现多次的计数。

也很有效,因为它只通过输入一次,然后再通过字典一次

如果您想要单词的实际位置:

words=input("Enter a sentence without punctuation:\n").strip().split()
word_counts = {}

for i in len(words):
    word = words[i]
    if word in word_counts:
        word_counts[word].append(i) // keep a list of indices
    else:
        word_counts[word] = [i]

for word in word_counts.keys():
    if len(word_counts[word]) > 1:
        print("{0} found in positions: {1}".format(word, word_counts[word]))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM