Posts: 58
Threads: 20
Joined: Jan 2017
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
Posts: 6,794
Threads: 20
Joined: Feb 2020
Dec-10-2023, 03:07 AM
(This post was last modified: Dec-10-2023, 03:07 AM by deanhystad.)
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
Posts: 58
Threads: 20
Joined: Jan 2017
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
Posts: 7,319
Threads: 123
Joined: Sep 2016
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)]
Posts: 453
Threads: 16
Joined: Jun 2022
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
Posts: 58
Threads: 20
Joined: Jan 2017
Dec-10-2023, 01:13 PM
(This post was last modified: Dec-10-2023, 01:13 PM by zinho.)
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
Posts: 6,794
Threads: 20
Joined: Feb 2020
Dec-10-2023, 01:39 PM
(This post was last modified: Dec-10-2023, 01:50 PM by deanhystad.)
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")
Posts: 1,093
Threads: 143
Joined: Jul 2017
Dec-10-2023, 09:14 PM
(This post was last modified: Dec-11-2023, 09:38 AM by Pedroski55.)
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
Posts: 58
Threads: 20
Joined: Jan 2017
Dec-11-2023, 05:14 PM
(This post was last modified: Dec-11-2023, 05:15 PM by zinho.)
Hi
Thank everyone, especially ale this #7 solve my question.
tks
|