我尝试将list作为元素输入,但预期结果不是包含元素列表的列表。以下代码是否需要修改或是否不正确?
例如,[1,2,3]
[3,4,5]
,但结果与[[1,2,3] ,[3,4,5]]
不一样
s = raw_input()
numbers = map(list , s.split())
print number
答案 0 :(得分:0)
Your objective is to store the input lists into a list (say 'l'). Iterate over input string, store the string letters between '[' and ']' into a temp list (say list1) which can be tracked using simple if-condition, and append the collected lists (list1 into the main list ('l'):
l = list()
s = input("input plz")
for a in s:
if a =='[':
list1 = list()
elif a ==']':
l.append(list1)
elif a !=',' and a!=' ':
list1.append(int(a))
print(l)
答案 1 :(得分:0)
s.split itself return list Try this
s = raw_input()
numbers = s.split()
print numbers
output
['2', '5', '6']
For multidimensional list
li=list()
s = raw_input()
while s!="Stop":
li.append(s.split())
s = raw_input()
print li
Output
[['2', '3', '5', '4'], ['2', '5', '63', '6'], ['2', '3', '56', '2']]
答案 2 :(得分:0)
这可能是一种可能的解决方案。
s1 = []
ask = raw_input("Yes/No: ")
while ask == "Yes":
s2 = raw_input()
lst_one = s2.split()
s1.append(s2)
print s1
ask = raw_input("Yes/No: ")