Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python3中,如何将列表中的混合数据类型作为用户输入?_Python - Fatal编程技术网

在Python3中,如何将列表中的混合数据类型作为用户输入?

在Python3中,如何将列表中的混合数据类型作为用户输入?,python,Python,然后 我如何获得这样的输出 >>> hello 1 5 ['h','e','l','l','o',' ','1',' ','5'] 您可以使用split将其拆分为单词,然后使用isdigit并强制转换为整数的任何子字符串 a = ['hello', 1, 5] 但对于浮点数或负数,效果不太好;)@深空-真的,但我相信一旦OP给了这个机会,他们会考虑更新这个社区维基答案;下面的答案很有帮助,但我认为重要的是,您必须了解,input()总是会为您返回一个字符串,并且将字符串转换

然后

我如何获得这样的输出

>>> hello 1 5
['h','e','l','l','o',' ','1',' ','5']

您可以使用
split
将其拆分为单词,然后使用
isdigit
并强制转换为整数的任何子字符串

a = ['hello', 1, 5]

但对于浮点数或负数,效果不太好;)@深空-真的,但我相信一旦OP给了这个机会,他们会考虑更新这个社区维基答案;下面的答案很有帮助,但我认为重要的是,您必须了解,
input()
总是会为您返回一个字符串,并且将字符串转换为列表将为您提供一个与原始问题类似的字符数组。python并没有自动的方法来计算输入中的数据类型,您需要编写一些代码来实现这一点。
a = ['hello', 1, 5]
[int(x) if x.isdigit() else x for x in a.split()]