I have a list, and I want to remove items from it that do not appear in another list. I tried the following:
for w in common: for i in range(1,n): if not w in words[i]: common.remove(w)
for w in common: for i in range(1,n): print w if not w in words[i]: print w common.remove(w)
I think you can simplify your statement like this:
filtered = filter(lambda x: x in words, common)
, . , x not in words , , , .
x not in words
, , , .
filtered = [x for x in common if x in words]
- EDITED. , , . !
, . .
for w in common[:]: for i in range(1,n): if not w in words[i]: common.remove(w)
Python:
, ( , ). , (, ), .
You modify the list when you try to iterate through it. You can change the first line of code to iterate over a copy of the list (using common [:]).
If you delete (say) paragraph 5, then the old element 6 will now be paragraph 5. So, if you are thinking of going to paragraph 6, you will skip it.
Is it possible to repeat iteration on this list? Then index changes occur in parts that you have already processed.
Source: https://habr.com/ru/post/1760329/More articles:SQL query query using keywords - sqlhibernate validator - javaHow does the route finder work? - gpshow to check fckeditor loads fully or not - fckeditorgit clone with different file permissions - gitHow the site works for London - google-mapsCSS Show active element over div with overflow: hidden - javascriptDisplaying a property in another table using NHibernate - nhibernateAsyncTask aborted exception in Android? - androidDisplaying a property in a field from another table in NHibernate - hibernateAll Articles