简体   繁体   中英

Find an element by inner tuple in a list of a tuple of tuples

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; ie 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 ; ie if it is (9, 2), (4, 7), (7, 3), (5, 0) or (8, 1)

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.

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")

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM