Python-将用户输入转换为列表 [英] Python- Turning user input into a list

查看:528
本文介绍了Python-将用户输入转换为列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法要求用户输入并将他们的输入转换为列表,元组或字符串呢?我希望将一系列数字插入矩阵中.我可以告诉他们在控制台中键入所有数字,不带空格并进行遍历,但是还有其他方法可以做到这一点吗?

Is there a way to ask for user input and turn their input into a list, tuple, or string for that matter? I want a series of numbers to insert into a matrix. I could tell them to type all the numbers into the console with no spaces and iterate through them but are there any other ways to do this?

推荐答案

您可以简单地执行以下操作:

You can simply do as follows:

user_input = input("Please provide list of numbers separated by comma, e.g. 1,2,3: ")

a_list =  list(map(float,user_input.split(',')))
print(a_list)
# example result: [1, 2, 3]

这篇关于Python-将用户输入转换为列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆