在python中将两组数字相乘 [英] Multiplying two sets of numbers in python
本文介绍了在python中将两组数字相乘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个数字列表,例如[1, 2, 3, 4, 5]
和[7, 8, 9, 10, 11]
,我想形成一个新列表,该列表由第一个列表中每个成员与第二个列表中每个成员的乘积组成.在这种情况下,新列表中将有5 * 5 = 25个元素.
I have two lists of numbers, say [1, 2, 3, 4, 5]
and [7, 8, 9, 10, 11]
, and I would like to form a new list which consists of the products of each member in the first list with each member in the second list. In this case, there would be 5*5 = 25 elements in the new list.
到目前为止,我一直无法通过while()循环执行此操作. 这是我到目前为止的内容:
I have been unable to do this so far with a while() loop. This is what I have so far:
x = 0
y = 99
results = []
while x < 5:
x = x + 1
results.append(x*y)
while y < 11:
y = y + 1
results.append(x*y)
推荐答案
您不尝试使用已知的旧方法吗?
Wht dont you try with known old ways;
list1 = range(1, 100)
list2 = range(10, 50, 5)
new_values = []
for x in list1:
for y in list2:
new_values.append(x*y)
这篇关于在python中将两组数字相乘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文