我有两个列表,我正在尝试根据数字出现的次数来创建数字的多个副本。
numbers = [0, 1, 2]
amount = [1, 2, 3]
我尝试过:
total = []
n = 0
for i in range(len(numbers)):
product = numbers[n] * amount[n]
n += 1
total.extend(product)
但是我得到了错误:
TypeError: 'int' object is not iterable.
我的预期输出是:
total = [0, 1, 1, 2, 2, 2]
答案 0 :(得分:3)
使用zip
// thread 1
a->name = "Bar";
// thread 2
Dog* d = static_cast<Dog*>(a);
result = []
for i, j in zip(numbers, amount):
result.extend([i] * j)
print(result)
答案 1 :(得分:2)
您的错误在行内:
product = numbers[n] * amount[n]
它不会产生列表,而是一个整数。因为您要将两个数字相乘。
您真正想要的是
product = [numbers[n]] * amount[n]