简体   繁体   English

将元素替换为 python 中的元素 - 1

[英]Replacing an element with the element - 1 in python

Hey there I have a question: i have a list嘿,我有一个问题:我有一个清单

l = [ 'a', 'b', '"', 'c', 'd', '"', 'e'] 

now i want to have all the '"' replaced by the element before. That means:现在我想用之前的元素替换所有的'"'。这意味着:

Output = [ 'a', 'b', 'b', 'c', 'd', 'd', 'e'] Output = [ 'a', 'b', 'b', 'c', 'd', 'd', 'e']

CONCRETE:具体的:

    Straßen_list = ['Katharinenstraße', 'iakobstraße', 'Katharinenstraße', 
'ilhelmsplatzKatharinen-', 'Hauptstätterstraße', '"', 'Wilhelmsplatzalter', 
'"', 'Schlosscrstraße', 'Houptstcktterstraße1^Uhlandapotheke\n', ';"', 
'!Torstraße[Einblick', 'HauptstätterstraßeNr50-36', '"', '"', 
"Hauptstätterstraße;Hs.i'ir.24", 'Brückenstraße', 'HauptstätterstraßeÖ', 
'Holzstraße', 'Rosenstraße', 'Holzstraße2', 'tilhlcurr', 
'HolzstraßeDanziger', 'EsslingerstraßeDanz.Freiheit', 'Danz.Freiheit', 
'Esslinger', 'RosenstraßeEsslinger8tr»', 'Esslinger', 'Esslinget', 
'bagnerstraße', 'Esslinger', 'Leonhardsplatz!Blickgegen', '*Holz-', '"', '"', 
'"', '<hardskirche\n', '', '']

and i need all '"' to be replaced with the element before.我需要用之前的元素替换所有'"'。

Can anyone please help.. I am going nuts...任何人都可以请帮助..我要疯了...

Easy to understand approach:易于理解的方法:

l = [ 'a', 'b', '"', 'c', 'd', '"', 'e']

def replacer(l):
    for i in range(1,len(l)):
        if l[i] == '"':
            l[i] = l[i-1]
    return(l)

print(replacer(l))

As you didn't specify what to do with the first term, I ignore it:)由于您没有指定如何处理第一个术语,我忽略它:)

EDIT : it's still not very clear what you want to do with '"' in a row, should the second '"' be replaced with -2 term, or stay like that?编辑:仍然不太清楚你想连续使用 '"' 做什么,第二个 '"' 应该替换为 -2 术语,还是保持这样? If you want it to stay like that, you should try this:如果你想让它保持这样,你应该试试这个:

l = [ 'a', 'b', '"', 'c', 'd', '"', 'e']

def replacer(l):
    out = [l[0]]
    for i in range(1,len(l)):
        if l[i] == '"':
            out.append(l[i-1])
        else:
            out.append(l[i])
    return(out)

print(replacer(l))

You still did not specify what to do with first term, if it's '"', should it take the value of the last term of the list? :)您仍然没有指定如何处理第一个术语,如果它是'"',它应该采用列表最后一个术语的值吗?:)

You can do it with a list comprehension:您可以通过列表理解来做到这一点:

l = [ 'a', 'b', '"', 'c', 'd', '"', 'e']
out = [elt if elt != '"' else l[i-1] for i, elt in enumerate(l)]

print(out)
# ['a', 'b', 'b', 'c', 'd', 'd', 'e']

This won't work as intended, though, if the initial list can contain more than one empty item in a row.但是,如果初始列表可以在一行中包含多个空项,则这不会按预期工作。 In this case, you could do:在这种情况下,您可以这样做:

data = [ 'a', 'b', '"', '"', 'c', 'd', '"', 'e']  # 2 " in a row

out = []
for elt in data:
    if elt != '"':
        last = elt
    out.append(last)

print(out)
# ['a', 'b', 'b', 'b', 'c', 'd', 'd', 'e']

This is one approach using enumerate .这是使用enumerate的一种方法。

Ex:前任:

l = [ 'a', 'b', '', 'c', 'd', '', 'e']
print([val if val.strip() else l[idx-1] for idx, val in enumerate(l)]) 
# --> ['a', 'b', 'b', 'c', 'd', 'd', 'e']

For a very explicit method,对于一个非常明确的方法,

l = [ 'a', 'b', '"', 'c', 'd', '"', 'e'] 
new_list = []

last = 'LAST' # In the event that the first string is '"'

for item in l:
    if item == '"':
        new_list.append(last)
    else:
        new_list.append(item)
        last = item

print(new_list)
characters = ["a", "b", "\"", "c", "d", "\"", "e"]
alpha_iter = iter(char for char in characters if char.isalpha())

characters_extended = []

previous_alpha = ""
for char in characters:
    if char.isalpha():
        characters_extended.append(char)
        previous_alpha = next(alpha_iter)
    else:
        characters_extended.append(previous_alpha)

print(characters_extended)

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

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