Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python 3x中对列表/元组执行计算的最佳方法_Python_Python 3.x_Tuples - Fatal编程技术网

在Python 3x中对列表/元组执行计算的最佳方法

在Python 3x中对列表/元组执行计算的最佳方法,python,python-3.x,tuples,Python,Python 3.x,Tuples,我写了这个程序,它会告诉你输入的两个倍数。如果我输入35(一个半素数),程序将打印5和7,这两个素数相乘为35 但我想知道是否有一种更简洁或更通俗的方法来迭代这个元组,这样我就不必编写下面所有的“elif”语句了 如果我不需要依赖任何外部库,那也太好了 # multiples of semiprimes 4 - 49 tuple1 = ( 2, 3, 5, 7 ) # tuple 1 calculations while True: try:

我写了这个程序,它会告诉你输入的两个倍数。如果我输入35(一个半素数),程序将打印5和7,这两个素数相乘为35

但我想知道是否有一种更简洁或更通俗的方法来迭代这个元组,这样我就不必编写下面所有的“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]))

我在评论中暗示了这一点,但意识到仅仅链接到功能文档可能是不够的

以下是如何使用以下方法编写代码:

好多了,依我看:)


编辑:以前使用的版本不会产生具有相同值的(x,y)对(例如,
(x,y)=(2,2)
)<代码>组合与替换允许重复。感谢@Copperfield指出这一点。

而jedwards展示了最具python风格的方法-使用您将逐渐了解和喜爱的
itertools
库-以下是更为“经典”的方法,用于您想要的模式的循环。我之所以提出这个问题,是因为作为一名编程初学者,了解这个基本的命令式习惯用法非常重要:

>>> 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])

尽管@jedwards解决方案非常好(以及简洁/通灵);另一种可能的解决办法:

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

@犹太矮人的方式就是智慧!嵌套的for循环似乎也可以。您可以计算给定数字的素数分解,而不必局限于预先计算的素数列表,使用素数检查函数或素数生成函数,如埃拉托斯特内斯的筛子在这种情况下,我认为
组合_与_替换
是正确的,因为OP也检查相同的元素position@Copperfield你说得对,他们确实在编辑它。@jedwards好多了,谢谢!但是在for循环中,函数参数中的“2”是什么?@miro.kh在这种情况下,
compositions\u with\u replacement
2
的第二个参数是从中得到的每组结果的期望大小,其中2个是
(2,2),(2,3),(2,5),(2,7),(3,3),…
,其中3个是
(2,2,2,3),(2,2,5),(2,2,7),(2,3,3),…
etc@miro.kh要生成的元素组合的长度/数量。我不讨厌嵌套循环,但我认为在元组上迭代(例如,对于tuple1中的e1:tuple1中的e2:…)可能是错误的better@jedwards但你会得到产品,而不是每一个独特的组合。您可以通过使用切片(或者更好的是,
islice
)和对元组进行迭代来避免这种情况,但我不想提出这个问题,因为不管怎样,模式才是最重要的。
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