将列表中的序列替换为Python中的值

时间:2022-09-01 21:00:15

I want to replace example two specific one-after-one going elements in a list with another element (elements).
For example - replace ["+", "="] with ["+="]:

我想用另一个元素(元素)替换列表中的示例两个特定的一对一运行元素。例如 - 用[“+ =”]替换[“+”,“=”]:

Input:

[3, "blah", "+", "foo", "=", "+", "="]

Output:

[3, "blah", "+", "foo", "=", "+="]

3 个解决方案

#1


The following, though very inefficient for long lists this will work:

以下,虽然对于长列表非常低效,但这将起作用:

loop=[3, "blah", "+", "foo", "=", "+", "="]
out=[]
prevdone=False
for i in range(len(loop)):
    if loop[i]=="+" and loop[i+1]=="=":
        out.append("+=")
        prevdone=True
    elif not(prevdone):
        out.append(loop[i])
    else:
        prevdone=False
print(out)

It iterates through the list and checks if the current and following characters meet conditions. If they do, it will add += and skip the next item.

它遍历列表并检查当前和后续字符是否满足条件。如果他们这样做,它将添加+ =并跳过下一个项目。

I have considered using "".join(list) and string.split("") but that wouldn't (I don't think) work for multiple-character elements.

我考虑使用“”.join(list)和string.split(“”),但不会(我认为)不适用于多字符元素。

As for a general function, it could be modified as such:

至于一般功能,可以这样修改:

def loopReplace(loopList, item1, item2):
    out=[]
    prevdone=False
    for i in range(len(loopList)):
        if loopList[i]==item1 and loopList[i+1]==item2:
            out.append(str(item1)+str(item2))
            prevdone=True
        elif not(prevdone):
            out.append(loopList[i])
        else:
            prevdone=False
    return out

#2


list = [3, "blah", "+", "foo", "=", "+", "="]

for index, item in enumerate(list):
    if item =='+' and list[index +1]=='=':
        list[index] = "+="
        del list[index + 1]

print(list)

#3


Ok, as I see these answers I think I'll post my solution. I've done a little wrapper function.

好的,当我看到这些答案时,我想我会发布我的解决方案。我做了一个小包装函数。

def replace(sequence, replacement, lst, expand=False):
    out = list(lst)
    for i, e in enumerate(lst):
        if e == sequence[0]:
            i1 = i
            f = 1
            for e1, e2 in zip(sequence, lst[i:]):
                if e1 != e2:
                    f = 0
                    break
                i1 += 1
            if f == 1:
                del out[i:i1]
                if expand:
                    for x in list(replacement):
                        out.insert(i, x)
                else:
                    out.insert(i, replacement)
    return out

#1


The following, though very inefficient for long lists this will work:

以下,虽然对于长列表非常低效,但这将起作用:

loop=[3, "blah", "+", "foo", "=", "+", "="]
out=[]
prevdone=False
for i in range(len(loop)):
    if loop[i]=="+" and loop[i+1]=="=":
        out.append("+=")
        prevdone=True
    elif not(prevdone):
        out.append(loop[i])
    else:
        prevdone=False
print(out)

It iterates through the list and checks if the current and following characters meet conditions. If they do, it will add += and skip the next item.

它遍历列表并检查当前和后续字符是否满足条件。如果他们这样做,它将添加+ =并跳过下一个项目。

I have considered using "".join(list) and string.split("") but that wouldn't (I don't think) work for multiple-character elements.

我考虑使用“”.join(list)和string.split(“”),但不会(我认为)不适用于多字符元素。

As for a general function, it could be modified as such:

至于一般功能,可以这样修改:

def loopReplace(loopList, item1, item2):
    out=[]
    prevdone=False
    for i in range(len(loopList)):
        if loopList[i]==item1 and loopList[i+1]==item2:
            out.append(str(item1)+str(item2))
            prevdone=True
        elif not(prevdone):
            out.append(loopList[i])
        else:
            prevdone=False
    return out

#2


list = [3, "blah", "+", "foo", "=", "+", "="]

for index, item in enumerate(list):
    if item =='+' and list[index +1]=='=':
        list[index] = "+="
        del list[index + 1]

print(list)

#3


Ok, as I see these answers I think I'll post my solution. I've done a little wrapper function.

好的,当我看到这些答案时,我想我会发布我的解决方案。我做了一个小包装函数。

def replace(sequence, replacement, lst, expand=False):
    out = list(lst)
    for i, e in enumerate(lst):
        if e == sequence[0]:
            i1 = i
            f = 1
            for e1, e2 in zip(sequence, lst[i:]):
                if e1 != e2:
                    f = 0
                    break
                i1 += 1
            if f == 1:
                del out[i:i1]
                if expand:
                    for x in list(replacement):
                        out.insert(i, x)
                else:
                    out.insert(i, replacement)
    return out