字符串中子字符串的基本索引重复(python) [英] Basic indexing recurrences of a substring within a string (python)

查看:115
本文介绍了字符串中子字符串的基本索引重复(python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在自学基础编程。

一个简单的项目是找到字符串中子字符串的重复索引。所以例如,在字符串abcdefdef和子串def中,我希望输出为3和6.我有一些代码写,但我没有得到我想要的答案。以下是我写的

I'm working on teaching myself basic programming.
One simple project is to find the index of recurrences of a substring within a string. So for example, in string "abcdefdef" and substring "def", I would like the output to be 3 and 6. I have some code written, but I'm not getting the answers I want. Following is what I have written

注意:我知道可能更容易生成结果的方法,利用语言的内置功能/包,例如正则表达式。我也知道我的方法可能不是最佳算法。从来没有,在这个时候,我只是在寻求修复以下逻辑的建议,而不是使用更多惯用的方法。

Note:I'm aware that there may be easier way to produce the result, leveraging built-in features/packages of the language, such as Regular Expressions. I'm also aware that my approach is probably not an optimal algorithm. Never the less, at this time, I'm only seeking advice on fixing the following logic, rather than using more idiomatic approaches.

import string

def MIT(String, substring): # "String" is the main string I'm searching within
    String_list = list(String)
    substring_list = list(substring)
    i = 0
    j = 0
    counter = 0
    results = []
    while i < (len(String)-1):
        if [j] == [i]:
            j = j + 1
            i = i + 1
            counter  = counter + 1
            if counter == len(substring):
                results.append([i - len(substring)+1])
                counter = 0
                j = 0
                i = i+1
        else:
            counter = 0
            j = 0
            i = i+1
    print results
    return

我的推理理由就是这样。我将String和子字符串转换为列表。这允许索引字符串中的每个字母。我设置i和j = 0 - 这些将分别是我在String和substring索引中的第一个值。我还有一个新变量counter,我将其设置为0.基本上,我使用计数器来计算位置[i]中的字母等于位置[j]中的元素的次数。如果counter等于substring的长度,那么我知道[i-len(substring)+ 1]是我的子串开始的位置,所以我将它添加到名为results的列表中。然后我重置计数器和j并继续搜索更多的子串。

My line of reasoning is as such. I turn the String and substring into a list. That allows for indexing of each letter in the string. I set i and j = 0--these will be my first values in the String and substring index, respectively. I also have a new variable, counter, which I set = to 0. Basically, I'm using counter to count how many times the letter in position [i] is equal to the element in position [j]. If counter equals the length of substring, then I know that [i - len(substring) + 1] is a position where my substring starts, so I add it to a list called results. Then I reset counter and j and continue searching for more substrings.

我知道代码很尴尬,但我认为我仍然可以得到答案。相反,我得到:

I know the code is awkward, but I thought that I should still be able to get the answer. Instead I get:

>>> MIT("abcdefghi", "def")
[[3]]
>>> MIT("abcdefghi", "efg")
[[3]]
>>> MIT("abcdefghi", "b")
[[1]]
>>> MIT("abcdefghi", "k")
[[1]]

任何想法?

推荐答案

首先,我在您的代码中添加了一些注释以提供一些提示

First, I added some comments to your code to give some tips

import string

def MIT(String, substring): 
    String_list = list(String)  # this doesn't need to be done; you can index strings
    substring_list = list(substring)
    i = 0
    j = 0
    counter = 0
    results = []
    while i < (len(String)-1):   
        if [j] == [i]:   # here you're comparing two, one-item lists. you must do substring[j] and substring[i]
            j = j + 1
            i = i + 1
            counter  = counter + 1
            if counter == len(substring):
                results.append([i - len(substring)+1]) # remove the brackets; append doesn't require them
                counter = 0
                j = 0
                i = i+1 # remove this 
        else:
            counter = 0
            j = 0
            i = i+1
print results
return

这是我的方式如果不使用内置库就可以做到这样:

Here's how I would do it without using built-in libraries and such:

def MIT(fullstring, substring):
    results = []
    sub_len = len(substring)
    for i in range(len(fullstring)):  # range returns a list of values from 0 to (len(fullstring) - 1)
        if fullstring[i:i+sub_len] == substring: # this is slice notation; it means take characters i up to (but not including) i + the length of th substring
            results.append(i)
    return results

这篇关于字符串中子字符串的基本索引重复(python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆