gpt4 book ai didi

python - 浏览列表,每当出现特定类别的号码时,创建一个其他类别的最新号码列表

转载 作者:行者123 更新时间:2023-12-04 08:24:43 27 4
gpt4 key购买 nike

假设我有一个随机数列表
随机列表 = [9, 8, 18, 1, 17, 3, 13, 4, 13, 1, 9, 10, 7, 13, 3, 9, 13, 10, 18, 10, 19, 3, 14, 14 , 19, 4, 20, 17, 8, 17, 3, 12, 8, 12, 1, 2, 15, 13, 2, 8, 18, 10, 14, 11, 17, 11, 2, 7, 4 , 7, 5, 5, 18, 7, 11, 13, 20, 9, 2, 14, 19, 1, 16, 10, 16, 19, 13, 19, 11, 17, 8, 2, 10, 16 , 5, 14, 7, 11, 17, 9, 9, 6, 12, 6, 12, 4, 14, 10, 2, 6, 9, 1, 14, 4, 14, 13, 18, 13, 6 , 8]
数字分为三类:
可被 2 整除的数字 (2,4,6,8,10,12,14,16,18,20)
除二外的素数 (3,7,11,13,17,19)
休息 (1,5,9,15)
现在我想做的是以下内容:
我将遍历列表,每当出现多个其余类别时,我都想创建一个像这样的列表 [%2, prime] 而元素是列表中最近出现的元素。目标是拥有一个包含两个元素的列表列表。我希望它清楚我想要做什么。这是我的代码:

randomlist = [9, 8, 18, 1, 17, 3, 13, 4, 13, 1, 9, 10, 7, 13, 3, 9, 13, 10, 18, 10, 19, 3, 14, 14, 19, 4, 20, 17, 8, 17, 3, 12, 8, 12, 1, 2, 15, 13, 2, 8, 18, 10, 14, 11, 17, 11, 2, 7, 4, 7, 5, 5, 18, 7, 11, 13, 20, 9, 2, 14, 19, 1, 16, 10, 16, 19, 13, 19, 11, 17, 8, 2, 10, 16, 5, 14, 7, 11, 17, 9, 9, 6, 12, 6, 12, 4, 14, 10, 2, 6, 9, 1, 14, 4, 14, 13, 18, 13, 6, 8]


def check_prime(x):

for i in range(2, x):
if (x % i) == 0:
return False
else:
return True

def check_number(x):

if x%2 == 0:
return "zweier"

elif check_prime(x) == True:
return "prim"

else:
return "rest"

final_list = []
partial_list = [0,0]

for x in randomlist:

if check_number(x) =="zweier":
partial_list[0] = x

elif check_number(x) == "rest":
partial_list[1] = x

else:
final_list.append(partial_list)

for x in final_list:
print(x)
输出:
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
[8, 9]
让我非常困惑的是,如果我在创建 final_list 成员后立即打印它们,它会显示正确的解决方案:
randomlist = [9, 8, 18, 1, 17, 3, 13, 4, 13, 1, 9, 10, 7, 13, 3, 9, 13, 10, 18, 10, 19, 3, 14, 14, 19, 4, 20, 17, 8, 17, 3, 12, 8, 12, 1, 2, 15, 13, 2, 8, 18, 10, 14, 11, 17, 11, 2, 7, 4, 7, 5, 5, 18, 7, 11, 13, 20, 9, 2, 14, 19, 1, 16, 10, 16, 19, 13, 19, 11, 17, 8, 2, 10, 16, 5, 14, 7, 11, 17, 9, 9, 6, 12, 6, 12, 4, 14, 10, 2, 6, 9, 1, 14, 4, 14, 13, 18, 13, 6, 8]


def check_prime(x):
for i in range(2, x):
if (x % i) == 0:
return False
else:
return True

def check_number(x):

if x%2 == 0:
return "zweier"

elif check_prime(x) == True:
return "prim"

else:
return "rest"

final_list = []
partial_list = [0,0]

for x in randomlist:

if check_number(x) =="zweier":
partial_list[0] = x

elif check_number(x) == "rest":
partial_list[1] = x

else:
final_list.append(partial_list)
print(final_list[-1])
输出:
[18, 9]
[18, 9]
[18, 9]
[18, 9]
[4, 9]
[4, 9]
[10, 9]
[10, 9]
[10, 9]
[10, 9]
[10, 9]
[10, 9]
[14, 9]
[20, 9]
[8, 9]
[8, 9]
[12, 9]
[2, 15]
[14, 15]
[14, 15]
[14, 15]
[2, 15]
[4, 15]
[4, 15]
[4, 15]
[18, 15]
[18, 15]
[18, 15]
[14, 9]
[14, 9]
[16, 9]
[16, 9]
[16, 9]
[16, 9]
[16, 9]
[16, 9]
[14, 9]
[14, 9]
[14, 9]
[6, 9]
[14, 9]
[18, 9]
所以看起来基本的想法是好的。我尝试了很多,但我无法找出我做错了什么。非常感谢您的帮助!

最佳答案

这会给你想要的结果。引用 here这解释了正在发生的事情。

randomlist = [9, 8, 18, 1, 17, 3, 13, 4, 13, 1, 9, 10, 7, 13, 3, 9, 13, 10, 18, 10, 19, 3, 14, 14, 19, 4, 20, 17, 8, 17, 3, 12, 8, 12, 1, 2, 15, 13, 2, 8, 18, 10, 14, 11, 17, 11, 2, 7, 4, 7, 5, 5, 18, 7, 11, 13, 20, 9, 2, 14, 19, 1, 16, 10, 16, 19, 13, 19, 11, 17, 8, 2, 10, 16, 5, 14, 7, 11, 17, 9, 9, 6, 12, 6, 12, 4, 14, 10, 2, 6, 9, 1, 14, 4, 14, 13, 18, 13, 6, 8]


def check_prime(x):
for i in range(2, x):
if (x % i) == 0:
return False
else:
return True

def check_number(x):
if x%2 == 0:
return "zweier"
elif check_prime(x) == True:
return "prim"
else:
return "rest"

final_list = []
partial_list = [0,0]

for x in randomlist:
if check_number(x) =="zweier":
partial_list[0] = x
elif check_number(x) == "rest":
partial_list[1] = x
else:
final_list.append(partial_list.copy())

for x in final_list:
print(x)

关于python - 浏览列表,每当出现特定类别的号码时,创建一个其他类别的最新号码列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65325021/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com