smi*_*y23 4 python list python-3.x
我要求完成的代码的目的是接收给定库存的输入,将它们返回到一行中的列表中.然后在第二行,复制列表,但这次加倍数字.
给定的输入是
Choc 5; Vani 10; Stra 7; Choc 3; Stra 4
Run Code Online (Sandbox Code Playgroud)
所需的输出是:
[['Choc', 5], ['Vani', 10], ['Stra', 7], ['Choc', 3], ['Stra', 4]]
[['Choc', 10], ['Vani', 20], ['Stra', 14], ['Choc', 6], ['Stra, 8]]
Run Code Online (Sandbox Code Playgroud)
我已经成功地获得了第一线的所需输出,但我正在努力争取如何成功地竞争第二线.
这是代码:
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)
Run Code Online (Sandbox Code Playgroud)
由于您已经完成了第一行,因此可以使用简单的列表推导来获取第二行:
x = [[i, j*2] for i,j in x]
print(x)
Run Code Online (Sandbox Code Playgroud)
输出:
[['Choc', 10], ['Vani', 20], ['Stra', 14], ['Choc', 6], ['Stra', 8]]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
100 次 |
| 最近记录: |