简体   繁体   English

从列表中仅提取整数(而不是浮点数)

[英]Extract only the integers from list (not the floats)

I have some problems with the following issue: 我遇到以下问题:

I have a string, which contains integers and floats. 我有一个字符串,其中包含整数和浮点数。 I fail to extract only the integers (NOT the floats!). 我无法仅提取整数(不是浮点数!)。

What i have (it is a string): 我有(它是一个字符串):

f=  "0:6.0 3:5.6 54:12.3 56:12.0"

How the result should be (not in a string form): 结果应如何(不是字符串形式):

0,3,54,56

I searched on Google (and stack-overflow) which leads to this solution: 我在Google上搜索(并且出现了堆栈溢出),从而得出了以下解决方案:

[int(s) for s in f.split() if s.isdigit()]

That leads to a empty list. 这导致一个空列表。

Other solutions like: 其他解决方案如:

int(re.search(r'\d+', f).group())

Leads to "0 integers". 导致“ 0整数”。 Sorry i'm new but I really can't solve this. 抱歉,我是新手,但我真的无法解决此问题。

You can use .partition(':') : 您可以使用.partition(':')

>>> s="0:6.0 3:5.6 54:12.3 56:12.0"
>>> [e.partition(':')[0] for e in s.split()]
['0', '3', '54', '56']

Then call int on those strings: 然后在这些字符串上调用int

>>> [int(e.partition(':')[0]) for e in s.split()]
[0, 3, 54, 56]

Or, 要么,

>>> map(int, (e.partition(':')[0] for e in s.split()))
[0, 3, 54, 56]

And you can use the same method (with a slight change) to get the floats: 您可以使用相同的方法(稍有变化)来获取浮点数:

>>> map(float, (e.partition(':')[2] for e in s.split()))
[6.0, 5.6, 12.3, 12.0]

Fair question asked in comments: Why use partition ? 在评论中问的公平问题: 为什么要使用分区? you can use int(split(":")[0]) 您可以使用int(split(“:”)[0])

  1. With .partition it is clear to all readers (including your future self) that you are looking at 1 split only. 使用.partition ,所有读者(包括您将来的自己)都可以清楚地看到,您只看1个拆分。 (Granted, you could use the 2 argument form of split(delimiter, maxsplit) but I think that is less clear for a single split...) (当然,您可以使用split(delimiter, maxsplit)的2个参数形式,但我认为对于单个split来说不太清楚...)
  2. It is easier to test successful partitioning since partition always produces a three element tuple and you only need to test the truthiness of the element tuple[1] . 测试成功分区很容易,因为partition 总是生成一个三元素元组,而您只需测试元素tuple[1]的真实性即可。
  3. You can safely use .partion in tuple assignments of the form lh,delimiter,rh=string.partion('delimiter') where lh, rh=string.split('delimiter') will produce a ValueError if the delimiter is not found. 您可以在形式为lh,delimiter,rh=string.partion('delimiter')元组分配中安全地使用.partion ,其中lh, rh=string.split('delimiter')如果找不到分隔符,则lh, rh=string.split('delimiter')会产生ValueError
  4. With the delimiter included in the resulting tuple, it is easier to reassemble the original string with ''.join(tuple_from_partion) vs split since the delimiter in split is lost. 与包括在所产生的元组中的分隔符,它更容易重组原始字符串''.join(tuple_from_partion) VS split由于在分隔符split丢失。
  5. Why not? 为什么不?

How about using the following regex: 如何使用以下正则表达式:

import re

f =  "0:6.0 3:5.6 54:12.3 56:12.0"
answer = [int(x) for x in re.findall(r'\d{1,2}(?=:)', f)]
print(answer)

Output 产量

[0, 3, 54, 56]

You can also achieve the same result using map instead of a list comprehension (as in @dawg's answer): 您也可以使用map而不是列表理解来实现相同的结果(如@dawg的答案):

answer = map(int, re.findall(r'\d{1,2}(?=:)', f))

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

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