1

Alright. So I've been through some SO answers such as Find an element in a list of tuples in python and they don't seem that specific to my case. And I am getting no idea on how to use them in my issue.

Let us say I have a list of a tuple of tuples; i.e. the list stores several data points each referring to a Cartesian point. Each outer tuple represents the entire data of the point. There is an inner tuple in this tuple which is the point exactly. That is, let us take the point (1,2) and have 5 denoting some meaning to this point. The outer tuple will be ((1,2),5)

Well, it is easy to figure out how to generate this. However, I want to search for an outer tuple based on the value of the inner tuple. That is I wanna do:

for y in range(0, 10):
    for x in range(0, 10):
        if (x, y) in ###:
            print("Found")

or something of this sense. How can this be done?


Based on the suggestion posted as a comment by @timgen, here is some pseudo-sample data.
The list is gonna be

selectPointSet = [((9, 2), 1), ((4, 7), 2), ((7, 3), 0), ((5, 0), 0), ((8, 1), 2)]

So I may wanna iterate through the whole domain of points which ranges from (0,0) to (9,9) and do something if the point is one among those in selectPointSet; i.e. if it is (9, 2), (4, 7), (7, 3), (5, 0) or (8, 1)

Aaron John Sabu
  • 297
  • 1
  • 2
  • 13

3 Answers3

2

You can make use of a dictionary.

temp = [((1,2),3),((2,3),4),((6,7),4)]
newDict = {}

# a dictionary with inner tuple as key
for t in temp:
    newDict[t[0]] = t[1]

for y in range(0, 10):
    for x in range(0, 10):
        if newDict.__contains__((x,y)):
            print("Found")

I hope this is what you are asking for.

Aaron John Sabu
  • 297
  • 1
  • 2
  • 13
San
  • 453
  • 3
  • 14
  • You are shadowing the built in name `dict` by using that variable name. Your dict could simply be constructed with `dict(temp)`. The `has_key` method has been removed in Python 3. – timgeb May 27 '20 at 20:47
  • I am referring to this answer: https://stackoverflow.com/a/56356052/6539635 for modifying the answer in order to solve the `has_key` issue. Probably the change won't come up now. A moderator will have to accept it – Aaron John Sabu May 27 '20 at 20:48
2

Using the data structures that you currently are, you can do it like this:

listTuple = [((1,1),5),((2,3),5)] #dummy list of tuples
for y in range(0, 10): 
    for x in range(0, 10):
        for i in listTuple:#loop through list of tuples
            if (x, y) in listTuple[listTuple.index(i)]:#test to see if (x,y) is in the tuple at this index
                print(str((x,y)) , "Found")

Brenden Price
  • 517
  • 2
  • 9
  • I did think of this but it is a huge hierarchy in that case. And it's gotta go through listTuple every time, though that's pretty much what'd happen in the other case as well – Aaron John Sabu May 27 '20 at 20:34
1

Make a set from the two-element tuples for O(1) lookup.

>>> data = [((1,2),3),((2,3),4),((6,7),4)]
>>> tups = {x[0] for x in data}

Now you can query tups with any tuple you like.

>>> (6, 7) in tups
True
>>> (3, 2) in tups
False

Searching for values from 0 to 9:

>>> from itertools import product
>>> for x, y in product(range(10), range(10)):
...     if (x, y) in tups:
...         print('found ({}, {})'.format(x, y))
...         
found (1, 2)
found (2, 3)
found (6, 7)

If you need to retain information about the third number (and the two-element inner tuples in data are unique) then you can also construct a dictionary instead of a set.

>>> d = dict(data)
>>> d
{(1, 2): 3, (2, 3): 4, (6, 7): 4}
>>> (2, 3) in d
True
>>> d[(2, 3)]
4
timgeb
  • 76,762
  • 20
  • 123
  • 145
  • Aha, I was waiting for that last edit, it seems :-D Gonna have to accept this one – Aaron John Sabu May 27 '20 at 20:53
  • Just a doubt. `d = dict(data)` make a deep copy of `data`, right? I.e. If I modify a value in `d`, it won't affect `data`, right? – Aaron John Sabu May 27 '20 at 20:55
  • 1
    @AaronJohnSabu no copies are made. But you cannot mutate values in `d` because tuples and integers are immutable. You are probably thinking about reassigning keys in `d`. In that case `data` is unchanged. – timgeb May 28 '20 at 05:55