简体   繁体   English

在python中将输入作为以空格分隔的整数

[英]Taking input as an integer separated by spaces in python

So I have gotten the program to work, my problem so far is that my solution requires that the output look like this [44, 76, 34, 98, 1, 99] but I keep getting ['1', '2', '3', '5', '6', '7'] if my input was 44 76 44 34 98 34 1 44 99 1 1 1所以我已经让程序运行了,到目前为止我的问题是我的解决方案要求输出看起来像这样[44, 76, 34, 98, 1, 99]但我一直得到['1', '2', '3', '5', '6', '7']如果我的输入是44 76 44 34 98 34 1 44 99 1 1 1

Trying to convert the final solution into an int and then back into a string does not work, I still end up with quotations.尝试将最终解决方案转换为 int 然后再转换回字符串不起作用,我仍然以引号结束。

The point of the code BTW is to remove duplicates from a given input and print it out in a list顺便说一句,代码的重点是从给定的输入中删除重复项并将其打印在列表中

def eliminateDuplicates(lst): 
    newlist = []
    for number in lst:
        if number not in newlist:
            newlist.append(number)
    return newlist

def main():
    numbers =(input("Enter numbers separated by space: "))
    x = ((numbers.split()))
    print("The distinct numbers are: ", (eliminateDuplicates(x))) 

main()

In the first line we store the input in d. 在第一行中,我们将输入存储在d中。 In the second line we convert d into set() and back to list(). 在第二行中,我们将d转换为set()并返回到list()。 In the third line we map() d to integer. 在第三行中,将map()d映射为整数。

d = raw_input("Enter Numbers: ").split()
d = list(set(d))
d = map(int, d)
print d

尝试这个:

print("The distinct numbers are: ", map(int, eliminateDuplicates(x)))

If the parameter lst is supposed to be a list , why don't you enter a list as input? 如果参数lst应该是list ,为什么不输入列表呢? (Instead of numbers separated by space). (而不是数字用空格分隔)。 Then you can ommit the split stuff: 然后,您可以省略split内容:

def eliminateDuplicates(lst): 
    newlist = []
    for number in lst:
        if number not in newlist:
            newlist.append(number)
    return newlist

def main():
    numbers =(input("Enter numbers as a list: "))
    print("The distinct numbers are: ", (eliminateDuplicates(numbers))) 
main()

Hope this helps: 希望这可以帮助:

from collections import OrderedDict
def eliminateDuplicates(lst): 
    newlist = [int(x) for x in lst]
    newlist = list(OrderedDict.fromkeys(newlist))
    return newlist

def main():
    numbers =(input("Enter numbers separated by space: "))
    x = ((numbers.split()))
    print("The distinct numbers are: ", (eliminateDuplicates(x))) 
main()

The reason you're getting quotation marks around your numbers when you print them is because you're printing a list , which implicitly converts the list to a string (using str ). 在打印数字时会在引号周围引起双引号的原因是因为您正在打印list ,该列表会将列表隐式转换为字符串(使用str )。 The list type's __str__ method uses the repr of each of its list items. list类型的__str__方法使用其每个列表项的repr The repr of a string puts quotes around it. 字符串的repr将引号引起来。

There's a good reason for this. 这是有充分的理由的。 repr is supposed to be a computer-friendly represnetation of an object. repr应该是对象的计算机友好表示。 If possible, it should create a duplicate of the object when eval ed. 如有可能,应在eval时创建该对象的副本。 If lists or strings behaved differently when printed, a list like ["foo", "bar, baz", "quux"] would have an ambiguous repr . 如果列表或字符串在打印时的行为不同,则类似["foo", "bar, baz", "quux"]将具有不明确的repr

Converting the input values to integers (as other answers have suggested) is one solution, but not a necessary one for this task. 将输入值转换为整数(如其他答案所示)是一种解决方案,但对于此任务不是必需的。 Your deduplicating function works just fine with lists of strings. 您的重复数据删除功能可以很好地用于字符串列表。 If the output formatting is the only issue, you can str.format and str.join to create an output string in the form you want from your list of strings, rather than printing the list directly. 如果仅是输出格式设置,则可以使用str.formatstr.join从字符串列表中以所需的形式创建输出字符串,而不是直接打印列表。 For instance: 例如:

print("The distinct numbers are: [{}]".format(", ".join(eliminateDuplicates(x)))

Just replace this line of your code.只需替换这行代码即可。

x =[ int(num) for num in numbers.split(' ') if len(num)>0 ]

Everything will work fine.一切都会好起来的。
After changing this line, your whole code will look like below更改此行后,您的整个代码将如下所示

def eliminateDuplicates(lst): 
newlist = []
for number in lst:
    if number not in newlist:
        newlist.append(number)
return newlist

def main():
    numbers =(input("Enter numbers separated by space: "))
    x =[ int(num) for num in numbers.split(' ') if len(num)>0 ]
    print("The distinct numbers are: ", (eliminateDuplicates(x))) 

main()

Output will be输出将是

Enter numbers separated by space: 44 76 44 34 98 34 1 44 99 1 1 1
The distinct numbers are:  [44, 76, 34, 98, 1, 99]

At the time of reading space separated inputs in python , they are treated as string so you need to convert them into integer.在 python 中读取空格分隔的输入时,它们被视为字符串,因此您需要将它们转换为整数。 strings can be converted into integers in many ways like below字符串可以通过多种方式转换为整数,如下所示
Using list comprehension使用列表理解

numbers = [ int(num) for num in input().split(' ') if len(num)>0 ]

Using filter and map function使用过滤器和地图功能

numbers = list(map(int,filter(lambda x : len(x)>0,input().split(' '))))

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

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