简体   繁体   English

如何检查列表中是否存在字符序列?

[英]How do I check if a sequence of characters exists in a list?

How do I check if a sequence of characters exists in a list? 如何检查列表中是否存在字符序列?

I have a string with some characters that have sequences that reoccur. 我有一个带有某些字符的字符串,这些字符具有重复出现的序列。 I know that strings are immutable so I turn the string into the list. 我知道字符串是不可变的,因此我将字符串转换为列表。 However, I'm not sure how to iterate through the list, find the occurrence and change the first letter of the occurrence. 但是,我不确定如何遍历列表,查找事件并更改事件的首字母。

message: DDMCAXQVEKGYBNDDMZUH
Occurence is: DDM

list: ['D', 'D', 'M', 'C', 'A', 'X', 'Q', 'V', 'E', 'K', 'G', 'Y', 'B', 'N', 'D', 'D', 'M', 'Z', 'U', 'H']

What I have currently is simply turning the message into the list. 我目前所拥有的只是简单地将消息变成列表。 I've tried different ways, which were unsuccessfully that's what I didn't post it. 我尝试了不同的方法,但没有成功,这就是我没有发布的方法。 Not really asking you to write the code but at the least explain how to achieve this. 并不是真正要求您编写代码,而是至少说明了如何实现此目的。

It's a lot easier to check if a string exists in another string since you can simply use the in operator: 检查字符串是否存在于另一个字符串中要容易得多,因为您可以简单地使用in运算符:

if 'DDM' in message:
     # do something

But since your goal is to change the first letter of the occurrence, you can use the str.index method to obtain the index of the occurrence and then assemble a new string with slices of the current string and the new letter: 但是,由于您的目标是更改事件的第一个字母,因此可以使用str.index方法获取事件的索引,然后使用当前字符串和新字母的切片组装一个新字符串:

try:
    i = message.index('DDM')
    message = message[:i] + new_letter + message[i + 1:]
except ValueError:
    raise RuntimeError("Sequence 'DDM' not found in message.")

You can use re.sub() : 您可以使用re.sub()

import re

s = 'DDMCAXQVEKGYBNDDMZUH'

re.sub(r'DDM', '$DM', s)
# $DMCAXQVEKGYBN$DMZUH

A simple solution with a for-loop would be: 一个for循环的简单解决方案是:

msg = 'DDMCAXQVEKGYBNDDMZUH'
occ = 'DDM'

for i in range(len(msg)):
    if msg[i:i+len(occ)] == occ:
        msg = msg[:i] + 'x' + msg[i+1:]

resulting in xDMCAXQVEKGYBNxDMZUH 导致xDMCAXQVEKGYBNxDMZUH

This also works with overlapping substrings. 这也适用于重叠的子字符串。 For example: 例如:

msg = 'AAABAA'
occ = 'AA'

will give xxABxA 将给xxABxA

The simplest way would be using string replace() function. 最简单的方法是使用字符串replace()函数。

string.replace(s, old, new[, maxreplace]) string.replace(s,old,new [,maxreplace])

Return a copy of string s with all occurrences of substring old replaced by new. 返回字符串s的副本,其中所有出现的子字符串old都替换为new。 If the optional argument maxreplace is given, the first maxreplace occurrences are replaced. 如果给出了可选参数maxreplace,则替换第一个出现的maxreplace。

message = "DDMCAXQVEKGYBNDDMZUH"
print message.replace("DDM", "ABC", 1)

Replace function would replace the first occurrence of DDM in the message string. 替换功能将替换消息字符串中第一次出现的DDM。

output: ABCCAXQVEKGYBNDDMZUH 输出:ABCCAXQVEKGYBNDDMZUH

If I carefully read your question you want to search the first occurrence of DDM in your message and replace the first character of it. 如果我仔细阅读了您的问题,您想搜索邮件中第一个出现的DDM并替换它的第一个字符。 In that case use below: 在这种情况下,请使用以下内容:

message = "DDMCAXQVEKGYBNDDMZUH"
print message.replace("DDM", "ADM", 1)

output: ADMCAXQVEKGYBNDDMZUH 输出:ADMCAXQVEKGYBNDDMZUH

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

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