我要完成的代码的目的是接收给定库存的输入,并将它们作为一行列表返回。然后在第二行,重复列表,但这次加倍数字。
给定的输入是
Choc 5; Vani 10; Stra 7; Choc 3; Stra 4
所需输出为:
[['Choc', 5], ['Vani', 10], ['Stra', 7], ['Choc', 3], ['Stra', 4]]
[['Choc', 10], ['Vani', 20], ['Stra', 14], ['Choc', 6], ['Stra, 8]]
我已经成功地获得了第一条生产线所需的产量,但我正在为如何成功地与第二条生产线竞争而苦苦挣扎。
这是代码:
def process_input(lst):
result = []
for string in lines:
res = string.split()
result.append([res[0], int(res[1])])
return result
def duplicate_inventory(invent):
# your code here
return = []
return result
# DON’T modify the code below
string = input()
lines = []
while string != "END":
lines.append(string)
string = input()
inventory1 = process_input(lines)
inventory2 = duplicate_inventory(inventory1)
print(inventory1)
print(inventory2)
最佳答案
因为已经完成了第一行,所以可以使用简单的列表理解来获得第二行:
x = [[i, j*2] for i,j in x]
print(x)
输出:
[['Choc', 10], ['Vani', 20], ['Stra', 14], ['Choc', 6], ['Stra', 8]]