Python Forum
Searche each element of each tuple based 3 numbes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Searche each element of each tuple based 3 numbes
#1
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
Reply
#2
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
Reply
#3
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
Reply
#4
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)]
Reply
#5
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.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#6
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
Reply
#7
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")
zinho likes this post
Reply
#8
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
Reply
#9
Hi

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


tks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 2,847 Nov-04-2020, 11:26 AM
Last Post: Aggam
  How to get first line of a tuple and the third item in its tuple. Need Help, Anybody? SukhmeetSingh 5 3,216 May-21-2019, 11:39 AM
Last Post: avorane
  Unable to locate element no such element gahhon 6 4,513 Feb-18-2019, 02:09 PM
Last Post: gahhon
  Converting List of 3 Element Tuple to Dictionary fooikonomou 11 5,849 Jan-14-2019, 09:51 AM
Last Post: perfringo
  Newbie question for sum the specific element in named tuple zydjohn 1 4,218 Dec-12-2017, 07:29 PM
Last Post: buran
  Change single element in 2D list changes every 1D element AceScottie 9 12,098 Nov-13-2017, 07:05 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020