Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.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_Python 3.x - Fatal编程技术网

Python 如何对二进制字符串列表进行排序

Python 如何对二进制字符串列表进行排序,python,python-3.x,Python,Python 3.x,比如说,我有一个二进制代码列表,如下所示: a=['100','10','01010','000','0001','10001'] 我希望排序后的列表为: a=['000','0001','10','100','01010','10001'] 您可以将函数int用作键: sorted(a, key=lambda x: int(x, 2)) # ['000', '0001', '10', '100', '01010', '10001'] 您可以将函数int用作键: sorted(a, key

比如说,我有一个二进制代码列表,如下所示:

a=['100','10','01010','000','0001','10001']
我希望排序后的列表为:

a=['000','0001','10','100','01010','10001']

您可以将函数
int
用作键:

sorted(a, key=lambda x: int(x, 2))
# ['000', '0001', '10', '100', '01010', '10001']

您可以将函数
int
用作键:

sorted(a, key=lambda x: int(x, 2))
# ['000', '0001', '10', '100', '01010', '10001']
输出

[0, 1, 10, 100, 1010, 10001]
输出

[0, 1, 10, 100, 1010, 10001]