[英]Python: Append to list only if multiple elements are not in another list
I am trying to create a new list based on a subset of elements.我正在尝试基于元素子集创建一个新列表。 The selection criteria is a partial string.
选择标准是部分字符串。 I have a working example, but I am trying to code this in a cleaner way such that the selection criteria itself is a list of elements.
我有一个工作示例,但我试图以更简洁的方式对此进行编码,以便选择标准本身是一个元素列表。
I want the source list element (which is itself a list) to be added to the new list only if multiple substrings are not present in any of the source list sub-elements.我希望仅当任何源列表子元素中不存在多个子字符串时才将源列表元素(它本身是一个列表)添加到新列表中。
Here is my working example code:这是我的工作示例代码:
links = [['abc', 'def'], ['ghi', 'jkl'], ['def', 'xyz']]
sublinks = []
for link in links:
if ((('ab' not in link[0]) and ('ab' not in link[1])) and\
(('xy' not in link[0]) and ('xy' not in link[1]))):
sublinks.append(link)
From the links
list, this achieves the result of appending only the element ['ghi', 'jkl']
since it is the only element from the source list that passes the matching criteria of elements not containing ab
or xy
in any of the sub-elements.从
links
列表中,这实现了仅附加元素['ghi', 'jkl']
的结果,因为它是源列表中唯一通过任何子中不包含ab
或xy
的元素的匹配标准的元素-元素。
The example code shows the logic I am trying to achieve, but I would instead like to place ab
and xy
into a list so that I can specify an arbitrary number of matching criteria.示例代码显示了我试图实现的逻辑,但我想将
ab
和xy
放入列表中,以便我可以指定任意数量的匹配条件。 The format of the source list (a list of lists with two elements) will remain the same.源列表(包含两个元素的列表)的格式将保持不变。
I have spent several hours trying to answer my own question, searching stackoverflow, trying to figure it out on my own, but I haven't had any success yet and any help is greatly appreciated!我花了几个小时试图回答我自己的问题,搜索stackoverflow,试图自己弄清楚,但我还没有取得任何成功,非常感谢任何帮助! Thank you
谢谢
Here is a one liner..only if I have understood the problem correctly.这是一个班轮..只有当我正确理解了问题时。 Please use a bigger "links" list to test this out.
请使用更大的“链接”列表进行测试。
links = [['abc', 'def'], ['ghi', 'jkl'], ['def', 'xyz']]
FinalList = list(filter(lambda x: not any(['ab' in i for i in x]) and not any(['xy' in i for i in x]), links))
print(FinalList)
Gives output as给出 output 为
[['ghi', 'jkl']]
Edit编辑
After I realised that you want the negated terms in a list, I changed the code.在我意识到您需要列表中的否定项后,我更改了代码。 The below will also work.
以下也将起作用。 In the below code the most important lists are "links" which you want to filter and "Negation_List" which will have terms which you want to avoid.
在下面的代码中,最重要的列表是您要过滤的“链接”和包含您想要避免的术语的“Negation_List”。
links = [['abc', 'def'], ['ghi', 'jkl'], ['mno','pqr'],['mno','ghi'],['pqr','jkl'], ['def', 'xyz']]
Negation_List = ['ab','xy','mn','pq']
FinalList = list(filter(lambda x: all(not any([eachNegation in i for i in x]) for eachNegation in Negation_List), links))
print(FinalList)
Gives output as给出 output 为
[['ghi', 'jkl']]
Firstly, create a new list containing the criteria:首先,创建一个包含条件的新列表:
criteria_list = ['ab', 'xy']
The replace your for loop with this:用这个替换你的for循环:
for link in links:
in_list = True
for criteria_item in criteria_list:
for item in link:
if criteria_item in item:
in_list = False
if in_list:
sublinks.append(link)
This will loop through each sub-element in the 'links' list, checking each of these elements against all of the items in the criteria_list, only adding the whole sub-list to sublinks if none of the items are in the criteria_list.这将遍历“链接”列表中的每个子元素,根据条件列表中的所有项目检查这些元素中的每一个,如果条件列表中没有任何项目,则仅将整个子列表添加到子链接中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.