我有一个字符串列表,如下所示:
['dsafjhsafjkhefhajwejh;NR;123;dfgdsrhgjhdfgjhdsfjhg','jhfewrgjhdfjhgsufgssdfjgh;NR;3243;fgjdsgfjsdfkjgdf', ...]
此列表应按“;”之间的数字排序,数字总是在“NR;”之后,但并非每个字符串都包含数字。我已经尝试了数百种解决方案,但似乎没有一个有效。在
请帮帮我
编辑:
抱歉,我没有添加一些示例,因此它们是:
1。在
^{pr2}$对于a中的项目: b、 追加(搜索(“(;NR;[0-9]*;)”,str(item)))
基本上我试过上面的代码和上面的一些变体,我记不清我试过的所有代码。在
对列表排序总是接受“key”参数,并且可以将任意函数作为key传递。在您的情况下,函数应该执行以下操作:
这里有一种方法
现在,您可以使用filter分离出包含“NR;”的字符串。在
^{2}$最后,对结果列表进行排序很简单
或者
一些注意事项:必须确保列表中的所有字符串都能满足“NR;<;int>;”regex的假设,并且字符串的int部分不应太大,这样“int()”转换不会导致溢出。在
正如下面的@EMS所指出的,可以在
^{2}$NR;
上拆分,如果始终存在NR;
,则可以使用其第一部分。在这将按顺序对列表进行排序。如果要创建列表的副本(为其分配一个新变量),可以使用
sorted
端到端:
相关问题 更多 >
编程相关推荐