用另一个列表中的元素替换列表中的元素-Python [英] Replace element in list with element from another list - Python

查看:142
本文介绍了用另一个列表中的元素替换列表中的元素-Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为rule.start.的列表.在此列表中,所有元素都等于另一个名为com.empty的列表中的元素.我希望将rule.start中的元素替换为com.empty中相同元素之后的元素.我该怎么办?

I have a list named rule.start. In this list, all elements are equal to an element from another list called com.empty. I want the element from rule.start to be replaced by the element that comes AFTER the same element from com.empty. How would I do this?

rule.start看起来像这样:

['F', 'L', 'G', 'L', 'F', 'R', 'F', 'R', 'F', 'L', 'G', 'L', 'F', 'L', 'F', 'L', 'G', 'L', 'F', 'L',........]

com.empty看起来像这样:

['F', ['fd'], 'G', ['fd'], 'L', ['lt', '60'], 'R', ['rt', '60']]

我已经尝试过了:

wut = 0    
for elem in rule.start:
    v = 1
    for mel in com.empty[::2]:
        if elem == mel:
            rule.start[wut] = com.empty[v]
            print elem
            print mel
            wut +=1
            v += 2

但是它只是将rule.start的所有元素替换为['fd']

But it just replaces all element of rule.start with ['fd']

最后,我想将所有元素都评估为命令,如下所示:fd(var, scale)

In the end, I want to evaluate all elements as commands, that looks like this: fd(var, scale)

和这个:rt(var, 60) # 60 is from the list.

推荐答案

首先,如果您有字典,可以将键映射到值,而不是列表,这将更容易编写,并且效率更高.键和值.您可以单线执行此操作:

First, this would be much easier to write, and a lot more efficient, if you had a dictionary, mapping keys to values, instead of a list of alternating keys and values. You can do that as a one-liner:

mapping = dict(zip(com.empty[::2], com.empty[1::2]))

但是,如果您不了解它是如何工作的,则可以这样做,您应该能够理解(基于代码).

But if you don't understand how that works, you can do this, which you should be able to understand (based on your code).

mapping = {}
key = None
for mel in com.empty:
    if key is None:
        key = mel
    else:
        mapping[key] = mel
        key = None

或者,如果可能的话,甚至更好,更改代码以构建字典,而不是首先构建交替值列表.

Or, even better, if possible, change your code to build a dict instead of a list of alternating values in the first place.

现在,一旦有了这个,循环就变得非常简单,而且很难出错:

Now, once you have this, your loop becomes dead simple, and very hard to get wrong:

for wut, elem in enumerate(rule.start):
    rule.start[wut] = mapping.get(elem, elem)

我正在使用 enumerate ,所以您不会必须手动跟踪wut,而我正在使用 dict.get 方法,而不是遍历所有键和值,这是您迷失的棘手部分.

I'm using enumerate so you don't have to keep track of wut manually, and I'm using the dict.get method instead of looping over all the keys and values, which is the tricky part that you got lost in.

或者,甚至更简单:

rule.start = [mapping.get(elem, elem) for elem in rule.start]


但是,如果您想知道自己的尝试出了什么问题:


But, if you want to know what's wrong with your attempt:

您从v = 1开始.对于每个mel,您将v递增2 if elem == mel.否则,它将保持为1.因此,第一次elem == mel,您将拥有v = 1,因此您将把com.empty[1]分配给rule.start[wut].如果将v + 2移到循环外,将解决此问题.

You start off v = 1. For each mel, you increment v by 2 if elem == mel. Otherwise, it stays at 1. So, the first time elem == mel, you're going to have v = 1, so you're going to assign com.empty[1] to rule.start[wut]. If you move the v + 2 outside the loop, that will fix this problem.

您也会遇到类似的问题,因为wut += 1放在错误的位置.应该为每个elem更新;相反,它会为每个匹配的elemmel更新,该更新可能是0次或(理论上)是3次.

You also have a similar problem with wut += 1 being in the wrong place. It should be updated for every elem; instead, it's updated for every elem and mel that match, which could be 0 times or (theoretically) 3 times.

正确处理这些内容是导致错误的主要原因,这正是enumerate存在的原因.

Getting this stuff right is a big source of errors, which is exactly why enumerate exists.

这篇关于用另一个列表中的元素替换列表中的元素-Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆