Search code examples
pythonlistfor-loopif-statementtuples

How do i check a list (T) of tuples up against a list (H), followed by extracting +-5 of these tuples in list which corresponds with element in H)?


I haven't done much list/tuple checking and i wanted to try it. I want to extract elements from list, compare lists containing tuples and add some statement to them.

count1 = 100
theCounter = range(count1)
rsData = 56
T = []
R = [56,112,168,224,280]
H = [95,74,53,32,11]
for i in theCounter:
    T.append((count1,rsData))
    count1 = count1 - 1
    if (count1/25).is_integer() :
        rsData = rsData + 56
    
print(R)
print(H)
print(T)

Here is the problem:

T is a list of tuples. [(100, 56), (99, 56), (98, 56), (97, 56), (96, 56), (95, 56), (94, 56), (93, 56), (92, 56), (91, 56), (90, 56), (89, 56), (88, 56), (87, 56), (86, 56), (85, 56), (84, 56), (83, 56), (82, 56), (81, 56), (80, 56), (79, 56), (78, 56), (77, 56), (76, 56), (75, 112), (74, 112), (73, 112), (72, 112), (71, 112), (70, 112), (69, 112), (68, 112), (67, 112), (66, 112),....]

I want to search the first element in each tuple in T (this, notThis) and extract the tuples that corresponds with list H and make new lists. Followed by this i want the 5 elements before and 5 elements after.

Example: from H[1] = 74 list_H_74 = [(79, 56), (78, 56), (77, 56), (76, 56), (75, 112), (74, 112), (73, 112), (72, 112), (71, 112), (70, 112), (69, 112)]

After this i want to check list_H_74 2nd tuple elements (rsData) up against my element in list R, which must correspond with the element in H. Example: if R[1] = 112 (check against H[1] (list_H_74)) list_R_112_H_74 =

This list_R_112_H_74 need some conditions:

  • 2nd tuple's (rsData) in list_R_112_H_74 must be 112 == R[1] before or where the first tuple's reaches 75 == H[1]
  • if it has reached 112 it can't go back to 56

How do i complete this task without making hundreds of unnecessary lines with code?

Probably not the easiest qeustion on here, but maybe there is some smart fucntions to use when comparing lists.

I tried stack overflow, but there aren't many questions on here where they are comparing tuples and lists this advanced.


Solution

  • This is the best I can come up with from my understanding of your problem, but it's not clear what you want the output to look like.

    output = {
        f"{x}": [y for y in T if y[0] >= x - 5 and y[0] <= x + 5 and y[1] == R[H.index(x)]]
        for x in H
    }
    print(output)
    

    Explanation:

    • For each item in H, create a list
    • The list should be populated with tuples from T where both of the following conditions are met:
      • The first item in the tuple should be within +/- 5 of the item from H
      • The second item in the tuple should be the value from the same index in R

    Output:

    {'95': [(100, 56), (99, 56), (98, 56), (97, 56), (96, 56), (95, 56), (94, 56), (93, 56), (92, 56), (91, 56), (90, 56)], '74': [(75, 112), (74, 112), (73, 112), (72, 112), (71, 112), (70, 112), (69, 112)], '53': [(50, 168), (49, 168), (48, 168)], '32': [], '11': []}