string = 'Hello, welcome to my world001'
Run Code Online (Sandbox Code Playgroud)
这是我一直试图分成两个(数字和字符串)的字符串。后面的数字需要从原始字符串中分离出来。python中是否有任何方法可以真正快速地做到这一点。
这是我的尝试(代码):
length_of_string = len(string) - 1
num = []
if string[-1].isdigit():
while length_of_string > 0:
if string[length_of_string].isdigit():
num.insert(0, int(string[length_of_string]))
length_of_string -= 1
print(num)
else:
string += '1'
print(string)
Run Code Online (Sandbox Code Playgroud) my_lst = [
{"usage": "0.59 Kbit/s"},
{"usage": "383.34 bit/s"},
{"usage": "158.13 bit/s"},
{"usage": "4.15 Gbit/s"},
{"usage": "18.5 Mbit/s"},
{"usage": "45 Kbit/s"}
]
Run Code Online (Sandbox Code Playgroud)
试图实现的输出应该是这样的,带有排序值:-
my_lst = [
{'usage': '4.15 Gbit/s'},
{'usage': '18.5 Mbit/s'},
{'usage': '45 Kbit/s'},
{'usage': '0.59 Kbit/s'},
{'usage': '383.34 bit/s'},
{'usage': '158.13 bit/s'}
]
Run Code Online (Sandbox Code Playgroud)