如何将一个浮点数字符串分成两部分?

时间:2022-06-11 02:28:19

I have the string '1234.12'. I want to split the string into two lists [1234] [12]

我有字符串'1234.12'。我想把字符串分成两个列表[1234] [12]

The only way I know is to make the whole string into a list by using .split(). Or use a comprehension [ n.split() for n in '1234.12' ] which gives me

我知道的唯一方法是使用.split()将整个字符串放入列表中。或者在'1234.12'中使用理解[n.split()for n)

[['1'], ['2'], ['3'], ['4'], ['.'], ['1'], ['2']]

3 个解决方案

#1


4  

s = '1234.12'

a,b = ([int(x)]  for x in s.split(".",1))

print(a,b)

Or just do it in 2 parts, map to int and just wrap in lists after:

或者只是分为两部分,映射到int,然后在以下列表中包装:

s = '1234.12'

a, b = map(int,s.split(".", 1))

a,b = [a],[b]

Which will both give you the two numbers cast to int and wrapped in lists:

哪个都会给你两个转换为int并包含在列表中的数字:

a -> [1234]
b ->  [12]

#2


3  

You can split the string with dot and the n use map function to convert the result to list:

您可以使用点分割字符串,使用n使用map函数将结果转换为list:

>>> map(list,s.split('.'))
[['1', '2', '3', '4'], ['1', '2']]

And of you want the result as int you can use int function within map :

并且您希望结果为int,您可以在map中使用int函数:

>>> map(int,s.split('.'))
[1234, 12]

Note that in this case you can access to the numbers with a simple indexing and don't need to put each of them within a list.

请注意,在这种情况下,您可以使用简单的索引访问数字,而无需将每个数字放在列表中。

#3


0  

To explain why what you were trying wasn't working,

要解释你为什么尝试不起作用,

n.split() for n in '1234.12'

is effectively equivalent to

实际上相当于

for n in '1234.12':
    n.split()

When you iterate over a string like that, you end up getting the individual characters, so we can also say that is equivalent to:

当你迭代这样的字符串时,你最终会获得单个字符,所以我们也可以说它相当于:

for n in ('1', '2', '3', '4', '.', '1', '2'):
    n.split()

So since the split was only given a single character at a time, it wasn't functional. You want to move the split to the string itself:

因此,由于拆分一次只给出一个字符,所以它不起作用。您想将拆分移动到字符串本身:

n for n in '1234.12'.split()

At which point you don't need the comprehension anymore.

此时你不再需要理解了。

'1234.12'.split()

The last piece was explained elsewhere... split, by default, splits on whitespace, so to tell it to split on a period instead:

最后一篇文章在别处解释了......默认情况下,split会拆分空白,所以要告诉它拆分一段时间:

'1234.12'.split('.')

#1


4  

s = '1234.12'

a,b = ([int(x)]  for x in s.split(".",1))

print(a,b)

Or just do it in 2 parts, map to int and just wrap in lists after:

或者只是分为两部分,映射到int,然后在以下列表中包装:

s = '1234.12'

a, b = map(int,s.split(".", 1))

a,b = [a],[b]

Which will both give you the two numbers cast to int and wrapped in lists:

哪个都会给你两个转换为int并包含在列表中的数字:

a -> [1234]
b ->  [12]

#2


3  

You can split the string with dot and the n use map function to convert the result to list:

您可以使用点分割字符串,使用n使用map函数将结果转换为list:

>>> map(list,s.split('.'))
[['1', '2', '3', '4'], ['1', '2']]

And of you want the result as int you can use int function within map :

并且您希望结果为int,您可以在map中使用int函数:

>>> map(int,s.split('.'))
[1234, 12]

Note that in this case you can access to the numbers with a simple indexing and don't need to put each of them within a list.

请注意,在这种情况下,您可以使用简单的索引访问数字,而无需将每个数字放在列表中。

#3


0  

To explain why what you were trying wasn't working,

要解释你为什么尝试不起作用,

n.split() for n in '1234.12'

is effectively equivalent to

实际上相当于

for n in '1234.12':
    n.split()

When you iterate over a string like that, you end up getting the individual characters, so we can also say that is equivalent to:

当你迭代这样的字符串时,你最终会获得单个字符,所以我们也可以说它相当于:

for n in ('1', '2', '3', '4', '.', '1', '2'):
    n.split()

So since the split was only given a single character at a time, it wasn't functional. You want to move the split to the string itself:

因此,由于拆分一次只给出一个字符,所以它不起作用。您想将拆分移动到字符串本身:

n for n in '1234.12'.split()

At which point you don't need the comprehension anymore.

此时你不再需要理解了。

'1234.12'.split()

The last piece was explained elsewhere... split, by default, splits on whitespace, so to tell it to split on a period instead:

最后一篇文章在别处解释了......默认情况下,split会拆分空白,所以要告诉它拆分一段时间:

'1234.12'.split('.')