Python Forum
Check if a list exists in given list of lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Check if a list exists in given list of lists
#1
I want to check if a list of tuples contains a specific tuple. If
numbers = [('a','a'), ('c','a')]
look = ('a','c')
"look" should not be contained in "numbers". But that's how my code thinks right now.
  def find_edge(self, src, dst):
        """
        Returns True if there's an edge from node `src` to node `dst`.
        """
        # Check if nodes exist
        if self.find_node(src) is True and self.find_node(dst) is True:
            src_node = self.return_head(src)

            src_edges = src_node.list_edges()
            src_edges_weightless = [x[:-1] for x in src_edges]

            possible_edge = (src, dst)

            for elem in src_edges_weightless:
                if collections.Counter(elem) == collections.Counter(possible_edge):
                    return True

        return False
[Image: 6DHp5pg.jpg]

So when looking if ("a","c") is contained in the list, return should be FALSE. However right now it returns TRUE. Any ideas why?
Reply
#2
Solved it by adding changing to this:

            for elem in src_edges_weightless:
                if collections.Counter(elem) == collections.Counter(possible_edge):
                    if elem[0] == possible_edge[0] and elem[1] == possible_edge[1]:
                        return True
Reply
#3
This is why your original program did not work:
import collection
x = collections.Counter(['a', 'b'])
y = collections.Counter(['b', 'a'])
print(x)
print(y)
print(x==y)
Output:
Counter({'a': 1, 'b': 1}) Counter({'b': 1, 'a': 1}) True
Counter does not care about order of entries in the source collection.

This is what your function does with all the extra stuff removed:
numbers = [('a','a'), ('c','a')]

def find_edge(nodes, x, y):
    for node in nodes:
        if x == node[0] and y == node[1]:
            return True
    return False

print(find_edge(numbers, 'a', 'c'))
print(find_edge(numbers, 'c', 'a'))
Output:
False True
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to reverse a list and store in another list in python SuperNinja3I3 6 3,231 Aug-14-2022, 06:36 PM
Last Post: DeaD_EyE
Question Python - List - Int List sophi 8 2,463 Apr-21-2022, 07:55 PM
Last Post: sophi
  list of strings to list of float undoredo 3 2,630 Feb-19-2020, 08:51 AM
Last Post: undoredo
  arrays sum list unsupported operand type(s) for +=: 'int' and 'list' DariusCG 7 4,075 Oct-20-2019, 06:24 PM
Last Post: Larz60+
  have homework to add a list to a list using append. celtickodiak 2 1,968 Oct-11-2019, 12:35 PM
Last Post: ibreeden
  Search character from 2d list to 2d list AHK2019 3 2,439 Sep-25-2019, 08:14 PM
Last Post: ichabod801
  I donr know how to search from 2d list to 2d list AHK2019 1 1,730 Sep-25-2019, 01:59 AM
Last Post: ichabod801
  sort a list alphabeticaly without changing the original list Holmen 5 4,138 Jan-27-2019, 01:49 PM
Last Post: Holmen
  making a dictionary from a list, one key with multiple values in a list within a list rhai 4 3,546 Oct-24-2018, 06:40 PM
Last Post: LeSchakal
  Need help with List of Lists johnissa 13 5,879 Apr-22-2018, 09:29 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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