Python Forum
Searche each element of each tuple based 3 numbes - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Searche each element of each tuple based 3 numbes (/thread-41264.html)



Searche each element of each tuple based 3 numbes - zinho - Dec-09-2023

Hi.
I have combinations based list with tuple nested
How to find based 3 numbers of my choice if all this 3 numbes is inside of list of tups?

import itertools

all_num = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
*res, = itertools.combinations(all_num,6)
look_up_numbs = (17,19,20) >>> lookup for this numbers inside of each tuples
(Any_number,Any_number,Any_number,17,19,20)
it doens't metter the position of elements if all three [look_up_numbs] is inside any of tuples

(13, 14, 15, 17, 18, 20) > No return, it's wrong (miss 19)
(13, 14, 15, 17, 19, 20) > OK, correct
(13, 14, 15, 18, 19, 20) > No return, it's wrong (miss 17)
(13, 14, 16, 17, 18, 20) > No return, it's wrong (miss 19)
(17, 18, 19, 20, 25, 26) > OK, correct


RE: Searche each element of each tuple based 3 numbes - deanhystad - Dec-10-2023

There are many ways this can be done, but using sets and the intersection operator is probably the fastest.

https://docs.python.org/2/library/sets.html


RE: Searche each element of each tuple based 3 numbes - zinho - Dec-10-2023

Hi.

I hope someone guive an example, I'm stuck here.
I try something lik ethis, but dint wotk.
look_up_numbs = [item for item in res if (17 and 19 and 20) in item]
tks


RE: Searche each element of each tuple based 3 numbes - snippsat - Dec-10-2023

Something like this should work.
import itertools

all_num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
combinations = itertools.combinations(all_num, 6)
look_up_numbs = (17, 19, 20)
filtered_comb = [comb for comb in combinations if all(num in comb for num in look_up_numbs)]
# 5 first and last
print(filtered_comb[:5])
print(filtered_comb[-5:])
Output:
[(1, 2, 3, 17, 19, 20), (1, 2, 4, 17, 19, 20), (1, 2, 5, 17, 19, 20), (1, 2, 6, 17, 19, 20), (1, 2, 7, 17, 19, 20)] [(13, 16, 17, 18, 19, 20), (14, 15, 16, 17, 19, 20), (14, 15, 17, 18, 19, 20), (14, 16, 17, 18, 19, 20), (15, 16, 17, 18, 19, 20)]



RE: Searche each element of each tuple based 3 numbes - rob101 - Dec-10-2023

I'd like to help, but I'm unclear on exactly what it is that you're trying to do?

Are you trying to return any six number, or six specific numbers, or asking if six specific numbers are in the all_num object? ... which (b.t.w) would be better constructed like this:
all_num = list(range(1, 21))
What code have you written to achieve the goal? Maybe, if you were to post that up, I could better understand what it is that you're trying to do.


RE: Searche each element of each tuple based 3 numbes - zinho - Dec-10-2023

hi.

This is my code:
import itertools

all_num = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
combinations = itertools.combinations(all_num, 6)
look_up_numbs = (17, 19, 20) # look inside of tuple but return only tuples that has this 3 number and another can be any
I need this result:
I write 5 here as exemple, but i need all possibles tuples that has corret desare 3 numbers inside.
... first combinations
(13, 14, 15, 17, 18, 20) > No return, it's wrong (miss 19)
(13, 14, 15, 17, 19, 20) > OK, correct
(13, 14, 15, 18, 19, 20) > No return, it's wrong (miss 17)
(13, 14, 16, 17, 18, 20) > No return, it's wrong (miss 19)
(17, 18, 19, 20, 25, 26) > OK, correct
.....50 milhions combinations


RE: Searche each element of each tuple based 3 numbes - deanhystad - Dec-10-2023

combinations = ((1, 2, 3, 4), (2, 3, 4, 5), (3, 4, 5, 6), (4, 5, 6, 7))
lookup_numbers = (2, 3, 4)

print("Using issubset()")
x = set(lookup_numbers)
for combo in combinations:
    if x.issubset(combo):
        print(combo)

print("\nUsing intersection")
x = set(lookup_numbers)
for combo in combinations:
    y = set(combo)
    if x & y == x:
        print(combo)

print("\nUsing all")
for combo in combinations:
    if all(x in combo for x in lookup_numbers):
        print(combo)

print("\nUsing len")
for combo in combinations:
    missing = [str(x) for x in lookup_numbers if x not in combo]
    if missing:
        print(combo, "is missing", ", ".join(missing))
    else:
        print(combo, "has it all")



RE: Searche each element of each tuple based 3 numbes - Pedroski55 - Dec-10-2023

This runs myApp() 250 times in 0.63 seconds

from timeit import timeit

def myApp():
    nums = [i for i in range(1, 21)]
    combinations = itertools.combinations(nums, 6)
    solutions = []     
    for combo in combinations:
        if 17 in combo:
            if 18 in combo:
                if 19 in combo:                    
                    solutions.append(combo)
    return solutions
                    
setup = ('from __main__ import myApp;' 'import itertools')
num = 250
time = timeit('myApp()', setup=setup, number=num)
print('Average time is:', time/250)
Output:
time 0.632492753007682



RE: Searche each element of each tuple based 3 numbes - zinho - Dec-11-2023

Hi

Thank everyone, especially ale this #7 solve my question.


tks