我编写了这个程序,它会告诉你输入的两个倍数因子。防爆。如果我输入35(一个半英里),程序将打印5和7,这是两个加到35的素数。
但是我想知道是否有更简洁或pythonic的方法来迭代这个元组,所以我不必编写所有这些" elif"你在下面看到的陈述。
如果我不需要依赖任何外部库,那也很棒。
# 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]))
答案 0 :(得分:3)
我在评论中暗示了这一点,但意识到功能文档的链接可能还不够。
以下是使用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)
好多了,IMO:)
修改:使用itertools.combinations
的先前版本不会产生具有相同值的(x,y)对(例如,(x,y) = (2,2)
永远不会发生)。 combinations_with_replacement
允许重复。感谢@Copperfield指出这一点。
答案 1 :(得分:1)
虽然jedwards展示了最恐怖的方法 - 使用你将会了解和喜爱的itertools
库 - 这里是更多的经典"使用for循环的方法为您想要的模式。我提出来是因为作为一个编程初学者,重要的是要知道这个基本的,命令式的习语:
>>> 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])
答案 2 :(得分:0)
即使@ jedwards 解决方案很棒,(以及简洁/ pythonic );另一个可能的解决方案:
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.
示例输出:
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