我正在尝试按两个值对python中的字符串列表进行排序。我想出的是:
import operator
l = ['A1 B2', 'A2 B2', 'A1 B3', 'A2 B1', 'A1 B1']
sorted(l, key=operator.itemgetter(1, 4))
['A1 B1', 'A1 B2', 'A1 B3', 'A2 B1', 'A2 B2']
但是,键的位置是相对硬编码的,并且如果字符串更改为['AA1 BB2', 'AA2 BB2', 'AA1 BB3', 'AA2 BB1', 'AA1 BB1']
,它将无法正常工作。此外,数字也可以包含多于一个数字['A1 B202', 'A2 B101', 'A1 B200', 'A2 B100', 'A1 B201']
。
如何更优雅地按两位数字对字符串列表进行排序?
答案 0 :(得分:2)
第三方模块有效吗?您可以将natsort
与可拆分并自然排序的键一起使用:
>>> import natsort
>>> natsort.natsorted(l, key=str.split)
['AA1 BB1', 'AA1 BB2', 'AA1 BB3', 'AA2 BB1', 'AA2 BB2']
您可以将natsort
与pip
一起安装。
排序是自然进行的,而不是字典上的,其他回答恰好返回与自然排序相同的答案。
答案 1 :(得分:1)
最好没有lambda:
>>> l=['A1 B202', 'A2 B101', 'A1 B200', 'A2 B100', 'A1 B201']
>>> sorted(l,key=str.split)
['A1 B200', 'A1 B201', 'A1 B202', 'A2 B100', 'A2 B101']
>>>
答案 2 :(得分:0)
0 info it worked if it ends with ok
1 verbose cli [ 'C:\\Development\\nodejs\\node.exe',
1 verbose cli 'C:\\Development\\nodejs\\node_modules\\npm\\bin\\npm-cli.js',
1 verbose cli 'start' ]
2 info using npm@5.6.0
3 info using node@v8.11.3
4 verbose run-script [ 'prestart', 'start', 'poststart' ]
5 info lifecycle example-server@1.0.0~prestart: example-server@1.0.0
6 info lifecycle example-server@1.0.0~start: example-server@1.0.0
7 verbose lifecycle example-server@1.0.0~start: unsafe-perm in lifecycle true
8 verbose lifecycle example-server@1.0.0~start: PATH: C:\Development\nodejs\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin;C:\Users\Derek\Documents\Cloud\Projects\2018-2019\NodeTest\example-server\node_modules\.bin;C:\Development\PostgreSQL\10\bin;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Program Files\ImageMagick-7.0.7-Q16;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\libnvvp;C:\Development\Python3\;C:\Development\Python3\Scripts;C:\Program Files (x86)\Common Files\Intel\Shared Files\cpp\bin\Intel64;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files (x86)\Windows Live\Shared;C:\Development\cURL\bin;C:\Development\Python27\;C:\Program Files (x86)\GtkSharp\2.12\bin;C:\Program Files\TortoiseSVN\bin;C:\Development\cygwin64\bin;C:\Development\MinGW\bin;C:\Development\ChucK\/bin;C:\Program Files\Git LFS;C:\Development\ANDROID_SDK;C:\Development\ANDROID_SDK\tools;C:\Program Files (x86)\WinMerge;C:\Program Files (x86)\Skype\Phone\;C:\Development\Yarn\bin;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Program Files\dotnet\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\Amazon\AWSCLI\;C:\Development\nodejs\;C:\Program Files\heroku\bin;C:\Users\Derek\AppData\Local\GitHub\PortableGit_69bd5e6f85e4842f07db71c9618a621154c52254\bin;C:\Development\flutter\bin;C:\Program Files\ImageMagick-7.0.7-Q16;C:\Development\ffmpeg-3.4.1-win64-static\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\bin;C:\Development\Python3\Scripts\;C:\Development\Python3\;C:\Development\RailsInstaller\Ruby2.2.0\bin;C:\Development\ANDROID_SDK\platform-tools\;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files (x86)\Windows Live\Shared;C:\Development\cURL\bin;C:\Users\Derek\AppData\Roaming\npm
9 verbose lifecycle example-server@1.0.0~start: CWD: C:\Users\Derek\Documents\Cloud\Projects\2018-2019\NodeTest\example-server
10 silly lifecycle example-server@1.0.0~start: Args: [ '/d /s /c', 'node server.js' ]
11 silly lifecycle example-server@1.0.0~start: Returned: code: 1 signal: null
12 info lifecycle example-server@1.0.0~start: Failed to exec start script
13 verbose stack Error: example-server@1.0.0 start: `node server.js`
13 verbose stack Exit status 1
13 verbose stack at EventEmitter.<anonymous> (C:\Development\nodejs\node_modules\npm\node_modules\npm-lifecycle\index.js:285:16)
13 verbose stack at emitTwo (events.js:126:13)
13 verbose stack at EventEmitter.emit (events.js:214:7)
13 verbose stack at ChildProcess.<anonymous> (C:\Development\nodejs\node_modules\npm\node_modules\npm-lifecycle\lib\spawn.js:55:14)
13 verbose stack at emitTwo (events.js:126:13)
13 verbose stack at ChildProcess.emit (events.js:214:7)
13 verbose stack at maybeClose (internal/child_process.js:925:16)
13 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:209:5)
14 verbose pkgid example-server@1.0.0
15 verbose cwd C:\Users\Derek\Documents\Cloud\Projects\2018-2019\NodeTest\example-server\src
16 verbose Windows_NT 6.3.9600
17 verbose argv "C:\\Development\\nodejs\\node.exe" "C:\\Development\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "start"
18 verbose node v8.11.3
19 verbose npm v5.6.0
20 error code ELIFECYCLE
21 error errno 1
22 error example-server@1.0.0 start: `node server.js`
22 error Exit status 1
23 error Failed at the example-server@1.0.0 start script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]
很抱歉用手机写的《错误提示》