Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 忽略嵌套列表中的某些元素_Python_Web Scraping_List Comprehension_Nested Lists - Fatal编程技术网

Python 忽略嵌套列表中的某些元素

Python 忽略嵌套列表中的某些元素,python,web-scraping,list-comprehension,nested-lists,Python,Web Scraping,List Comprehension,Nested Lists,我有以下清单 [["abc","cdf","efgh","x","hijk","y","z"],["xyz","qwerty","uiop","x","asdf","y","z"]] 我想有以下输出 [["abc","cdf","efgh","hijk"],["xyz","qwerty","uiop","asdf"]] 如何在此处执行拆分操作? PS:原始数据相当大。 原始数据:我会为您的任务使用嵌套列表理解 old = [["abc","cdf","efgh","x","hijk","y"

我有以下清单

[["abc","cdf","efgh","x","hijk","y","z"],["xyz","qwerty","uiop","x","asdf","y","z"]]
我想有以下输出

[["abc","cdf","efgh","hijk"],["xyz","qwerty","uiop","asdf"]]
如何在此处执行拆分操作? PS:原始数据相当大。
原始数据:

我会为您的任务使用嵌套列表理解

old = [["abc","cdf","efgh","x","hijk","y","z"],["xyz","qwerty","uiop","x","asdf","y","z"]]

droplist = ["x", "y", "z"]
new = [[item for item in sublist if item not in droplist] for sublist in old]
print(new)
请注意新列表的创建。外部列表压缩考虑每个子列表。内部列表考虑单个字符串

当执行不在液滴列表中的if项时,会出现过滤器

如果项目不在droplist中,则可以用您可以编码的任何条件替换。例如:

new = [[item for item in sublist if len(item) >= 3] for sublist in old]
甚至:

def do_I_like_it(s):
    # Arbitrary code to decide if `s` is worth keeping
    return True
new = [[item for item in sublist if do_I_like_it(item)] for sublist in old]
如果要按项目在子列表中的位置删除项目,请使用切片:

# Remove last 2 elements of each sublist
new = [sublist[:-2] for sublist in old]
assert new == [['abc', 'cdf', 'efgh', 'x', 'hijk'], ['xyz', 'qwerty', 'uiop', 'x', 'asdf']]

输入和输出之间的关系是什么?还有,你试过什么?注意这是一个过滤操作,不是拆分。我正在尝试从网站上抓取某些数据。使用Selenium,我能够提取嵌套列表中的数据。我的问题是,我对嵌套列表中的某些元素不感兴趣。但是,是什么决定了您对哪些元素不感兴趣呢?长度是1字符串吗?etc..@chrisz如果你遵循链接,在给定的数据中,我对从follow开始比较的最后9个元素不感兴趣。我想从每个嵌套列表中删除所有这9个元素。将droplist设置为一个集合会更快吗?我想不是针对小的组。