John Ebenezer
John Ebenezer

Reputation: 51

How to minus off the elements of the tuples by it's own index or with some values in python

R = [(1, 2,3,4), (1, 3), (1, 4), (1,4, 5), (1, 6), (2, 3), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6), (4, 5), (4, 6), (5, 6)]

From the above set i just want to minus off the index or any values from the each element of the tuple.

And the expected answer would be

#R = [(1-0, 2-0,3-0,4-0), (1-1, 3-1), (1-2, 4-2) and so on

here is my code but i want it in a simplest way pls help

final_matches = {k: v for k,v in matches.items() if len(v) >= 4}

    for k, v in matches.items():
        if len(v) >= 3:
            print(k,':',v,'/',len(v))
            #for j in range(0,len(v)):
                #print(v[j])

Upvotes: 0

Views: 274

Answers (2)

John Ebenezer
John Ebenezer

Reputation: 51

v[:] = [x - k for x in v] can do this when you iterate through the dict.

Upvotes: 0

Ajax1234
Ajax1234

Reputation: 71451

You can try this:

R = [(1, 2,3,4), (1, 3), (1, 4), (1,4, 5), (1, 6), (2, 3), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6), (4, 5), (4, 6), (5, 6)]

new_list = [tuple(c-i for c in a) for i, a in enumerate(R)]

to get only tuples with length three:

new_list = [i for i in new_list if len(i) >= 3]

Final output:

[(1, 2, 3, 4), (-2, 1, 2)]

Upvotes: 1

Related Questions