python - 用里面的字符串对列表进行排序
问题描述
我再次需要你的帮助,我想整理一份清单。排序是根据A或B前面的数字
怎么做?谢谢
original list:
['1A21','1B21','21A21','21B21','14A21','14B21'....]
the length of string with A and B are not the same. For example, there are '31A21', but there is no '31B21'
sorted list:
['1A21','2A21'........'1B21','2B21'.......]
My original code is for the case only has either A or B inside the string:
wells.sort(key=key_sortwell,reverse=True)
def key_sortwell(well):
return int(well.split('A')[0])
But now I have both A and B inside the string. Now I am not sure how to do it?
解决方案
您可以将内置sorted
函数与一个键函数一起使用,该函数接受每个字符串并返回之前的数字A
或B
sorted_lst = sorted(original_lst, key=lambda s:s.replace('B', 'A').split('A')[0])
推荐阅读
- javascript - 评估 JavaScript 中的 if 语句
- reactjs - 出乎意料的'!在 'worker-loader!mapbox-gl/dist/mapbox-gl-csp-worker'
- github - 克隆 github 存储库时如何解决错误
- python - 在 Spark DataFrame 中展平嵌套数组
- android - 有大数据问题的 Android 密钥库,我们使用 AES/GCM/NoPadding。在特定范围后抛出异常
- sql - 运行时错误代码 3709:从文件运行 SQL 查询并将数据粘贴到电子表格中
- julia - 如何在 VSCode 中看到与 RStudio“全局环境”相同的内容?
- python - 反验证码不起作用,在回调之前进行验证 - Selenium
- c# - 发票人 PDF 给出 null 但会员资格不给出 PDF null
- azure-ad-b2c - AAD B2C“呼叫我”选项未启动 MFA 电话呼叫