python - 如何提取深层嵌套元组的元素并将它们放在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:])
解决方案
您可以很容易地将您的函数调整为生成器:
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
如有必要。
推荐阅读
- java - 将用户角色存储到一个表字段或具有角色的单独表中
- firebase - 构建 Flutter 项目时 Dexing 出错
- python - 如何编写对 CSV 的 boto3 响应?
- javascript - 在对象数组中通过公共键合并项目
- c# - HighQualityBicubic 插值在调整大小时添加黑色背景
- android - 在 android 主题中重用样式
- python - Pycharm:DLL加载失败:找不到指定的程序
- parallel-processing - MPI 仅在部分代码上工作,而其余部分是单个进程
- python - rand.randrange 返回“builtin_function_or_method”对象没有属性“randrange”
- r - 将熔体数据设为一个