简体   繁体   中英

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']

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? 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 .

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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