Python 在列表中保留确切的单词,并删除其他单词

Python 在列表中保留确切的单词,并删除其他单词,python,python-3.x,list,Python,Python 3.x,List,这里我有一个列表a和另一个列表b,其中包括一些字符串。对于列表a中的字符串,我想保留列表b中出现的字符串。并删除列表b中未出现的其他字符串 例如: list_a = [['a','a','a','b','b','b','g','b','b','b'],['c','we','c','c','c','c','c','a','b','a','b','a','b','a','b']] list_b = ['a'] 我预期的结果是: 像这样获取列表:[['a','a','a'],['a','a','a'

这里我有一个列表a和另一个列表b,其中包括一些字符串。对于列表a中的字符串,我想保留列表b中出现的字符串。并删除列表b中未出现的其他字符串

例如:

list_a = [['a','a','a','b','b','b','g','b','b','b'],['c','we','c','c','c','c','c','a','b','a','b','a','b','a','b']]
list_b = ['a']
我预期的结果是:

像这样获取列表:[['a','a','a'],['a','a','a','a','a']

但是,当我运行代码时:

data = [['a','a','a','b','g','b'],['we','c','a','b','a','a','b','a','b']]
keep_words = ['a']
for document in data:
    print('######')
    for word in document:
        print(word)
        if word in keep_words:
            document.remove(word)
            print(document)
print('#####')
print(data)
我得到这个结果:

line 1:######
line 2:a
line 3:['a', 'a', 'b', 'g', 'b']
line 4:a
line 5:['a', 'b', 'g', 'b']
line 6:g
line 7:b
line 8:######
line 9:we
line 10:c
line 11:a
line 12:['we', 'c', 'b', 'a', 'a', 'b', 'a', 'b']
line 13:a
line 14:['we', 'c', 'b', 'a', 'b', 'a', 'b']
line 15:b
line 16:a
line 17:['we', 'c', 'b', 'b', 'a', 'b']
line 18:#####
line 19:[['a', 'b', 'g', 'b'], ['we', 'c', 'b', 'b', 'a', 'b']]
所以我很困惑: 为什么在第6行中,它打印的是“g”而不是“a”?因为在第5行中我们得到了一个列表['a','b','g','b',],所以在下一个for循环中,它应该在这个列表的开头得到单词'a'

任何人都可以告诉我为什么会发生这种情况,以及如何解决我的问题?多谢各位


在对数组进行迭代时,切勿从数组中删除元素,这里有一个解决方案,涉及到用所需的结果筛选替换子列表:

data = [['a','a','a','b','g','b'],['we','c','a','b','a','a','b','a','b']]
keep_words = ['a']

for i in range(len(data)):
  data[i] = [d for d in data[i] if d in keep_words] # only keep desired data

print(data) # ==> [['a', 'a', 'a'], ['a', 'a', 'a', 'a']]

在对数组进行迭代时,切勿从数组中删除元素,这里有一个解决方案,涉及到用所需的结果筛选替换子列表:

data = [['a','a','a','b','g','b'],['we','c','a','b','a','a','b','a','b']]
keep_words = ['a']

for i in range(len(data)):
  data[i] = [d for d in data[i] if d in keep_words] # only keep desired data

print(data) # ==> [['a', 'a', 'a'], ['a', 'a', 'a', 'a']]

正如在注释中提到的,如果您在迭代列表时对其进行变异,您将体验到以下类型的

另一种解决方案是利用Python的超级快速和可读的列表理解

In [33]: [[a for a in l if a in list_b] for l in list_a]
Out[33]: [['a', 'a', 'a'], ['a', 'a', 'a', 'a']]

注意,由于ListSub的大小在增长,您可能需要考虑使用一个比列表更快的集合来检查成员资格。它还将忽略任何重复条目

In [52]: import random

In [73]: import string

In [74]: keep_s = set(['a', 'b', 'e'])

In [75]: keep_l = ['a', 'b', 'e']

# Create a random document -- random choice of 'a'-'f' between 1-100 times
In [78]: def rand_doc():
    ...:     return [random.choice(string.ascii_lowercase[:6]) for _ in range(random.randint(1,100))]
    ...:

# Create 1000 random documents
In [79]: docs = [rand_doc() for _ in range(1000)]

In [80]: %timeit [[word for word in doc if word in keep_l] for doc in docs]
4.39 ms ± 135 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [81]: %timeit [[word for word in doc if word in keep_s] for doc in docs]
3.16 ms ± 130 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

正如在注释中提到的,如果您在迭代列表时对其进行变异,您将体验到以下类型的

另一种解决方案是利用Python的超级快速和可读的列表理解

In [33]: [[a for a in l if a in list_b] for l in list_a]
Out[33]: [['a', 'a', 'a'], ['a', 'a', 'a', 'a']]

注意,由于ListSub的大小在增长,您可能需要考虑使用一个比列表更快的集合来检查成员资格。它还将忽略任何重复条目

In [52]: import random

In [73]: import string

In [74]: keep_s = set(['a', 'b', 'e'])

In [75]: keep_l = ['a', 'b', 'e']

# Create a random document -- random choice of 'a'-'f' between 1-100 times
In [78]: def rand_doc():
    ...:     return [random.choice(string.ascii_lowercase[:6]) for _ in range(random.randint(1,100))]
    ...:

# Create 1000 random documents
In [79]: docs = [rand_doc() for _ in range(1000)]

In [80]: %timeit [[word for word in doc if word in keep_l] for doc in docs]
4.39 ms ± 135 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [81]: %timeit [[word for word in doc if word in keep_s] for doc in docs]
3.16 ms ± 130 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
在对文档中的word进行迭代时,切勿修改列表文档。请删除:。在迭代过程中修改副本或创建副本添加要复制的项目以保留,而不是删除不需要的项目。决不修改列表文档的可能重复项。在对文档中的word进行迭代时删除:。在迭代过程中修改副本或创建副本添加要保留的副本项,而不是删除不需要的项。副本可能重复