小编Sun*_*dav的帖子

如何将字符串拆分为数字和字符

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)

python string split

6
推荐指数
1
解决办法
176
查看次数

如何首先根据字典的单位(bit/s、Kbit/s 等)对特定的字典列表进行排序,然后根据它们的值对它们进行排序

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)

python sorting dictionary list prometheus

2
推荐指数
1
解决办法
49
查看次数

标签 统计

python ×2

dictionary ×1

list ×1

prometheus ×1

sorting ×1

split ×1

string ×1