简体   繁体   English

从整数列表中删除空格

[英]Remove spaces from a list of integers

I wrote a code that accepts multiple numbers and converts them into a list of integers.我编写了一个接受多个数字并将它们转换为整数列表的代码。 But I get them with spaces.但我用空格得到它们。

For example: I enter as input: 1,2,3,4,5 ( with commas ).例如:我输入为输入:1,2,3,4,5(带逗号)。

I get a list of [1, 2, 3, 4, 5]我得到[1, 2, 3, 4, 5]的列表

Now I just need to delete the spaces but It's not working, I need it to look something like this [1,2,3,4,5] .现在我只需要删除空格,但它不起作用,我需要它看起来像这样[1,2,3,4,5] I tried doing it this way:我试过这样做:

numbers = input().split(',')

for i in range(0, len(numbers)):
    numbers[i] = int(numbers[i])
mylist = str(numbers).replace(' ','')
print(mylist)

This causes the square parentheses to be considered as items.这导致方括号被视为项目。

How do I delete the spaces the right way?如何以正确的方式删除空格?

I think you are conflating the list and the representation of the list.我认为您将列表和列表的表示混为一谈。 The list itself doesn't have spaces, or even commas, it just has the items (in your case, the numbers).列表本身没有空格,甚至没有逗号,它只有项目(在你的情况下是数字)。 The representation of the list is just that - a way to represent the data inside of it in a normal, standard way.列表的表示就是这样 - 一种以正常、标准的方式表示其中的数据的方法。 That standard includes commas and spaces.该标准包括逗号和空格。

If you want a new thing that represents the list and its items in the way you are saying, you can do that by making a new string just like that.如果您想要一个以您所说的方式表示列表及其项目的新事物,您可以通过像这样创建一个新字符串来实现。

str(numbers).replace(' ','')

This has two functions in it, chained together.这有两个功能,链接在一起。 First, we do str(numbers) to get a string that is the string-representation of the list.首先,我们使用str(numbers)来获得一个字符串,它是列表的字符串表示形式。 This will be the string '[1, 2, 3, 4, 5]' .这将是字符串'[1, 2, 3, 4, 5]' Then you replace any blank space-bar ' ' with nothing '' .然后,您replace任何空白空格键' '替换为 nothing ''

Edit:编辑:

I think I read your question too quickly and see that you did the exact same as what I have in my code here and said it isn't doing exactly what you want to do.我想我读你的问题太快了,看到你做的和我在这里的代码中的完全一样,并说它没有做你想做的事。 I think the answer here is: no.我认为这里的答案是:不。

As I say in my first paragraph, there is the list and there is the lists representation.正如我在第一段中所说,有列表,也有列表表示。 The function for the list's representation is a python built-in that can not be trivially overridden.列表表示的函数是一个不能被轻易覆盖的python内置函数。 I'm not saying it can't be done, but I don't think you'll get it done without defining some new things.我并不是说它不能完成,但我认为如果不定义一些新事物就无法完成它。

My understanding of your problem is that you want to input a list as a string and want to transform it into a Python list of numbers.我对您的问题的理解是,您想将列表作为字符串输入,并希望将其转换为 Python 数字列表。

If you input the following string [1, 2, 3, 4, 5] , then you can split on , .如果输入以下字符串[1, 2, 3, 4, 5] ,则可以在,上进行拆分。 Then you need to consider removing the leftmost and rightmost character, that correspond to the brackets:然后你需要考虑删除最左边和最右边的字符,对应于括号:

numbers = input()[1:-1].split(', ')

for i in range(len(numbers)):
    numbers[i] = int(numbers[i])

print(numbers)

Other options to transform the numbers from strings to integers are the following:将数字从字符串转换为整数的其他选项如下:

  • Python built-in map function: Python内置map功能:

     numbers = input()[1:-1].split(', ') numbers = list(map(int, numbers)) print(numbers)
  • Python list comprehension: Python列表理解:

     numbers = input()[1:-1].split(', ') numbers = [int(n) for n in numbers] print(numbers)

You can change the print behavior of the list to something you code yourself.您可以将列表的打印行为更改为您自己编写的代码。

numbers = input().split(',')

for i in range(0, len(numbers)):
    numbers[i] = int(numbers[i])
mylist = str(numbers).replace(' ','')

print('[' + ','.join([str(n) for n in numbers]) + ']')

This prints a bracket [ , then each number separated by commas without any spaces, and finally the ending bracket ] .这将打印一个括号[ ,然后每个数字用逗号分隔,没有任何空格,最后是结束括号]

Output looks like this:输出如下所示:

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

What you might want to do is create a subclass of list that has the repr implementation you want.您可能想要做的是创建一个具有您想要的repr实现的list子类。 That way you can still operate on your list object as you would any other list (eg you can access numbers[0] and it will return the first number), but printing the list as a whole will produce the output you're looking for.这样,您仍然可以像对任何其他列表一样对列表对象进行操作(例如,您可以访问numbers[0]并且它将返回第一个数字),但是将列表作为一个整体打印将产生您正在寻找的输出.

class MyList(list):
    def __repr__(self) -> str:
        return "[" + ",".join(repr(i) for i in self) + "]"

numbers = MyList(int(n) for n in input().split(","))
print(numbers)
print(numbers[0])

Input:输入:

1,2,3,4,5

Output:输出:

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

Since MyList is a list subclass, you can construct one from any iterable, including a generator expression (as in the code above) or an existing list, eg:由于MyList是一个list子类,您可以从任何可迭代对象构造一个,包括生成器表达式(如上面的代码中所示)或现有列表,例如:

>>> n_copy = MyList(numbers)
>>> n_copy
[1,2,3,4,5]

You could use the re module's split function to deal with the whitespaces when splitting.您可以使用re模块的split函数来处理拆分时的空格。 Then you just need to join the elements with ',' and embed the whole thing between brackets in the string representation.然后,您只需要使用','连接元素,并将整个内容嵌入到字符串表示中的括号之间。

import re

numbers = re.split(r'\s*,\s*', input())

print(f"[{','.join(numbers)}]")

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

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