Reputation: 175
I'm new to python and I'm trying to figure out how to access and use integers stored in lists of tuple pairs (a, b), such that I can divide a by b and if it meets a criteria, I append the tuple to a new list and I count the tuple. I would like to do this using only basic functions and for-loops.
I borrowed some code from another stackoverflow question that I used to create the list of tuples from two separate integer lists of different sizes as such:
list_a = list(range(10, 51))
list_b = list(range(1, 21))
new_tuple_list = []
new_tuple_count = 0
for i, a in enumerate(list_a):
new_tuple_list.append((a, list_b[i % len(list_b)]))
divisors_count += 1
print ("New tuple count: ", new_tuple_count)
print (new_tuple_list)
This gives me: New tuple count: 41
[(10, 1), (11, 2), (12, 3), (13, 4), (14, 5), (15, 6), (16, 7), (17, 8), (18, 9), (19, 10), (20, 11), (21, 12), (22, 13), (23, 14), (24, 15), (25, 16), (26, 17), (27, 18), (28, 19), (29, 20), (30, 1), (31, 2), (32, 3), (33, 4), (34, 5), (35, 6), (36, 7), (37, 8), (38, 9), (39, 10), (40, 11), (41, 12), (42, 13), (43, 14), (44, 15), (45, 16), (46, 17), (47, 18), (48, 19), (49, 20), (50, 1)]
But I want to know if I divide 10 by 1 from (10, 1), 11 by 2, etc., will I get an integer, and if so, I want to add that to a new list and count the number of tuple pairs this is true for. I have tried this:
tuple_test = [(10,1), (11,2)]
def find_divisors (x):
NUM_tuples = []
tuples_count = 0
for x[0] in pairs:
for x[1] in pairs:
if x[0] / x[1] % 2 == 0:
NUM_tuples.append(pairs)
tuples_count += 1
return (x[0] / x[1] % 2)
return NUM_tuples
return tuples_count
find_divisors(tuple_test)
I have also tried something like this:
def divisors(list_a, list_b):
test_int = 0
new_divisors = []
for a in list_a:
for b in list_b:
if a/b % 2 == 0:
test_int += 1
new_divisors += (a,b)
return new_divisors
return test_int
NUM_tuples = []
tuples_count = 0
for i, c in enumerate(list_a):
NUM_tuples.append((c, list_b[i % len(list_b)]))
tuples_count += 1
return tuples_count
return NUM_tuples
divisors(list_a, list_b)
any help would be appreciated!
Upvotes: 1
Views: 193
Reputation: 5384
I would also like to add that you can reduce your code to get the original list of tuples to the following using itertools.cycle
.
from itertools import cycle
# a and b don't have to be a list unless you need them as a list elsewhere.
a = range(10, 51)
b = range(1, 21)
# cycle the shorter one
tuple_lst = list(zip(a, cycle(b)))
print("New tuple count:", len(tuple_lst))
print(tuple_lst)
Upvotes: 0
Reputation: 1859
Try the following :
a = range(1,20)
b = range(1,5)
c = [(x,y) for x in a for y in b if x%y==0]
print(c)
It gives the following output
[(1, 1), (2, 1), (2, 2), (3, 1), (3, 3), (4, 1), (4, 2), (4, 4), (5, 1), (6, 1), (6, 2), (6, 3), (7, 1), (8, 1), (8, 2), (8, 4), (9, 1), (9, 3), (10, 1), (10, 2), (11, 1), (12, 1), (12, 2), (12, 3), (12, 4), (13, 1), (14, 1), (14, 2), (15, 1), (15, 3), (16, 1), (16, 2), (16, 4), (17, 1), (18, 1), (18, 2), (18, 3), (19, 1)]
You can easily adapt this for your program I think !!
PS : The length of c
above will give you the tuple count.
Upvotes: 3