亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在python中用元素 - 1 替換元素

在python中用元素 - 1 替換元素

精慕HU 2022-06-28 15:26:37
我有一個問題:我有一個清單l = [ 'a', 'b', '"', 'c', 'd', '"', 'e'] 現在我想用之前的元素替換所有的'"'。這意味著:輸出 =[ 'a', 'b', 'b', 'c', 'd', 'd', 'e']具體的:    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", 'Bru?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', '', '']我需要用之前的元素替換所有'"'。誰能幫忙!!我要瘋了……
查看完整描述

5 回答

?
慕碼人2483693

TA貢獻1860條經驗 獲得超9個贊

易于理解的方法:


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))

由于您沒有指定如何處理第一個術語,所以我忽略它:)


編輯:仍然不太清楚你想連續使用 '"' 做什么,第二個 '"' 應該替換為 -2 術語,還是保持這樣?如果你想讓它保持這種狀態,你應該試試這個:


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))

您仍然沒有指定如何處理第一個術語,如果它是'"',它應該采用列表最后一個術語的值嗎?:)


查看完整回答
反對 回復 2022-06-28
?
幕布斯6054654

TA貢獻1876條經驗 獲得超7個贊

您可以通過列表理解來做到這一點:


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

但是,如果初始列表可以在一行中包含多個空項,則這不會按預期工作。在這種情況下,您可以這樣做:


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


查看完整回答
反對 回復 2022-06-28
?
紅糖糍粑

TA貢獻1815條經驗 獲得超6個贊

這是一種使用enumerate.


前任:


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


查看完整回答
反對 回復 2022-06-28
?
慕姐8265434

TA貢獻1813條經驗 獲得超2個贊

對于一個非常明確的方法,


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)


查看完整回答
反對 回復 2022-06-28
?
牛魔王的故事

TA貢獻1830條經驗 獲得超3個贊

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)


查看完整回答
反對 回復 2022-06-28
  • 5 回答
  • 0 關注
  • 152 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號