在循环期间删除列表中的项目 [英] Removing item in list during loop

查看:33
本文介绍了在循环期间删除列表中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的代码.我试图从列表 predict strings teststrings 中删除两个字符串,如果其中一个已在另一个字符串中找到.问题是我必须将它们分开,然后检查另一个内部是否有一个字符串的部分".如果有,那么我只是说有一个匹配项,然后从列表中删除这两个字符串,以使它们不再重复.

I have the code below. I'm trying to remove two strings from lists predict strings and test strings if one of them has been found in the other. The issue is that I have to split up each of them and check if there is a "portion" of one string inside the other. If there is then I just say there is a match and then delete both strings from the list so they are no longer iterated over.

ValueError: list.remove(x): x not in list

但是我得到了上面的错误,我想这是因为我无法从test_strings中删除字符串,因为它已经被迭代了吗?有没有解决的办法?

I get the above error though and I am assuming this is because I can't delete the string from test_strings since it is being iterated over? Is there a way around this?

谢谢

    for test_string in test_strings[:]:
        for predict_string in predict_strings[:]:
            split_string = predict_string.split('/')
            for string in split_string:
                if (split_string in test_string):
                    no_matches = no_matches + 1
                    # Found match so remove both
                    test_strings.remove(test_string)
                    predict_strings.remove(predict_string)

示例输入:

test_strings = ['hello/there', 'what/is/up', 'yo/do/di/doodle', 'ding/dong/darn']
predict_strings =['hello/there/mister', 'interesting/what/that/is']

所以我希望hello/there和hello/there/mister之间有一个匹配项,并在进行下一个比较时将它们从列表中删除.

so I want there to be a match between hello/there and hello/there/mister and for them to be removed from the list when doing the next comparison.

经过一轮迭代,我希望它是:

After one iteration I expect it to be:

test_strings == ['what/is/up', 'yo/do/di/doodle', 'ding/dong/darn']
predict_strings == ['interesting/what/that/is']

第二次迭代后,我希望它是:

After the second iteration I expect it to be:

test_strings == ['yo/do/di/doodle', 'ding/dong/darn']
predict_strings == []

推荐答案

在迭代过程中,绝对不要尝试修改它,而这实际上仍然是您要尝试做的事情.创建一个 set 来跟踪您的匹配项,然后在最后删除这些元素.

You should never try to modify an iterable while you're iterating over it, which is still effectively what you're trying to do. Make a set to keep track of your matches, then remove those elements at the end.

另外,您的代码行中split_string:中的字符串实际上并没有做任何事情.您没有使用变量 string.要么删除该循环,要么更改代码,以便使用 string .

Also, your line for string in split_string: isn't really doing anything. You're not using the variable string. Either remove that loop, or change your code so that you're using string.

您可以使用增广赋值来增加no_matches的值.

You can use augmented assignment to increase the value of no_matches.

no_matches = 0

found_in_test = set()
found_in_predict = set()

for test_string in test_strings:
    test_set = set(test_string.split("/"))
    for predict_string in predict_strings:
        split_strings = set(predict_string.split("/"))
        if not split_strings.isdisjoint(test_set):
            no_matches += 1
            found_in_test.add(test_string)
            found_in_predict.add(predict_string)

for element in found_in_test:
    test_strings.remove(element)

for element in found_in_predict:
    predict_strings.remove(element)

这篇关于在循环期间删除列表中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆