Best way to do list / tuple calculations in Python 3x

I wrote this program that will tell you two multiple factors of your input. Ex. if I were to enter 35 (half-primary), the program printed 5 and 7, which are two primes that are multiplied by 35.

But I am wondering if there is a more concise or pythonic way of iterating through this tuple, so I won’t need to code all of these “elif” statements that you see below.

It would also be great if I did not have to rely on any external libraries.

# multiples of semiprimes 4 - 49
tuple1 = ( 2, 3, 5, 7 )

# tuple 1 calculations
while True:

        try:
                semiprime = int(input('Enter Semiprime: '))

        except ValueError:
                print('INPUT MUST BE AN INTEGER')
                continue

        # index 0 - 3
        if (tuple1[0]) * (tuple1[0]) == semiprime:
                print((tuple1[0]), (tuple1[0]))

        elif (tuple1[0]) * (tuple1[1]) == semiprime:
                print((tuple1[0]), (tuple1[1]))

        elif (tuple1[0]) * (tuple1[2]) == semiprime:
                print((tuple1[0]), (tuple1[2]))

        elif (tuple1[0]) * (tuple1[3]) == semiprime:
                print((tuple1[0]), (tuple1[3]))

        # index 1 - 3
        elif (tuple1[1]) * (tuple1[0]) == semiprime:
                print((tuple1[1]), (tuple1[0]))

        elif (tuple1[1]) * (tuple1[1]) == semiprime:
                print((tuple1[1]), (tuple1[1]))

        elif (tuple1[1]) * (tuple1[2]) == semiprime:
                print((tuple1[1]), (tuple1[2]))

        elif (tuple1[1]) * (tuple1[3]) == semiprime:
                print((tuple1[1]), (tuple1[3]))

        # index 2 - 3
        elif (tuple1[2]) * (tuple1[0]) == semiprime:
                print((tuple1[2]), (tuple1[0]))

        elif (tuple1[2]) * (tuple1[1]) == semiprime:
                print((tuple1[2]), (tuple1[1]))

        elif (tuple1[2]) * (tuple1[2]) == semiprime:
                print((tuple1[2]), (tuple1[2]))

        elif (tuple1[2]) * (tuple1[3]) == semiprime:
                print((tuple1[2]), (tuple1[3]))

        #index 3 - 3
        elif (tuple1[3]) * (tuple1[0]) == semiprime:
                print((tuple1[3]), (tuple1[0]))

        elif (tuple1[3]) * (tuple1[1]) == semiprime:
                print((tuple1[3]), (tuple1[1]))

        elif (tuple1[3]) * (tuple1[2]) == semiprime:
                print((tuple1[3]), (tuple1[2]))
+4
source share
3 answers

, , docs .

, itertools.combinations_with_replacement:

from itertools import combinations_with_replacement

# multiples of semiprimes 4 - 49
tuple1 = ( 2, 3, 5, 7 )

# tuple 1 calculations
while True:

    try:
        semiprime = int(input('Enter Semiprime: '))

    except ValueError:
        print('INPUT MUST BE AN INTEGER')
        continue

    for (x,y) in combinations_with_replacement(tuple1, 2):
        if x * y == semiprime:
            print(x,y)

, :)

: itertools.combinations, (x, y) (, (x,y) = (2,2) ). combinations_with_replacement . @Copperfield .

+3

jedwards Pythonic - itertools, , - "" , for-loops , . , , , , :

>>> tuple1 = (2,3,5,7)
>>> for i in range(len(tuple1)):
...   for j in range(i+1, len(tuple1)):
...     print(tuple1[i], tuple1[j])
... 
2 3
2 5
2 7
3 5
3 7
5 7
>>> 

, :

for i in range(len(tuple1)):
    for j in range(i+1, len(tuple1)):
        if tuple1[i] * tuple1[j] == semiprime
            print(tuple1[i], tuple1[j])
+1

Even though the @ jedwards solution is great, (as well as a short / pythonic); another possible solution:

def prime_multiples(l,t ):  
    for i in l:  # Iterate over our list.
        for j in t:  # Iterate over the tuple of prime factors.
            #  We check to see that we can divide without a remainder with our factor,
            #  then check to see if that factor exists in our tuple.
            if i%j == 0 and i/j in t:
                print "Prime factors: {} * {} = {}".format(j, i/j, i)
                break  # We could go not break to print out more options.

Output Example:

l = [4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, 39, 46, 49]
t = ( 2, 3, 5, 7 )
prime_multiples(l, t)
>>> Prime factors: 2 * 2 = 4
... Prime factors: 2 * 3 = 6
... Prime factors: 3 * 3 = 9
... Prime factors: 2 * 5 = 10
... Prime factors: 2 * 7 = 14
... Prime factors: 3 * 5 = 15
... Prime factors: 3 * 7 = 21
... Prime factors: 5 * 5 = 25
... Prime factors: 5 * 7 = 35
... Prime factors: 7 * 7 = 49
0
source

Source: https://habr.com/ru/post/1649978/


All Articles