Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用内部字符串对列表进行排序_Python - Fatal编程技术网

Python 使用内部字符串对列表进行排序

Python 使用内部字符串对列表进行排序,python,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'.....

我再次需要你的帮助,我想整理一下清单。排序基于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(原始_lst,key=lambda s:s.replace('B','A')。split('A')[0])

你不会说你想如何打破关系——如果你想先按第一个数字排序,然后按字母排序,然后按第二个数字排序,等等,我建议使用
函数将字符串拆分为数字和字母:

>>> from itertools import groupby
>>> original_list = ['1A21','1B21','21A21','21B21','14A21','14B21']
>>> sorted(original_list, key=lambda s: [int("".join(group)) if d else "".join(group) for d, group in groupby(s, key=str.isdigit)])
['1A21', '1B21', '14A21', '14B21', '21A21', '21B21']
>>> sorted(original_list, key=lambda s: [int("".join(group)) if d else "".join(group) for d, group in groupby(s, key=str.isdigit)][1::-1])
['1A21', '14A21', '21A21', '1B21', '14B21', '21B21']
表达式分解为单个字符串,可以更容易地查看其工作原理:

>>> [(d, list(group)) for d, group in groupby("1A21", key=str.isdigit)]
[(True, ['1']), (False, ['A']), (True, ['2', '1'])]
>>> [(d, ''.join(group)) for d, group in groupby("1A21", key=str.isdigit)]
[(True, '1'), (False, 'A'), (True, '21')]
>>> [(d, ''.join(group)) for d, group in groupby("1A21", key=str.isdigit)][1::-1]
[(False, 'A'), (True, '1')]
>>> [int(''.join(group)) if d else ''.join(group) for d, group in groupby("1A21", key=str.isdigit)][1::-1]
['A', 1]

当您出于排序目的比较列表时,首先对第一个元素进行排序,以此类推。

这是不正确的语法,项目是否为字符串?是的,很抱歉,所有项目均为字符串,谢谢请重复并从中删除。“演示如何解决此编码问题”不是堆栈溢出问题。我们希望您做出诚实的尝试,然后就您的算法或技术提出具体问题。堆栈溢出不是为了替换现有的文档和教程。您需要学习一些字符串处理以及如何提供自定义排序键。我做了。我会把我的代码摆出来谢谢Samwise,我试过了,结果是一样的,它变成了['1A21',1B21',2A21',2B21',我想要的是['1A21',2A21',1B21',2B21']谢谢你说你想根据字符串开头的数字排序。为什么你有一个1后面有一个2?14号和21号怎么了?谢谢Sam,很抱歉不清楚。我感谢你的帮助。21在A或B不需要考虑的情况下,首先需要用A和B将列表分组为字符串,然后根据A或B之前的编号对列表进行排序。?在我的答案中添加了一点,可以分解每个步骤。大多数重要的事情都发生在
groupby
中,这使您可以将字符串分为数字和非数字。谢谢Hadrian,不幸的是这不起作用