英文:
Making list from elements from each level of a nested list
问题
I have a list:
cleaner = [['Re:', '31_300323', 'RE777']]
I want to check if another list contains the same value. I write:
any('31_300323' in sl for sl in cleaner)
, and get "True," but if I write:
suka = []
for h in cleaner:
if any(h in sl for sl in cleaner):
suka.append(h)
the empty list remains empty. Why? Thank You
英文:
I have a list:
cleaner = [['Re:', '31_300323', 'RE777']]
I want to check if another list contains same value, I write:
any('31_300323' in sl for sl in cleaner)
,
and get "True", but if I write:
suka = []
for h in cleaner:
if any(h in sl for sl in cleaner):
suka.append(h)
the emptry list remains empty. Why? Thank You
答案1
得分: 0
让我们迭代您的代码,当您写“for h in cleaner”时,这意味着h是cleaner中的每个元素,因此cleaner中的第一个且唯一的元素是['Re:', '31_300323', 'RE777'],所以现在h将成为我提到的列表。
现在让我们看看您的if条件,在此条件中,您正在检查h是否在sl中,其中sl是['Re:', '31_300323', 'RE777'],根据上面段落中的相同逻辑,由于sl的元素中没有['Re:', '31_300323', 'RE777'],因此它将返回false,因此您的列表为空。
不客气。
英文:
Let's iterate your code,
when you write "for h in cleaner" it means that h is every element in cleaner so the first and only element in cleaner is ['Re:', '31_300323', 'RE777'] so now h would be the list i mentioned.
Now let us see your if condition in this condition you are checking if h in sl where sl is ['Re:', '31_300323', 'RE777'] by the same logic in above paragraph since sl doesn't have ['Re:', '31_300323', 'RE777'] as its element therefore it would return false and therefore your list is empty.
You are welcome
答案2
得分: 0
flatten function: 用于展平 nested_list
Code:
def flatten(lst):
"""展平任意深度的嵌套列表。"""
flattened = []
for item in lst:
if isinstance(item, list):
flattened.extend(flatten(item))
else:
flattened.append(item)
return flattened
cleaner=[['Re:', '31_300323', 'RE777','21_110123'],[['26_100822']]]
zajavki=['21_220223', '21_110123', '23_200123', '26_100822', '25_260123', '31_300323']
# 展平嵌套列表
cleaner_flat = flatten(cleaner)
suko=[]
for word in cleaner_flat:
if word in zajavki:
suko.append(word)
print(suko) # ['31_300323','21_110123','26_100822']
注意:如果不希望有重复项,可以使用数据结构 set()
假设您知道 nested_list
的深度,那么就不需要对其进行展平。
Code:
cleaner=[['Re:', '31_300323', 'RE777'],['Ze:', '23_200123', 'RE778'],['De:', '21_220223', 'RE779']]
zajavki=['21_220223', '21_110123', '23_200123', '26_100822', '25_260123', '31_300323']
suko=[]
for sublist in cleaner:
for check in sublist:
if check in zajavki:
suko.append(check)
print(suko) # ['31_300323', '23_200123', '21_220223']
英文:
flatten function: used to flatten the nested_list
Code:
def flatten(lst):
"""Flattens a nested list of any depth."""
flattened = []
for item in lst:
if isinstance(item, list):
flattened.extend(flatten(item))
else:
flattened.append(item)
return flattened
cleaner=[['Re:', '31_300323', 'RE777','21_110123'],[[['26_100822']]]]
zajavki=['21_220223', '21_110123', '23_200123', '26_100822', '25_260123', '31_300323']
# Flatten the nested list
cleaner_flat = flatten(cleaner)
suko=[]
for word in cleaner_flat:
if word in zajavki:
suko.append(word)
print(suko) # ['31_300323','21_110123','26_100822']
Note* If you don't want duplicates you can use data structure set()
Let's say you known the depth of the nested_list
than you don't need to flatten
the nested list
Code:
cleaner=[['Re:', '31_300323', 'RE777'],['Ze:', '23_200123', 'RE778'],['De:', '21_220223', 'RE779']]
zajavki=['21_220223', '21_110123', '23_200123', '26_100822', '25_260123', '31_300323']
suko=[]
for sublist in cleaner:
for check in sublist:
if check in zajavki:
suko.append(check)
print(suko) #['31_300323', '23_200123', '21_220223']
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论