繁体   English   中英

在转换为列表时,如何防止Python将两位数的整数拆分为两个单个整数

[英]How to keep Python from splitting a double digit integer into two single integers, when converting to a list

我的任务是编写一个脚本,该脚本让用户输入一个数字列表,并让该脚本将列表中的每个数字相加以获得总和。 我能够编写脚本,但是当我输入一个大于一位的整数时遇到了麻烦。 这是我的代码和工作脚本:

#!/usr/bin/python

#Sum of integers in a list

#Takes input and removes non-number characters

nList = input("please enter a list of integers separated by commas:\n")
n = ""
numList1 = list(n.join((i) for i in nList if i.isalnum())) #Converts into list of Strings
numList = list(map(int, numList1))#Converts list of strings into a list of integers
print(numList) #prints the numList

#Takes list and adds all the numbers together

def sumArg(numList):
    nSum= map(int, numList) #converts list into individual integers
    numSum = sum(i for i in nSum) #adds the integers together
    print(numSum)#prints the sum

sumArg(numList) #runs script

当我运行脚本并输入(1,2,3,4,5,6,7,8,9)时,它会打印出来

[1, 2, 3, 4, 5, 6, 7, 8, 9]
45

但是,当我将数字10添加到列表的末尾时,我将其作为输出接收

[1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 0]
46

我知道数字1到10的总和等于55,而不是46。有没有办法让我编写此脚本而无需将Python的10变成1、0。我希望能够让用户输入任何类型的整数并打印出正确的总和。

根本不应该将逗号分隔的字符串转换为整数。 尤其是您的算法会通过删除所有逗号来开始该过程,这使得无法从下一个数字中分辨出一个数字。

而是使用str.split()将字符串简单地拆分为给定分隔符上的list

nList = input("please enter a list of integers separated by commas:\n")
nList = map(int, nList.split(','))

此外,您可以使用内置的sum()函数找到此map对象的总和(无需将其变成list ):

total = sum(nList)
  1. 在这里使用str.split()
  2. 我认为使用return而不是print是一个好主意。

nList = input("please enter a list of integers separated by commas:\n")
numList = nList.split(',')

def sumArg(numList):
    nSum= map(int, numList)
    numSum = sum(nSum)
    return numSum

print(numList)    
print(sumArg(numList))

演示:

please enter a list of integers separated by commas:
1,2,3,4,5,6,7,8,9,10,120
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '120']
175

这是您的更正代码:

nList = input("please enter a list of integers separated by commas:\n")

# Converts into list of Strings
numList1 = nList.split(',')
print(numList1)
numList = list(map(int, numList1))
print(numList)


def sumArg(numList):
     print(sum(numList))

sumArg(numList)  # runs script

问题是您的numList1 = list(n.join((i) for i in nList if i.isalnum()))正在扫描每个字符。 我上面发布的代码在每个逗号处拆分数组,将数字转换为整数并将它们求和。

 nList = input("please enter a list of integers separated by commas:\\n") 

nList是一个字符串,而字符串是字符序列

 (i) for i in nList 

在这里,您遍历字符串,分别处理每个字符。 您分隔了每个字符,这就是为什么“ 10”变成“ 1”和“ 0”的原因。

由于您输入的每个数字都以逗号分隔,因此请尝试以逗号分隔。

numList1 = nList.split(",")

如果输入内容也用括号括起来,则需要先将其删除,然后再转换为int。

实际上,如果您使用的是Python 2,则可以将脚本简化为:

nList = input("please enter a list of integers separated by commas:\n")
print sum(nList)

这是因为input ,在Python 2,已经从一个字符串输入转换成整数列表。


如果要将字符串作为输入,然后将其转换为整数列表,则可以使用raw_input (或Python 3中的input ):

inputStr = raw_input("please enter a list of integers separated by commas:\n")
listOfStrs = inputStr.split(",")
listOfInts = map(int, listOfStrs)
print sum(listOfInts)

期望输入像1, 2, 3, 4, 5, 6, 7, 8, 9, 10 (不带括号)。

暂无
暂无

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

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