首页 > 解决方案 > 如何提取深层嵌套元组的元素并将它们放在python中的列表中?

问题描述

我一直在玩 Python,并编写了以下代码来查找给定范围内的所有素数,如下所示:

def get_primes(x):
    primes = []

    def is_prime(x):
        if x == 0:
            return
        else:
            for i in range(2, int(x)):
                if (x % i) == 0:
                    return is_prime(x - 1)
            else:
                return x, is_prime(x - 1)

    primes.append(is_prime(x))
    return primes


print(get_primes(int(input("Enter the range: 0 - "))))

输出为:(例如输入 100)

Enter the range: 0 - 100
[(97, (89, (83, (79, (73, (71, (67, (61, (59, (53, (47, (43, (41, (37, (31, (29, (23, (19, (17, (13, (11, (7, (5, (3, (2, (1, None))))))))))))))))))))))))))]

看起来很丑。所以,我需要一种方法来展平递归元组结构:

[97, 89, 83, 79, 73, 71, 67, 61, 59, 53, 47, 43, 41, 37, 31, 29, 23, 19, 17, 13, 11, 7, 5, 3, 2, 1]

我使用以下代码这样做:

x = get_primes(100)
arr = []
arr.append(x[0][0])
arr.append(x[0][1][0])
arr.append(x[0][1][1][0])
arr.append(x[0][1][1][1][0])
arr.append(x[0][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
print(arr)

但是,当然,这不是一种专业的方法。

所以,我想知道如何制作这个: [97, 89, 83, 79, 73, 71, 67, 61, 59, 53, 47, 43, 41, 37, 31, 29, 23, 19, 17, 13, 11, 7, 5, 3, 2, 1]

从这个: [(97, (89, (83, (79, (73, (71, (67, (61, (59, (53, (47, (43, (41, (37, (31, (29, (23, (19, (17, (13, (11, (7, (5, (3, (2, (1, None))))))))))))))))))))))))))]

我在这里找到了答案How to flatten a tuple in python但是那里的代码是针对 python 2 的,所以我对其进行了一些修改。

并使用了这段代码:

def flatten(T):
    if type(T) is not tuple:
        return (T,)
    elif len(T) == 0:
        return ()
    else:
        return flatten(T[0]) + flatten(T[1:])

标签: pythontuples

解决方案


您可以很容易地将您的函数调整为生成器:

def is_prime(x):
    if x != 0:
        for i in range(2, int(x)):
            if (x % i) == 0:
                yield from is_prime(x - 1)
                return
        else:
            yield x
            yield from is_prime(x - 1)

然后要使所有素数达到 100,您可以将该生成器传递给以list获取包含该生成器的值的列表:

print(list(is_prime(100)))
# [97, 89, 83, 79, 73, 71, 67, 61, 59, 53, 47, 43, 41, 37, 31, 29, 23, 19, 17, 13, 11, 7, 5, 3, 2, 1]

递归并不是解决这个问题的好方法,我建议查找Eratosthenes 的筛子以获得更好的方法。

yield from语法仅在较新版本的 Python (>= 3.3) 中可用。你可以更换

yield from x

for y in x:
    yield y

如有必要。


推荐阅读