소프트웨어 버전과 같이 점으로 구분 된 숫자 목록 정렬
다음과 같은 버전 문자열이 포함 된 목록이 있습니다.
versions_list = ["1.1.2", "1.0.0", "1.3.3", "1.0.12", "1.0.2"]
나는 그것을 정렬하고 싶다. 그래서 결과는 다음과 같을 것이다.
versions_list = ["1.0.0", "1.0.2", "1.0.12", "1.1.2", "1.3.3"]
숫자의 우선 순위는 분명히 왼쪽에서 오른쪽으로, 내림차순이어야합니다. 그래서 1.2.3
이전 2.2.3
과 이전 에 2.2.2
옵니다 2.2.3
.
파이썬에서 어떻게합니까?
각 버전 문자열을 분할하여 정수 목록으로 비교합니다.
versions_list.sort(key=lambda s: map(int, s.split('.')))
귀하의 목록에 대해 다음을 제공합니다.
['1.0.0', '1.0.2', '1.0.12', '1.1.2', '1.3.3']
Python3 map
에서는 더 이상를 반환하지 않으므로 호출list
로 래핑list
해야합니다 .
versions_list.sort(key=lambda s: list(map(int, s.split('.'))))
여기에 매핑하는 대안은 목록 이해 입니다. 목록 이해에 대한 자세한 내용은 이 게시물 을 참조하십시오 .
versions_list.sort(key=lambda s: [int(u) for u in s.split('.')])
distutils.version
표준 라이브러리의 모듈을 사용할 수도 있습니다 .
from distutils.version import StrictVersion
versions = ["1.1.2", "1.0.0", "1.3.3", "1.0.12", "1.0.2"]
versions.sort(key=StrictVersion)
제공합니다 :
['1.0.0', '1.0.2', '1.0.12', '1.1.2', '1.3.3']
또한 다음과 같이 사전 출시 태그가있는 버전을 처리 할 수 있습니다.
versions = ["1.1", "1.1b1", "1.1a1"]
versions.sort(key=StrictVersion)
제공합니다 :
["1.1a1", "1.1b1", "1.1"]
문서 : https://github.com/python/cpython/blob/3.2/Lib/distutils/version.py#L101
natsort는 "자연 정렬"을 제안합니다. 매우 직관적으로 작동합니다 (Python 3에서)
from natsort import natsorted
versions = ["1.1.2", "1.0.0", "1.3.3", "1.0.12", "1.0.2"]
natsorted(versions)
준다
['1.0.0', '1.0.2', '1.0.12', '1.1.2', '1.3.3']
그러나 버전 번호가있는 전체 패키지 이름에서도 작동합니다.
versions = ['version-1.9', 'version-2.0', 'version-1.11', 'version-1.10']
natsorted(versions)
준다
['version-1.9', 'version-1.10', 'version-1.11', 'version-2.0']
내 버전은 몇 가지 추가 작업을 수행하지만 Python을 사용 하여이 질문을 해결했지만 다음은 내 코드입니다.
def answer(l):
list1 = [] # this is the list for the nested strings
for x in l:
list1.append(x.split("."))
list2 = [] # this is the same list as list one except everything is an integer in order for proper sorting
for y in list1:
y = map(int, y)
list2.append(y)
list3 = sorted(list2) #this is the sorted list of of list 2
FinalList = [] # this is the list that converts everything back to the way it was
for a in list3:
a = '.'.join(str(z) for z in a)
FinalList.append(a)
return FinalList
For versions there exist three things; Major, Minor, and the revision. What this does is that it organises it so that '1'
will come before '1.0'
which will come before '1.0.0'
. Also, another plus, no need to import any libraries incase you don't have them, and it works with old versions of Python, this one was specifically meant for Version 2.7.6. Anyway, here are a few examples:
Inputs:
(string list) l = ["1.1.2", "1.0", "1.3.3", "1.0.12", "1.0.2"]
Output:
(string list) ["1.0", "1.0.2", "1.0.12", "1.1.2", "1.3.3"]
Inputs:
(string list) l = ["1.11", "2.0.0", "1.2", "2", "0.1", "1.2.1", "1.1.1", "2.0"]
Output:
(string list) ["0.1", "1.1.1", "1.2", "1.2.1", "1.11", "2", "2.0", "2.0.0"]
If you have any questions, just comment on the answer!!
'Program Tip' 카테고리의 다른 글
ZooKeeper가 실행 중인지 또는 명령 프롬프트에서 실행 중인지 확인하는 방법은 무엇입니까? (0) | 2020.12.02 |
---|---|
jQuery : 선택한 라디오 버튼에 대한 부모 tr 가져 오기 (0) | 2020.12.02 |
Rails의 경로 별칭 (0) | 2020.12.02 |
RecyclerView에서 단일 선택 (0) | 2020.12.02 |
sudo : 포트 : 명령을 찾을 수 없습니다. (0) | 2020.12.02 |