#Take an element from testnumbers
for test in testnumbers:
newlist = []
#Multiply it with each element of mylist, and append it to a list
for elem in mylist:
value = elem*test
newlist.append(value)
print(newlist)
#[1, 2, 3, 4]
#[5, 10, 15, 20]
#[10, 20, 30, 40]
为了更进一步,我们可以将所有这些列表添加到一个更大的列表中
results = []
for test in testnumbers:
newlist = []
for elem in mylist:
value = elem*test
newlist.append(value)
results.append(newlist)
print(results)
#[[1, 2, 3, 4], [5, 10, 15, 20], [10, 20, 30, 40]]
输出
我们从两个列表开始。你知道吗
现在我们希望
mylist
的每个元素都需要乘以testnumbers
中的元素,所以我们需要一个嵌套的for循环为了更进一步,我们可以将所有这些列表添加到一个更大的列表中
循环第二个列表中的每个元素,与第一个列表中的每个元素相乘以创建一个新列表。你知道吗
相关问题 更多 >
编程相关推荐