In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the "Python3 string comparison and rewrite cmp function method" related knowledge, Xiaobian through the actual case to show you the operation process, the operation method is simple and fast, practical, I hope that this "Python3 string comparison and rewrite cmp function method" article can help you solve the problem.
Python3 string comparison _ override cmp function
Because I am used to using the CMP function in C + +, when it comes to string sorting, I take it for granted that I use sort (start, end, CMP) to sort the list, but the result doesn't seem to work.
Later, when I looked up the online information, it seemed that the CMP function had been replaced in python3.
So we have no choice but to find another way. The following is a very simple string date extraction and sorting by date.
The requirement is that because the string read from the text is unordered, it needs to be output before and after time.
Don't say much. Go straight to the code #! / usr/bin/python#_*_coding:utf-8_*_import functoolsimport re def cmp (str1,str2): day1 = (re.search (r'\ d {4} _\ d {2} _\ d {2}', str1)) .group () day2 = (re.search (r'\ d {4} _\ d {2} _\ d {2}', str2)) .group () start1 = (re.search (r'Start\ d') Str1) .group () start2 = (re.search (r'Start\ dcards, str2) .group () if day1 > day2: return 1 elif day1
< day2: return -1 elif start1 >Start2: return 1 elif start1 < start2: return-1 else: return 0if _ _ name__ ='_ _ main__': strList = [r "STRLIST2018_07_30\ Start0", r "STRLIST2018_05_01\ Start0", r "STRLIST2018_06_30\ Start1", r "STRLIST2018_05_01\ Start1", r "STRLIST2018_05_30\ Start0" R "STRLIST2018_06_01\ Start0", r "STRLIST2018_06_30\ Start0", r "STRLIST2018_05_30\ Start1", r "STRLIST2018_07_30\ Start1" R "STRLIST2018_06_01\ Start1"] print ("Is not sorted-") for i in strList: print (I) strList = sorted (strList,key = functools.cmp_to_key (cmp)) print ("Has sorted-") for i in strList: print (I)
The above is a small implementation of custom sorting, for custom sorting, this Xiaobai is mainly used for sorting arrays of custom structures, dictionaries, etc., and will be used in more places in the future.
String comparison cmp op.eq
Python3 no longer uses cmp (str1,str2) to compare strings
Replaced by operator module, you need to import the module.
If you use cmp directly, an error will occur.
How to check my python version? mine is windows.
Command: (note that V must be capitalized)
Python-V
The python3 comparison string is as follows
This is the end of the introduction to "methods for comparing Python3 strings and rewriting cmp functions". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.