從字符串中找出最大的數字串,字符串中可以帶小數點。--python實現


 1 import re
 2 input_str = "abcd123.4567.891.123yfdsa4567.54"
 3 # 使用正則找出所有數字及下標
 4 pattern = re.compile(r"\d+")
 5 s = pattern.finditer(input_str)
 6 tmp = []
 7 for i in s:
 8     tmp.append((i.group(), i.regs[0]))
 9 # 求出最大的數及對應下標
10 max_int = max(map(int, [c[0] for c in tmp]))
11 tmp = [i for i in tmp if i[0]==str(max_int)]
12 print(tmp)
13 find = []
14 for t in tmp:
15     inx = t[1][1]
16     xiaoshu = ""
17     _f = t[0]
18     if len(input_str) != inx and input_str[inx] == ".":
19         _inx = inx
20         for c in range(len(input_str[inx+1:])):
21             if input_str[_inx+1].isdigit():
22                 _inx += 1
23             else:
24                 break
25         xiaoshu = input_str[inx+1:_inx+1]
26         print("xiaoshu:", xiaoshu)
27     if xiaoshu:
28         _f = t[0] + "."+xiaoshu
29     find.append(_f)
30 print("===========")
31 find = max(map(float, find))
32 print(find)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM