简体   繁体   English

如何打印另一个字符串中某些字符第一次出现的索引?

[英]how can I print the index of the first occurrence of some characters in another string?

I'm trying to make a function made up of two strings:我正在尝试制作一个由两个字符串组成的 function :

def find_firstchars( chars, wholestring )

Which, when printed, returns the positions of each of those characters in the char string.哪个,当打印时,返回每个字符在 char 字符串中的位置。 The first character in "chars" must mark the beginning of the index for the next character, so A is indexed first, then the function carries on indexing the next character after that position, and so on. “chars”中的第一个字符必须标记下一个字符的索引的开始,因此首先索引 A,然后 function 继续索引 position 之后的下一个字符,依此类推。

There may be multiple occurrences of the "chars" string in "wholestring", but I only need to index the characters of the first one. “wholestring”中可能多次出现“chars”字符串,但我只需要索引第一个的字符。

For example,例如,

print (find_firstchars( "ABC", "VMQOAJVBKRJCPGI" )

Would return the position list: [4, 7, 11]将返回 position 列表: [4, 7, 11]

I've tried something like the code below, and beside the error I get in console for this about slice indices needing to be integers, I'm not sure how to efficiently search for each character in the following string.我已经尝试过类似下面的代码,除了我在控制台中得到的关于切片索引需要是整数的错误之外,我不确定如何有效地搜索以下字符串中的每个字符。

def find_firstchars(chars, wholestring):
    index = 0 # Initializing index
    splice = [] # Initializing list
    while index != -1: # Run until at end of index
        index = chars.find(chars,wholestring) # Finds index value of each char in subsequence
        if index != -1: # If not at end:
            splice.append(index) # Append index value to splice list,
            index += 1 # Then keep looking
    return splice
print (find_firstchars("GTA", "ACGACATCACGTGACG"))

Though this should print [2, 6, 8].虽然这应该打印 [2, 6, 8]。

You are close:你很接近:

def find_firstchars(chars, wholestring):
    index = 0 # Initializing index
    splice = [] # Initializing list
    for c in chars:
        index = wholestring.find(c,index)
        splice.append(index)
        index += 1 # Then keep looking
    return splice
print (find_firstchars("GTA", "ACGACATCACGTGACG"))

暂无
暂无

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

相关问题 如何将字符串从第一次出现的子字符串的索引切片到Python中第二次出现的子字符串? - How can I slice a string from the index of the first occurrence of a sub string to the second occurrence of a sub string in Python? 如何从python中另一个字符串的列表中找到字符串的首次出现 - How can I find a first occurrence of a string from a list in another string in python 如何获得字符串中另一个字符周围的一些字符? - How can I get some characters around another character in a string? 如何在Python中首次出现字母时拆分字符串? - How can I split a string at the first occurrence of a letter in Python? 如何找到列表中项目的每次出现并打印其每次出现的索引号? - How can I find each occurrence of a item in a list and print its each occurrences index number? 如何删除不是列表中的第一次出现? - How can I remove not a first occurrence in the list? 如何找到 python 字符串中第一次出现的子字符串? - How can I find the first occurrence of a sub-string in a python string? 如何找到python中另一个子字符串后出现的第一个子字符串? - How can I find the first occurrence of a substring occurring after another substring in python? 如何更改 Kivy 字符串中某些字符的偏移量? - How can I change offset for some characters in Kivy string? 如何替换字符串中的第二次出现? - How can I replace the second occurrence in a string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM