我有一个编程任务,但我无法弄清楚如何解决这个输出问题。
这是我的输出:
[['Choc 5\t'], ['Vani 10'], ['Stra 7'], ['Choc 3 '], ['Stra 4']]
但它应该是这样的:
['Choc', 5], ['Vani', 10], ['Stra', 7], ['Choc', 3], ['Stra', 4]]
代码:
def process_input(lst):
result = list(map(lambda x: [x[0:7]], lst))
return result
string = input()
lines = []
while string != "END":
lines.append(string)
string = input()
print(process_input(lines))
答案 0 :(得分:0)
output = [['Choc 5\t'], ['Vani 10'], ['Stra 7'], ['Choc 3 '], ['Stra 4']]
print(output)
output = [[s[0].strip().split(' ')[0], int(s[0].strip().split(' ')[1])] for s in a]
print(output)
输出
[['Choc 5\t'], ['Vani 10'], ['Stra 7'], ['Choc 3 '], ['Stra 4']]
[['Choc', 5], ['Vani', 10], ['Stra', 7], ['Choc', 3], ['Stra', 4]]
答案 1 :(得分:0)
您可以尝试这种方法:
data=[['Choc 5\t'], ['Vani 10'], ['Stra 7'], ['Choc 3 '], ['Stra 4']]
print(list(map(lambda x:[x[0].split()[0] , int(x[0].split()[1])],data)))
输出:
[['Choc', 5], ['Vani', 10], ['Stra', 7], ['Choc', 3], ['Stra', 4]]