简体   繁体   English

查找和替换List中条目内的所有值

[英]Finding and Replacing all Values inside of entries within a List

Question: 题:

Given a list of the form: 给出一个表格列表:

[((x1,y1,z1), (x2,y2,z2), variable_1, variable_2, variable_3),
((x3,y3,z3), (x1,y1,z1), variable_4, variable_5, variable_6),...
((x1,y1,z1), (xN,yN,zN), variable_M, variable_M+1, variable_M+2)]

What is the most efficient (and preferably universal, ie for any list) way to replace all entries in the list matching a value (say (x1,y1,z1) ) with another value (say (xM,yM,zM) )? 将值(例如(x1,y1,z1) )与另一个值(例如(xM,yM,zM) )匹配的列表中的所有条目替换为最有效(并且最好是通用的,即任何列表)的方式是什么?

Central to my problem is that each entry in the list is composed of multiple elements, and typical methods I use to replace values (eg [new_input if x == current else x for x in pairs] ) do not appear to be useful in this case. 我的问题的核心是列表中的每个条目都由多个元素组成,我用来替换值的典型方法(例如[new_input if x == current else x for x in pairs] )似乎没有用处案件。 Note the form of each entry is: ((x1,y1,z1), (x2,y2,z2), variable_1, variable_2, variable_3) , and the list is: [((x1,y1,z1), (x2,y2,z2), variable_1, variable_2, variable_3), ((x3,y3,z3), (x1,y1,z1), variable_4, variable_5, variable_6)] . 注意每个条目的形式是: ((x1,y1,z1), (x2,y2,z2), variable_1, variable_2, variable_3) ,列表是: [((x1,y1,z1), (x2,y2,z2), variable_1, variable_2, variable_3), ((x3,y3,z3), (x1,y1,z1), variable_4, variable_5, variable_6)] I'm looking to change elements within the entry. 我想改变条目中的元素。

Currently I am using a reliable if crude method in which the list is broken into columns and then replaces individual elements in the list, before zipping the columns back together. 目前我正在使用一种可靠的粗制方法,其中列表被分成列,然后替换列表中的各个元素,然后再将列压缩回来。 The code is roughly as follows: 代码大致如下:

current=(x1,y1,z1)
new_input=(xM,yM,zM)
pairs=#list as above
def column(list, col_num):
    return [column[col_num] for column in list]

p_c_1=column(pairs, 0)
p_c_2=column(pairs, 1)
p_c_3=column(pairs, 2)
p_c_4=column(pairs, 3)
p_c_5=column(pairs, 4)

for i in xrange(len(pairs)):
    if p_c_1[i]==current:
       p_c_1.remove(p_c_1[i])
       p_c_1.insert(i,new_input)
    if p_c_2[i]==current:
       p_c_2.remove(p_c_2[i])
       p_c_2.insert(i,new_input)

pairs=zip(p_c_1,p_c_2,p_c_3,p_c_4,p_c_5)

This method yields an output: 这种方法产生一个输出:

[((xM,yM,zM), (x2,y2,z2), variable_1, variable_2, variable_3),
((x3,y3,z3), (xM,yM,zM), variable_4, variable_5, variable_6),...
((xM,yM,zM), (xN,yN,zN), variable_M, variable_M+1, variable_M+2)]

The method has proved reliable for this particular case, but as there are multiple lists with individual entries composed of several items, I have to construct a new bit of code such as that above for each unique list, which seems crude. 对于这种特殊情况,该方法已被证明是可靠的,但由于存在多个列表,其中各个条目由若干项组成,因此我必须为每个唯一列表构建一个新的代码,例如上面的代码,这看起来很粗糙。 I would ideally want to avoid disassembling and reassembling the list every time I change an entry, or automate the process at least. 理想情况下,我希望每次更改条目时都避免反汇编和重新组合列表,或至少自动化该过程。

What is the best way to go about this issue? 解决这个问题的最佳方法是什么?

Note: A solution would ideally be able to handle the replacement of any element within a list entry, and not just the first two columns. 注意:理想情况下,解决方案可以处理列表条目中任何元素的替换,而不仅仅是前两列。 Other lists I have do not necessarily follow the above format. 其他列表我不一定遵循上述格式。

Create a dict that is a mapping of (things you want to replace) to (things to replace them with). 创建一个dict ,它是(要替换的东西)到(要替换它们的东西)的映射。

Then it's straightforward to make a new list comprehension using dict.get to default to the extant value if you haven't specified a replacement in your replacement-dict. 然后,如果你没有在replacement-dict中指定替换,那么使用dict.get将新列表理解为默认为现存值是很简单的。

li = [('a','c','b'),('b','b','c'),('c','d'),'5',4]

d = {('b','b','c'):('b','b','b'),('c','d'):('c','c')}

[d.get(x,x) for x in li]
Out[33]: [('a', 'c', 'b'), ('b', 'b', 'b'), ('c', 'c'), '5', 4]

Edit: since it seems you have a peculiar data structure (one giant tuple spanning the entire list ), you can do this: 编辑:因为看起来你有一个特殊的数据结构(一个跨越整个list巨型元组),你可以这样做:

[tuple(d.get(x,x) for x in li[0])]

Or, if you actually have more of those nested outer-tuples that you're just not showing, try: 或者,如果你实际上有更多那些你没有展示的嵌套外元组,请尝试:

[tuple(d.get(x,x) for x in outer_tup) for outer_tup in li]

Both get you back to your original list-of-tuples-of-(tuples and other things) data structure. 两者都会让您回到原始的元组列表(元组和其他东西)数据结构。

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

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