交换列表中的多个项目

时间:2018-10-14 23:51:22

标签: python list

我想交换列表中的项目,但总是交换两个单词(“ go”,“ chargers”)并举个例子:

>lst = ['hello', 'go', 'hi', 'chargers']
>lst = ['hello', 'go', 'chargers', 'hi']

我可以使用列表分配:

lst[2], lst[3] = lst[2], lst[3]

但是将其概括化,它将如何工作,例如

['hello', 'go', 'sun', 'chargers', 'good', 'go', 'can', 'chargers']
['hello', 'go', 'chargers','sun', 'good', 'go', 'chargers', 'can']

我该如何配对所有“充电器”

4 个答案:

答案 0 :(得分:0)

在截断的列表上使用enumerate,然后使用多变量赋值进行交换

for i, v in enumerate(lst[:-2]):
    if v == 'go' and lst[i + 2] == 'chargers':
        lst[i + 1], lst[i + 2] = lst[i + 2], lst[i + 1]

print(lst) # => ['hello', 'go', 'chargers', 'sun', 'good', 'go', 'chargers', 'can']

答案 1 :(得分:0)

假设您有相等数量的“充电器”和“充电器”并且它们被随机放置,则可以使用enumerate来获取它们的索引,然后执行交换:

words = ['hello', 'go', 'sun', 'chargers', 'good', 'go', 'can', 'another word', 'chargers']

go_indices = [i for i, w in enumerate(words) if w == 'go']
charger_indices = [i for i, w in enumerate(words) if w == 'chargers']

for gi, ci in list(zip(go_indices, charger_indices)):
    words[gi + 1], words[ci] = words[ci], words[gi + 1]

print(words)
# ['hello', 'go', 'chargers', 'sun', 'good', 'go', 'chargers', 'another word', 'can']

答案 2 :(得分:0)

这不是一个很好的解决方案。首先确定充电器位置。一看便知,在充电器位置交换每个物品

lst = ['hello', 'go', 'go', 'chargers', 'good', 'sun', 'can', 'chargers']
index = 1
# loop opposite to make array, later pop them to get increasing order of sequence
allChargerIndex = [len(lst)-i-1 for i,val in enumerate(lst[::-1]) if val=="chargers"]
while index < len(lst):
    if lst[index-1] == "go":
        chargerIndex = allChargerIndex.pop()
        lst[index], lst[chargerIndex] = lst[chargerIndex], lst[index]
    index += 1
print(lst)

当然,您可以将其包含在函数中并尝试多种情况

def swapGoAndChargers(lst):
   index = 1
   # loop opposite to make array, later pop them to get increasing order of sequence
   allChargerIndex = [len(lst)-i-1 for i,val in enumerate(lst[::-1]) if val=="chargers"]
   while index < len(lst):
        if lst[index-1] == "go":
            chargerIndex = allChargerIndex.pop()
            lst[index], lst[chargerIndex] = lst[chargerIndex], lst[index]
        index += 1
   return lst

print(swapGoAndChargers(['hello', 'go', 'go', 'chargers', 'good', 'sun', 'can', 'chargers']))
#['hello', 'go', 'chargers', 'go', 'chargers', 'sun', 'can', 'good']
print(swapGoAndChargers(['hello', 'go', 'sun', 'chargers', 'good', 'go', 'can', 'chargers']))
#['hello', 'go', 'chargers', 'sun', 'good', 'go', 'chargers', 'can']
print(swapGoAndChargers(['go', 'hello', 'sun', 'go', 'good', 'chargers', 'can', 'chargers']))
#['go', 'chargers', 'sun', 'go', 'chargers', 'hello', 'can', 'good']

答案 3 :(得分:-1)

我并不是说这特别优雅或Pythonic。

def go_chargers(lst):
    o = []

    for i in range(0, len(lst)):
        o.append(lst[i])
        if lst[i] == 'go':
            for j in range(i + 2, len(lst)):
                if lst[j] == 'chargers':
                    lst[i+1], lst[j] = lst[j], lst[i+1]
                    break
    return o

print(go_chargers(['go', 'sun', 'chargers', 'good', 'go', 'can', 'chargers']))
# ['go', 'chargers', 'sun', 'good', 'go', 'chargers', 'can']

print(go_chargers( ["Rock", "go", "Stars", "go", "Paper", "chargers", "Scissors", "chargers"]))
# ['Rock', 'go', 'chargers', 'go', 'chargers', 'Stars', 'Scissors', 'Paper']