【发布时间】:2023-01-05 23:42:13
【问题描述】:
假设您有一个包含很多数字的字符串,这些数字非常靠近某些字符 像这样:
string = "I have a cellphone with 4GB of ram and 64 GB of rom, My last computer had 4GB of ram and NASA only had 4KB when ... that's incredible"
我希望它返回:
[4GB, 64GB, 4GB, 4KB]
我正在努力
import re
def extract_gb(string):
gb = re.findall('[0-9]+',string)
return gb
extract_gb(string)
输出 [4, 64, 4, 4]
只给出数字作为输出,但它想获得数字和附加或关闭它的字符串集,我希望输出 [4GB, 64GB, 4GB, 4KB]
我感谢任何形式的帮助。
【问题讨论】:
-
这应该是答案。
-
@barryodev 是的,可能吧。在我注意到我的错误并调换了数字和字母之后。
-
“o 非常接近”:请定义“非常接近”。
-
它起作用了,谢谢,非常接近的是当我们在数字和字符集之间有一个空格“”时,例如“64 GB”