Python 按前两个字符将数组排序到表中
好的,我有一个这样的数组:Python 按前两个字符将数组排序到表中,python,arrays,sorting,Python,Arrays,Sorting,好的,我有一个这样的数组: ['0157', '0234', '0467', '0164', '0363', '0341', '0179', ...] 我需要将数组排序为表(2D数组),如下所示: 0157|0234|0326|0467 0164| |0341| 0179| | | 你能帮我吗?试试这个代码 import itertools data = ['0157', '0234', '0467', '0164', '0363', '0341', '017
['0157',
'0234',
'0467',
'0164',
'0363',
'0341',
'0179',
...]
我需要将数组排序为表(2D数组),如下所示:
0157|0234|0326|0467
0164| |0341|
0179| | |
你能帮我吗?试试这个代码
import itertools
data = ['0157', '0234', '0467', '0164', '0363', '0341', '0179']
output = []
for key, group in itertools.groupby(sorted(data), lambda x: x[:2]):
output.append(list(group))
print output
其中,groupby
通过前两个字符x[:2]
进行分组
输出是
[['0157', '0164', '0179'], ['0234'], ['0341', '0363'], ['0467']]
有关“分组依据”的更多信息,请查看。标准是什么?它是第二个字符串值吗?你试了什么?给我们看看你写的代码