[英]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_list
在empty
列表中,则返回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
:您可以使用
zip
与all
:
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.