Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个字母数字代码,如下所示:
"PRODUCTNAME600COUPON50"
哪里PRODUCTNAME是长度不一致的变量
PRODUCTNAME
我希望能够将字符串的整数值提取到一个列表中——在这种情况下[600, 50]。
[600, 50]
我正在寻找一种巧妙的 Pythonic 方式来实现它——我从这个解决方案开始,用于查找字符串中第一个数字的索引,但在这种情况下它不够用。
使用以下正则表达式:
In [67]: strs="PRODUCTNAME600COUPON50" In [68]: re.findall(r'\d+',strs) Out[68]: ['600', '50']
获取整数:
In [69]: map(int,re.findall(r'\d+',strs)) Out[69]: [600, 50]