简体   繁体   English

如何根据条件根据另一个相同长度的嵌套列表将列表中的项目替换为空字符串

[英]How to replace item in a list to empty string based on another nested list of same length based on condition

I have a header list as below:我有一个 header 列表如下:

h_list = ['h1','h2','h3','h4','h5']

Now I have data list (Nested):现在我有数据列表(嵌套):

d_list = [
[1, None, 3, ' ', 5],
[1, ' ', 2, ' ', 9]
]

Both lists are of same length every time, so I want to match in each list of nested list at same index position and if its all corresponding values are either None or ' ' , then replace the item from h_list to ' ' (Empty string)两个列表每次都具有相同的长度,因此我想在同一索引 position 的每个嵌套列表列表中进行匹配,并且如果其所有对应值都是None' ' ,则将项目从h_list替换为' ' (空字符串)

My expected output is:我预期的 output 是:

h_list = ['h1',' ','h3',' ','h5']

Try a list comprehension:尝试列表理解:

h_list = ['h1','h2','h3','h4','h5']


d_list = [
[1, None, 3, ' ', 5],
[1, ' ', 2, ' ', 9]
]

empty = [' ', None]
h_list = [' ' if any(b[i] in empty for b in d_list) else v for i, v in enumerate(h_list)]
print(h_list)

Output: Output:

['h1', ' ', 'h3', ' ', 'h5']

Breaking down this part of the code:分解这部分代码:

h_list = [' ' if any(b[i] in empty for b in d_list) else v for i, v in enumerate(h_list)]

First, lets have only首先,让我们只有

[(i, v) for i, v in enumerate(h_list)]

The above will be a list of the indices and values of each element in h_list .以上将是h_list中每个元素的索引和值的列表。

Now, we use an if statement to determine when to add the ' ' .现在,我们使用if语句来确定何时添加' ' First, we need to recognize the any() function:首先,我们需要识别any() function:

any(b[i] in empty for b in d_list)

returns True if any of the arrays inside d_list at index i is in the empty list.如果索引i处的 d_list 中的任何d_listempty列表中,则返回True We want None and ' ' to be in place for all the strings in h_list that its index returns a ' ' or None for any of the lists in d_list , so:我们希望对h_list中的所有字符串都使用None' ' ,它的索引对于d_list中的任何列表都返回' 'None ,所以:

[' ' for i, v in enumerate(h_list) if any(b[i] in empty for b in d_list)]

Finally, we want to use the original string if not any(b[i] in empty for b in d_list) .最后,我们想使用原始字符串 if not any(b[i] in empty for b in d_list) For that, we use an else statement (note that with an else , the statements get shifted to the left side of the for loop.) :为此,我们使用else语句(请注意,使用else时,语句会移到for循环的左侧。)

h_list = [' ' if any(b[i] in empty for b in d_list) else v for i, v in enumerate(h_list)]

I believe this should work for your examples:我相信这应该适用于您的示例:

new_list = []
for orig_element, *values in zip(h_list, *d_list):
    new_list.append(orig_element if any(not (v is None or str(v).strip() == '') for v in values) else '')

If you want to modify the list in-place simply do:如果您想就地修改列表,只需执行以下操作:

for i, (orig_element, *values) in enumerate(h_list, *d_list):
    h_list[i] = orig_element if any(not (v is None or str(v).strip() == '') for v in values) else ''

Basic solution基本解决方案

h_list = ['h1','h2','h3','h4','h5']
d_list = [
[1, None, 3, ' ', 5],
[1, ' ', 2, ' ', 9]
]
# loop over d_list
for i in d_list:
# loop over inner list 
    for k in i:
# if type not int, give me space in that index
        if type(k)!=int:
            h_list[i.index(k)]=" "
h_list

You can use zip with all :您可以使用zipall

h_list = ['h1','h2','h3','h4','h5']
d_list = [[1, None, 3, ' ', 5], [1, ' ', 2, ' ', 9]]
r = [' ' if all(k in {None, ' '} for k in j) else a for a, j in zip(h_list, zip(*d_list))]

Output: Output:

['h1', ' ', 'h3', ' ', 'h5']

暂无
暂无

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

相关问题 根据条件用另一个嵌套列表的值替换嵌套列表中的值 - Replace values in a nested list with values of another nested list based on a condition 如何根据特定条件用另一个列表替换多维数组python中的嵌套列表 - How to replace a nested list in multi-dimensional array python with another list based on a certain condition 如何根据另一个字符串列表随机替换列表中的字符串 - How to randomly replace a string in a list based on another list of strings 根据其他列条件用数据框中的另一个列表替换列表 - Replace list with another list in dataframe based on another column condition 如何根据条件替换列表中的多个元素? - How to replace multiple elements in a list, based on condition? 如何根据另一个二维列表的长度值创建一个空的二维列表? - how to create an empty 2 dimensional list based on another 2 dimensional list's length value? 如何在条件下对长度不等的锯齿状列表进行分组(基于每个重复项)Python - how to group jagged list with unequal length on condition (based on every repeated item) Python 如何根据列表中字符串的位置将列表中的字符串替换为另一字符串 - How would one replace a string in a list with another string based on the location of the string in the list 如何根据条件用具有相同结构的另一个字符串中的字符动态替换字符串中的字符? - How to dynamically replace chars in a string with chars in another string having same structure based on condition? 根据条件向python列表中的每个项目添加字符串 - Add string to each item in python list based on condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM