如何在Python中的多维列表中接受用户输入字符串?

时间:2017-08-19 09:31:58

标签: python

我想要的是接受用户输入

Hello I am Ram
I am from pokhara
I need to go home

我的名单应该是:

a[[Hello I am Ram],[I am from pokhara],[I need to go home]]

我绑的是

a = []
for x in range(4):
    a[i] = ([let for let in input('')])

如同 How to accept user input in multi dimension list in Python?

3 个答案:

答案 0 :(得分:0)

使用python3命令运行。

a = []
for x in range(4):
    a.append([input('')])
print (a)

长度:

a = []
for x in range(4):
    l = input('')
    a.append(l)
    print (len(l))

repetittion:

d = {}
for n in range(1, 4):
    v = input('Enter line {}'.format(n))
    if v in d:
        d[v] = d[v] + 1
    else:
        d[v] = 1
print (d)

答案 1 :(得分:0)

您的输入似乎是:

>>> input_string = 'Hello I am Ram\nI am from pokhara\nI need to go home'

因此,您可以使用列表理解来获得结果:

>>> a = [[s] for s in input_string.split('\n')]
>>> print(a)
    [['Hello I am Ram'], ['I am from pokhara'], ['I need to go home']]

答案 2 :(得分:0)

input_list = []

while(1):

    u_input = input()

    if not u_input:

        break

    input_list.append([u_input])

print input_list

这将接受输入,除非你没有提供任何类型的东西并输入