Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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
Python 无法创建能够根据用户输入区分整数和浮点的列表_Python_List - Fatal编程技术网

Python 无法创建能够根据用户输入区分整数和浮点的列表

Python 无法创建能够根据用户输入区分整数和浮点的列表,python,list,Python,List,代码应该创建一个包含五个列表的列表,每个列表都是根据用户每次输入的数字(五个)创建的。例如,如果用户输入 1 2 3 4 5 1 2 1.5 2 2.2 1 输出应该是:[[1,2,3,4,5],[1,2],[1.5,2],[2,2],[1]] 感谢您的帮助。我知道要解决这个问题,您需要将字符串转换为int或float,但似乎无法使其工作 nlist = [] n = len(nlist) while n < 5: num = input("Enter a Number

代码应该创建一个包含五个列表的列表,每个列表都是根据用户每次输入的数字(五个)创建的。例如,如果用户输入

1 2 3 4 5

1 2

1.5 2

2.2

1
输出应该是:
[[1,2,3,4,5],[1,2],[1.5,2],[2,2],[1]]
感谢您的帮助。我知道要解决这个问题,您需要将字符串转换为int或float,但似乎无法使其工作

nlist = []
n = len(nlist)

while n < 5:
    num = input("Enter a Number: ").split()

    for x in num:

        if '.' in num[x]:
            num[x] = float(num[x])
        else:
            num[x] = int(num[x])

    nlist.append(num)    
    n = n + 1
print(nlist)
nlist=[]
n=长度(nlist)
当n<5时:
num=输入(“输入一个数字:”).split()
对于数量中的x:
如果编号[x]中的“.”:
num[x]=浮点(num[x])
其他:
num[x]=int(num[x])
nlist.append(num)
n=n+1
打印(nlist)

基本上,在您的代码中,您尝试在
for in
循环中访问列表
num
x
第th个元素,但是
x
不是列表中某个元素的索引,而是对该元素的引用

看看修改后的代码:

nlist = []
n = len(nlist)

while n < 5:
    num = input("Enter a Number: ").split()

    tmplist = []
    for x in num:

        if '.' in x:
            x = float(x)
        else:
            x = int(x)

        tmplist.append(x)

    nlist.append(tmplist)    
    n = n + 1
print(nlist)
它将输出:

[[1, 2, 3, 4, 5], [1, 2], [1.5, 2], [2.2], [1]]

此方法可以实现您所追求的效果,并且只需很少的行:

nList = []

for n in range(5):
    #Takes an input "5 3.5 5 2 3" converts it to [5, 3.5, 5, 2, 3]
    listItems = list(map(eval, input("Numbers for sub-list: ").split()))

    #Append the list to the nList
    nList.append(listItems)

print(nList)
因此,在“listItem”变量中,它不嵌套两个循环,而是将输入作为字符串:

"5 3.5 2"
拆分它:

input().split()

["5", "3.5", "2"]
然后将每个项目映射到“eval”,后者根据字符串选择浮点或整数:

list(map(eval, input().split()))

[5, 3.5, 2]
重要注意事项:“eval”可能是一个危险的命令,在进一步使用它之前,请务必广泛查阅它的文档。查看下面的评论以了解更多详细信息

或者 如果您不愿意使用EVE,请考虑这个解决方案:

nList = []

for n in range(5):
    itemList = input("Numbers to add: ").split()
    subList = []

    for item in itemList:


        if "." in item:
            item = float(item)
        else:
            item = int(item)

        subList.append(item)

    nList.append(subList)

print(nList)

使用
列表理解
str.find
查看是否转换为
float
int

>>> out = []
>>> for i in range(n): 
        inp = input().split() 
        out.append([ float(ele) if ele.find('.')>-1 else int(ele) for ele in inp])
#驱动程序值:

IN : n = 5
IN : 
1 2 3 4 5
1 2
1.5 2
2.2
1

OUT : [[1, 2, 3, 4, 5], [1, 2], [1.5, 2], [2.2], [1]]

[2,2]
可能应该是
[2.2]
在所需的输出中,我认为在num:中为x开始循环
的行是好的,但您似乎不理解这一点,即循环中每次
x
包含的内容。我建议您将这一行放在循环语句
print(“x='%s'%x)
之后,然后思考您看到的内容。这会让你想到“为什么我把num[x]放在那里?”。
eval
在我看来不应该被考虑在这样一个问题的解决方案中。如果你不想使用eval,只需将它们全部转换为浮点数,只要它不是程序的整数,有些是整数,有些是浮点数,让它们都浮动对性能没有影响。这不是性能问题,而是安全问题
eval
使用不可信的输入?我添加了一个不使用“eval”的替代解决方案,但您能解释一下使用它的问题吗?程序中没有任何错误检查,也没有任何人提供的答案,它不是为了安全,只是一个简单的脚本来完成工作-如果它因错误输入而崩溃,请使用
eval
方法重新启动它。用户可以调用模块范围内的方法,在for循环之前创建一个函数,如:
def infinite():而True:pass
,然后执行脚本并键入:
Numbers for sub-list:infinite()
。用户输入得到了一个无限循环,当然这只是一个愚蠢的例子,但是想象一下一个打印敏感数据的函数,用户可以通过利用
eval
轻松获得它。
IN : n = 5
IN : 
1 2 3 4 5
1 2
1.5 2
2.2
1

OUT : [[1, 2, 3, 4, 5], [1, 2], [1.5, 2], [2.2], [1]]