【问题标题】:Retrieving multiple iterating positions of strings in list [duplicate]检索列表中字符串的多个迭代位置[重复]
【发布时间】:2016-07-30 12:21:53
【问题描述】:

我的程序的目的是找到一个句子中单词迭代的位置,故障子程序如下。

def analyse(splitString):
wordToSearch = input("What word are you searching for instances of? ").lower()
for word in splitString:
    positionLibrary = ""
    positionInSplitString = 0
    instances = 0
    if word == wordToSearch:
        position = splitString.index(word)
        positionLibrary += str(position)
        print (position, word)
        instances += 1
    positionInSplitString += 1
return (positionLibrary, instances, wordToSearch)

让“splitString”成为句子“运动的改变永远与所施加的动力成正比,并且在施加该力的正确直线上产生”的列表形式。现在,假设我在 splitString 中搜索“impressed”,它返回 What word are you searching for instances of? impressed 11 impressed 11 impressed ['the', 'alteration', 'of', 'motion', 'is', 'ever', 'proportional', 'to', 'the', 'motive', 'force', 'impressed', 'and', 'is', 'made', 'in', 'the', 'right', 'line', 'on', 'which', 'that', 'force', 'is', 'impressed'] wordToSearch impressed instances 1 positionLibrary 11 这告诉我该程序以某种方式知道有 2 个“印象深刻”的实例,但并未将这些实例的数量计入“实例”变量(这似乎不可靠且不起作用。) positionLibrary,用于存储(作为字符串)找到的实例位置的记录不起作用。我相信这是因为程序只返回“印象”的第一个实例的位置,如 11 impressed 11 impressed 所示。

现在,我如何让程序在单词的第一个实例之后实际返回任何位置并使“实例”变量起作用?我搜索了很远,但没有找到解决方案。

【问题讨论】:

    标签: python list subroutine


    【解决方案1】:

    您不需要使用index() 方法,因为您已经在循环通过splitString。您只需要一个索引或计数器来跟踪您正在进行的迭代。为此,您可以使用enumerate

    这个呢:

    def analyse(splitString, wordToSearch):
        positionLibrary = [j for j, word in enumerate(splitString) if word == wordToSearch]
        instances = len(positionLibrary)
        return (positionLibrary, instances)
    
    splitString = ['the', 'alteration', 'of', 'motion', 'is', 'ever', 'proportional', 'to', 'the', 'motive', 'force', 'impressed', 'and', 'is', 'made', 'in', 'the', 'right', 'line', 'on', 'which', 'that', 'force', 'is', 'impressed']
    print analyse(splitString, 'impressed')
    # ([11, 24], 2)
    

    如果您确实想使用index(),它可以采用第二个参数,即您应该开始搜索的位置。例如,

    print splitString.index('impressed') # 11
    print splitString.index('impressed', 0) # 11
    print splitString.index('impressed', 12) # 24
    

    【讨论】:

    • 建议:将名称 analyse(splitString, wordToSearch) 更改为更有用的名称,例如 index_multiple(iterable, value)
    • @Kupiakos 我只是复制/粘贴了 OP 的代码 :-) 但我同意你的看法,它可能会更好。
    【解决方案2】:

    如果你喜欢这样尝试:-

    def index_count_search(sentance, search):
         searchedList = map(lambda x:x[0], filter(lambda (index, value): search == value, enumerate(sentance.split())))
         return (",".join(searchedList), len(searchedList), search)
    
    
    wordToSearch = input("What word are you searching for instances of? ").lower()
    print analyse("THE ALTERATION OF MOTION IS EVER PROPORTIONAL TO THE MOTIVE FORCE IMPRESSED AND IS MADE IN THE RIGHT LINE ON WHICH THAT FORCE IS IMPRESSED".lower(), wordToSearch)
    

    【讨论】:

      猜你喜欢
      • 2016-06-03
      • 2019-04-30
      • 1970-01-01
      • 2017-10-22
      • 1970-01-01
      • 1970-01-01
      • 2020-07-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多