Python Forum
Сheck if an element from a list is in another list that contains a namedtuple
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Сheck if an element from a list is in another list that contains a namedtuple
#1
Hello, i have this code:

def some_generator(some_args):
    Import = namedtuple("Import", ["module", "name", "alias"]) # "module", "name" and "alias" can be listed
...
    for n in node.names:
        yield Import(module, n.name.split('.'), n.asname)
...
files = list()
requirements = list()
...
for member in members:
    files.append(member)
...
for requirement in self.some_generator(some_args):
    requirements.append(requirement)
...
and then i need to check if an element from "files" is in "requirements" (module, name or alias)
how to implement it?
Reply
#2
Do you mean this?
for file in files:
    if file in requirements:
        print(f"{file} is in requirements")
Reply
#3
(Oct-25-2022, 05:06 PM)ibreeden Wrote: Do you mean this?
for file in files:
    if file in requirements:
        print(f"{file} is in requirements")

yes, but i have namedtuple (module, name, alias and they can be listed)
Reply
#4
This is vague:
yes, but i have namedtuple (module, name, alias and they can be listed)
Do you want to check if file matches any of the fields in the tuple or a particular field?

If there is a match, what do you want to happen?
Reply
#5
(Oct-25-2022, 06:40 PM)deanhystad Wrote: This is vague:
yes, but i have namedtuple (module, name, alias and they can be listed)
Do you want to check if file matches any of the fields in the tuple or a particular field?

If there is a match, what do you want to happen?

"file" is it string, i need to check if an element from "files" (string) is in "requirements" (namedtuple) (module, name or alias)
Reply
#6
iberdeen's code does that. Is the problem that it does it for 1 tuple? Here I search multiple tuples (but I have only one file).
from random import randint
from secrets import randbelow

Thing = namedtuple("Thing", ("A", "B", "C"))

things = [Thing(randint(1, 10), randint(1, 10), randint(1, 10)) for _ in range(10)]

for thing in things:
    if 5 in thing:
        print(thing)
Output:
Thing(A=8, B=6, C=5) Thing(A=4, B=5, C=8) Thing(A=5, B=2, C=7)
A namedtuple is still a tuple. The names don't make any difference to "in".

If you have many of these tuples and you want to check many filenames, and you don't really care which tuple is matched (that is a lot of if's), consider making a set that contains all requirements.
from collections import namedtuple
from random import randint
from itertools import chain

Import = namedtuple("Import", ("module", "name", "alias"))
requirements = [Import(randint(1, 1000), randint(1, 1000), randint(1, 1000)) for _ in range(10)]
all_requirements = set(chain(*requirements))  # This will contain every value for module, name and alias that appears in any requirement

files = list(range(100, 200))
for file in files:
    if file in all_requirements:
        print(file)
Output:
126 133
elnk likes this post
Reply
#7
(Oct-25-2022, 08:15 PM)deanhystad Wrote: iberdeen's code does that. Is the problem that it does it for 1 tuple? Here I search multiple tuples (but I have only one file).
from random import randint
from secrets import randbelow

Thing = namedtuple("Thing", ("A", "B", "C"))

things = [Thing(randint(1, 10), randint(1, 10), randint(1, 10)) for _ in range(10)]

for thing in things:
    if 5 in thing:
        print(thing)
Output:
Thing(A=8, B=6, C=5) Thing(A=4, B=5, C=8) Thing(A=5, B=2, C=7)
A namedtuple is still a tuple. The names don't make any difference to "in".

If you have many of these tuples and you want to check many filenames, and you don't really care which tuple is matched (that is a lot of if's), consider making a set that contains all requirements.
from collections import namedtuple
from random import randint
from itertools import chain

Import = namedtuple("Import", ("module", "name", "alias"))
requirements = [Import(randint(1, 1000), randint(1, 1000), randint(1, 1000)) for _ in range(10)]
all_requirements = set(chain(*requirements))  # This will contain every value for module, name and alias that appears in any requirement

files = list(range(100, 200))
for file in files:
    if file in all_requirements:
        print(file)
Output:
126 133

thank you!
so if i want to delete element from "requirements" wich conrain in "files" is it code right and it work?
        for file in files:
            for requirement in requirements:
                if file in requirement:
                    requirements.remove(requirement)
Reply
#8
(Oct-26-2022, 10:32 AM)elnk Wrote: so if i want to delete element from "requirements" wich conrain in "files" is it code right and it work?

for file in files:
    for requirement in requirements:
        if file in requirement:
            requirements.remove(requirement)
No. You must never change a collection while iterating over it. You are iterating over "requirements" and then remove items of it. Instead make a copy of "requirements" and iterate over that copy.
elnk likes this post
Reply
#9
Don't remove, append. I would build a new list that only contains the requirements you want to keep, and I would use set operations to determine which requirements to save
from collections import namedtuple
from random import randint

Import = namedtuple("Import", ("module", "name", "alias"))

files = set(range(100, 200))

# All the requirements
requirements = [Import(randint(1, 1000), randint(1, 1000), randint(1, 1000)) for _ in range(10)]

# Requirements that have no match with any file.
requirements = [req for req in requirements if set(req).isdisjoint(files)]
print(*requirements, sep="\n")
elnk likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  list in dicitonary element problem jacksfrustration 3 625 Oct-14-2023, 03:37 PM
Last Post: deanhystad
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,091 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,466 May-01-2023, 09:06 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 878 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Find (each) element from a list in a file tester_V 3 1,155 Nov-15-2022, 08:40 PM
Last Post: tester_V
Question Keyword to build list from list of objects? pfdjhfuys 3 1,499 Aug-06-2022, 11:39 PM
Last Post: Pedroski55
  Membership test for an element in a list that is a dict value for a particular key? Mark17 2 1,159 Jul-01-2022, 10:52 PM
Last Post: Pedroski55
  How to find the second lowest element in the list? Anonymous 3 1,903 May-31-2022, 01:58 PM
Last Post: Larz60+
  check if element is in a list in a dictionary value ambrozote 4 1,879 May-11-2022, 06:05 PM
Last Post: deanhystad
  Split a number to list and list sum must be number sunny9495 5 2,196 Apr-28-2022, 09:32 AM
Last Post: Dexty

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020