简体   繁体   English

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

[英]How to accept user input string in multi dimension list in Python?

Here what I want is to accept user inputs they could be 我想要的是接受他们可能是的用户输入

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

And my list should be: 我的清单应该是:

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

What I tied is 我绑的是

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

as in How to accept user input in multi dimension list in Python? 如何在Python的多维列表中接受用户输入?

Use python3 command to run. 使用python3命令运行。

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

Length: 长度:

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)

Your input seems to be: 您的输入似乎是:

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

Hence you can you can use list comprehension to get your result: 因此,您可以使用列表推导来获得结果:

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

while(1):

    u_input = input()

    if not u_input:

        break

    input_list.append([u_input])

print input_list

This will accept input unless you dont give an type anything and give enter 这将接受输入,除非您不输入任何类型并输入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM