【问题标题】:Removing Tuples From A List Removes Some But Not All从列表中删除元组会删除一些但不是全部
【发布时间】:2018-02-20 23:27:58
【问题描述】:

我一定遗漏了一些非常明显的东西。

我有一个(短语,数字)对的元组列表。我想从我的停用词列表中删除包含停用词的短语的整个元组。

stopwords = ['for', 'with', 'and', 'in', 'on', 'down']
tup_list = [('faucet', 5185), ('kitchen', 2719), ('faucets', 2628),
            ('kitchen faucet', 1511), ('shower', 1471), ('bathroom', 1131),
            ('handle', 1048), ('for', 1035), ('cheap', 960), ('bronze', 807),
            ('tub', 797), ('sale', 771), ('sink', 762), ('with', 696),
            ('single', 620), ('kitchen faucets', 615), ('stainless faucet', 613),
            ('pull', 603), ('and', 477), ('in', 447), ('single handle', 430),
            ('for sale', 406), ('bathroom faucet', 392), ('on', 369),
            ('down', 363), ('head', 359), ('pull down', 357), ('wall', 351),
            ('faucet with', 350)]

for p,n in tup_list:
    print('p', p, p.split(), any(phrase in stopwords for phrase in p.split()))

print(len(tup_list))
for p,n in tup_list:
    if any(phrase in stopwords for phrase in p.split()):
        tup_list.remove((p,n))
        print('Removing', p)
print(len(tup_list))

print([item for item in tup_list if item[0] == 'in'])

当我运行上述内容时,我得到以下打印输出:

p faucet ['faucet'] False
p kitchen ['kitchen'] False
p faucets ['faucets'] False
p kitchen faucet ['kitchen', 'faucet'] False
p shower ['shower'] False
p bathroom ['bathroom'] False
p handle ['handle'] False
p for ['for'] True
p cheap ['cheap'] False
p bronze ['bronze'] False
p tub ['tub'] False
p sale ['sale'] False
p sink ['sink'] False
p with ['with'] True
p single ['single'] False
p kitchen faucets ['kitchen', 'faucets'] False
p stainless faucet ['stainless', 'faucet'] False
p pull ['pull'] False
p and ['and'] True
p in ['in'] True
p single handle ['single', 'handle'] False
p for sale ['for', 'sale'] True
p bathroom faucet ['bathroom', 'faucet'] False
p on ['on'] True
p down ['down'] True
p head ['head'] False
p pull down ['pull', 'down'] True
p wall ['wall'] False
p faucet with ['faucet', 'with'] True
29
Removing for
Removing with
Removing and
Removing for sale
Removing on
Removing pull down
Removing faucet with
22
[('in', 447)]

我的问题:为什么不删除包含('in', 447) 的元组?打印输出显示p in ['in'] True 表示“in”在停用词列表中,那么为什么tup_list.remove((p,n)) 不删除它?

【问题讨论】:

  • 当您从正在迭代的列表中删除元素时会发生这种情况 - 某些元素会丢失。一种解决方案是遍历列表的副本 - for p,n in tup_list[:]:
  • @jasonharper 谢谢。我没有考虑这个事实;我试图通过不创建新列表来“高效”,但在这种情况下显然不应该这样做。

标签: python python-3.x list tuples


【解决方案1】:

当您从列表中删除一个项目时,索引会发生变化。当您遍历一个发生变化的列表时,您会看到意想不到的结果。

这是一种解决方案。这不是最有效的,但可能适合您的需求。

remove_indices = []

for i, (p, n) in enumerate(tup_list):
    if any(phrase in stopwords for phrase in p.split()):
        remove_indices.append(i)
        print('Removing', p)

tup_list = [i for j, i in enumerate(tup_list) if j not in remove_indices]

【讨论】:

  • OCD 注意:for i in range(len(someseq)): 后跟使用i 索引someseq 是一种反模式。您可以使用 enumerate 并仍然直接解压缩为有用的(-ish)名称:for i, (p, n) in enumerate(tup_list):
猜你喜欢
  • 2019-02-05
  • 2021-12-26
  • 2015-09-26
  • 2021-10-06
  • 2013-06-14
  • 2019-08-22
  • 1970-01-01
  • 1970-01-01
  • 2017-09-21
相关资源
最近更新 更多