Python 将空格分隔的整数字符串更改为 int 列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19555472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:02:07  来源:igfitidea点击:

Change a string of integers separated by spaces to a list of int

pythonstringlistint

提问by Spock

How do i make something like

我如何制作类似的东西

x = '1 2 3 45 87 65 6 8'

>>> foo(x)
[1,2,3,45,87,65,6,8]

I'm completely stuck, if i do it by index, then the numbers with more than 1 digit will be broken down. Help please.

我完全卡住了,如果我按索引来做,那么超过 1 位的数字将被分解。请帮忙。

回答by tijko

The most simple solution is to use .split()to create a list of strings:

最简单的解决方案是使用.split()创建字符串列表:

x = x.split()

Alternatively, you can use a list comprehension in combination with the .split()method:

或者,您可以将列表推导.split()方法结合使用:

x = [int(i) for i in x.split()]

You could even use map mapas a third option:

您甚至可以使用 mapmap作为第三种选择:

x = list(map(int, x.split()))

This will create a listof int's if you want integers.

如果你想要整数,这将创建一个listof int

回答by aIKid

Just to make a clear explanation.

只是为了做一个清楚的解释。

You can use the string method str.split()which split the string into a list. You can learn more about this method here.

您可以使用将字符串str.split()拆分为列表的字符串方法。您可以在此处了解有关此方法的更多信息。

Example:

例子:

def foo(x):
    x = x.split() #x is now ['1','2','3','45', ..] the spaces are removed.
    for i, v  in enumerate(x): #Loop through the list
        x[i] = int(v) #convert each element of v to an integer

That should do it!

应该这样做!

>>> x
[1, 2, 3, 45, 87, 65, 6, 8]

回答by Sravan K Ghantasala

A simple line can be...

一条简单的线可以...

print (map(int, x.split()))  

As some one wisely corrected me, in python >=3, it shall become,

正如有人明智地纠正我的那样,在 python >=3 中,它会变成,

print(list(map(int,x.split())))  

It can also be user in earlier versions.

它也可以是早期版本的用户。

回答by Shivendra Singh

No need to worry, because python provide split() function to change string into a list.

不用担心,因为 python 提供了 split() 函数来将字符串更改为列表。

x='1 2 3 4 67 8 9'
x.split()

['1', '2', '3', '4', '67', '8']

['1', '2', '3', '4', '67', '8']

or if you want output in integer form then you can use map function

或者如果你想以整数形式输出,那么你可以使用 map 函数

map(int ,x.split(' '))

[1, 2, 3, 4, 67, 8]

[1, 2, 3, 4, 67, 8]

回答by Mikaelblomkvistsson

Having input with space at beginning or end of the string or delimited with multiple uneven amount of spaces between the items as above, s.split(' ') returns also empty items:

在字符串的开头或结尾输入空格,或者在项目之间用多个不均匀数量的空格分隔,s.split(' ') 也返回空项目:

>>> s=' 1 2  3 4 67 8 9 ' 
>>> list(s.split(' '))
['', '1', '2', '', '3', '4', '67', '8', '9', '']

I's better to avoid specifying a delimiter:

我最好避免指定分隔符:

>>> list(s.split())
['1', '2', '3', '4', '67', '8', '9']

If the optional second argument sep is absent or None, the words are separated by arbitrary strings of whitespace characters (space, tab, newline, return, formfeed).

如果可选的第二个参数 sep 不存在或 None ,则单词由任意的空白字符字符串(空格、制表符、换行符、返回、换页符)分隔。

If you want to split only at spaces, empty strings can be easily filtered:

如果只想在空格处拆分,可以轻松过滤空字符串:

>>> [item for item in s.split(' ') if item]
['1', '2', '3', '4', '67', '8', '9']

回答by 6pack kid

Assuming you only have digits in your input, you can have something like following:

假设您的输入中只有数字,您可以使用以下内容:

>>> x = '1 2 3 45 87 65 6 8'
>>> num_x = map(int, filter(None, x.split(' ')))
>>> num_x
[1 2 3 45 87 65 6 8]

This will take care of the case when the digits are separated by more than one space character or when there are space characters in front or rear of the input. Something like following:

当数字被多个空格字符分隔或在输入的前面或后面有空格字符时,这将处理这种情况。类似于以下内容:

>>> x = ' 1 2 3  4 '
>>> num_x = map(int, filter(None, x.split(' ')))
>>> num_x
[1, 2, 3, 4]

You can replace input to x.split(' ')to match other delimiter types as well e.g. ,or ;etc.

您也可以将输入替换x.split(' ')为匹配其他分隔符类型,例如,;等。