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练习了一个星期,所以请给我休息一下,我知道这是一个愚蠢的问题。
答案 0 :(得分:2)
您需要使用嵌套循环
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的长度。
答案 1 :(得分:1)
没有必要这样做
for i in range(10, 100):
x.append(i)
您可以从range
对象创建一个列表,如下所示:
x = list(range(10, 100))
但是,没有必要制作x
和y
列表。您可以通过直接遍历范围来创建产品列表。例如:
products = []
for x in range(10, 100):
for y in range(10, 100):
products.append(x * y)
制作一个只含有回文的清单:
def is_palindrome(n):
s = str(n)
return s == s[::-1]
products = []
for x in range(10, 100):
for y in range(10, 100):
n = x * y
if is_palindrome(n):
products.append(n)
然后,您可以max(products)
找到最高的回文,它是两位两位数的乘积。
这是一种使用生成器表达式的更紧凑的方式,因此它不需要构建列表:
r = range(10, 100)
print(max(filter(is_palindrome, (x * y for x in r for y in r))))
答案 2 :(得分:0)
你的for循环等同于:
products=[i*j for i in x[:-1] for j in y]
答案 3 :(得分:-1)
您可以使用Kaushal Kumar Singh的解决方案,或者这是另外两种解决方案。然而,在编程中没有通用的解决方案,许多解决方案可以共存。
代码1:
products = [i * j for i, j in zip(x, y)]
代码2:
from operator import mul
products = list(map(mul, x, y))