gpt4 book ai didi

python:如何将列表中的每个值与另一个列表中的所有值相乘

转载 作者:行者123 更新时间:2023-11-28 17:18:38 26 4
gpt4 key购买 nike

EDIT2:我删除了其余代码,以免破坏解决方案。感谢您的帮助。

编辑:我包含了我的全部代码。我不想包括它,因为我不想破坏任何人自己尝试这个问题的解决方案。但我需要帮助所以就在这里。

我通过执行以下操作手动完成此操作,但我希望它能在更大范围内工作,并且我拒绝多次手动复制和粘贴它。这不是编写代码的重点。

x = []
y = []

for i in range(len(x)-1):
products.append(x[0] * y[i])
products.append(x[1] * y[i])
products.append(x[2] * y[i])
products.append(x[3] * y[i])
products.append(x[4] * y[i])
products.append(x[5] * y[i])
products.append(x[6] * y[i])
products.append(x[7] * y[i])
products.append(x[8] * y[i])
products.append(x[9] * y[i])
products.append(x[10] * y[i])
products.append(x[11] * y[i])
products.append(x[12] * y[i])
products.append(x[13] * y[i])
products.append(x[14] * y[i])
products.append(x[15] * y[i])
products.append(x[16] * y[i])
products.append(x[17] * y[i])
products.append(x[18] * y[i])
products.append(x[19] * y[i])
products.append(x[20] * y[i])
products.append(x[21] * y[i])
products.append(x[22] * y[i])
products.append(x[23] * y[i])
products.append(x[24] * y[i])
products.append(x[25] * y[i])
products.append(x[26] * y[i])
products.append(x[27] * y[i])
products.append(x[28] * y[i])
products.append(x[29] * y[i])
products.append(x[30] * y[i])
products.append(x[31] * y[i])
products.append(x[32] * y[i])
products.append(x[33] * y[i])
products.append(x[34] * y[i])
products.append(x[35] * y[i])
products.append(x[36] * y[i])
products.append(x[37] * y[i])
products.append(x[38] * y[i])
products.append(x[39] * y[i])
products.append(x[40] * y[i])
products.append(x[41] * y[i])
products.append(x[42] * y[i])
products.append(x[43] * y[i])
products.append(x[44] * y[i])
products.append(x[45] * y[i])
products.append(x[46] * y[i])
products.append(x[47] * y[i])
products.append(x[48] * y[i])
products.append(x[49] * y[i])
products.append(x[50] * y[i])
products.append(x[51] * y[i])
products.append(x[52] * y[i])
products.append(x[53] * y[i])
products.append(x[54] * y[i])
products.append(x[55] * y[i])
products.append(x[56] * y[i])
products.append(x[57] * y[i])
products.append(x[58] * y[i])
products.append(x[59] * y[i])
products.append(x[60] * y[i])
products.append(x[61] * y[i])
products.append(x[62] * y[i])
products.append(x[63] * y[i])
products.append(x[64] * y[i])
products.append(x[65] * y[i])
products.append(x[66] * y[i])
products.append(x[67] * y[i])
products.append(x[68] * y[i])
products.append(x[69] * y[i])
products.append(x[70] * y[i])
products.append(x[71] * y[i])
products.append(x[72] * y[i])
products.append(x[73] * y[i])
products.append(x[74] * y[i])
products.append(x[75] * y[i])
products.append(x[76] * y[i])
products.append(x[77] * y[i])
products.append(x[78] * y[i])
products.append(x[79] * y[i])
products.append(x[80] * y[i])
products.append(x[81] * y[i])
products.append(x[82] * y[i])
products.append(x[83] * y[i])
products.append(x[84] * y[i])
products.append(x[85] * y[i])
products.append(x[86] * y[i])
products.append(x[87] * y[i])
products.append(x[88] * y[i])
products.append(x[89] * y[i])

我知道,如果您重复代码,那您就错了。我不知道如何让它工作。

我试过 x[i] * y[i] 但这只是从每个值中获取第一个值并将它们相乘。

我尝试在其中放置另一个 for 循环,但也没有用(结果相同)。

我尝试了 j = 0 的 while 循环,并让它每次都将 j 增加 1,但这也不起作用(相同的结果)。

请帮助我了解如何执行此操作。我知道这是可能的。我已经在 python 中练习了一个星期所以请让我休息一下,我知道这是一个愚蠢的问题。

最佳答案

你需要使用嵌套循环

x = []
y = []
products = []
palindromes = []

for i in range(10, 100):
x.append(i)
# print(x)

for i in range(10, 100):
y.append(i)
# print(y)

for i in range(len(y)):
for j in range(len(x)):
products.append(x[j] * y[i])


for i in range((len(products)-1)):
a = str(products[i])
b = tuple(a)
c = b[::-1]
if b == c:
print('palindrome found!', a)
print('b value:', b)
print('c value:', c)
palindromes.append(a)

solutions = list(map(int, palindromes))
solutions.sort()
print(solutions)

这称为嵌套循环。变量 i 迭代长度为 y 减 1,变量 j 迭代长度为 x 减 1。

关于python:如何将列表中的每个值与另一个列表中的所有值相乘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42713158/

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