업데이트 :이 이전 게시물을 참조함으로써 궁극적 인 목표를 훨씬 명확하게했습니다. 섹션함수 호출에서 튜플 분할하기
def section(s):
return[int(_) for _ in s.split(".")]
및 L1, L2, L3이다 코드
result = list(zip(*sorted(zip(l1, l2, l3, files_good_list), section(l1), key = lambda x: float(x[0]))))
의 다음 라인으로 files_good_list Combing 2D list of tuples and then sorting them in Python
스트링의리스트이다.
제 목표는이 네 가지 목록을 결합한 다음 l1로 정렬하는 것입니다. 어디
결과를 사용하는 경우
l1 = ['1', '1.1', '1.2', '1.10', '2.1', '3.1', '1', '1.1', '1.2', '1.3', '1.4', '2', '2.1', '2.10', '3', '3.1', '3.2', '3.3', '3.4', '3.5', '3.6', '3.7', '3.8']
내 코드가 작동 = 목록 (우편 (* 분류 (우편 (L1, L2, L3, files_good_list), 키 = 람다 X : 플로트 (X [0])))))
그러나 l1을 '1', '1.1', '1.2', '1.10'으로 정렬하려면 '1', '1.1', 1.10 ',' . 이것이 내가 원하는 순서로 정렬하기 위해 함수 섹션을 사용하려고하는 이유입니다.
나는 내가이 오류가 인수로에서 통과 할 때, How do I sort a list of section numbers in Python?그러나 유사하다이 게시물에 대한 답변에서 섹션을 발견했다.
Traceback (most recent call last):
File "<ipython-input-422-bbd574034cbd>", line 1, in <module>
runfile('C:/Users/justin.white/Documents/Work/Regex_try.py', wdir='C:/Users/justin.white/Documents/Work')
File "C:\Users\justin.white\AppData\Local\Continuum\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 699, in runfile
execfile(filename, namespace)
File "C:\Users\justin.white\AppData\Local\Continuum\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 88, in execfile
exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)
File "C:/Users/justin.white/Documents/Work/Regex_try.py", line 84, in <module>
result = list(zip(*sorted(zip(l1, l2, l3, files_good_list), section(l1), key = lambda x: float(x[0]))))
File "C:/Users/justin.white/Documents/Work/Regex_try.py", line 82, in section
return[int(_) for _ in s.split(".")]
AttributeError: 'list' object has no attribute 'split'
하지만 난이 오류가 발생하지 않는
sorted(l1, key=section)
을하고 그것이 내가 원하는 순서대로 정렬 할 때. 그래서 내 질문에 왜 섹션을 zip에있을 때 정렬로 전달할 수없는 것입니까?
설명이 필요하면 알려주십시오. 감사합니다
당신은 당신의 키의 부동 소수점 값으로 정렬하고, 전'1.10'을'1.1'을 배치하고'1.10' 함께, 왜 궁금 '1.2'?제게 그것이 당신이 기대해야 할 것과 똑같은 것 같습니다 ... – twalberg
그것이 그 일을 이해합니다. 이것이 함수 섹션이 1.1, 1.10, 1.2 대신 1.1, 1.2, 1.10으로 정렬되는 이유입니다. – Jstuff
'sorted' 함수 시그니처는'sorted (iterable, cmp = None, key = None, reverse = False)'또는'sorted (iterable, key = None, reverse = False)'중 하나입니다. (파이썬 3.x). 나는 당신이 당신의'section' 함수가 어떻게 재생 될지를 예상하지 못합니다 - 그것은 외계의 인자로서 무시 될 가능성이 있습니다 ... – twalberg