This article mainly introduces Python easy to ignore the knowledge point, the example analysis is easy to ignore in python the common operation skill, the need friend may refer to the
Here's a record of the little things that are easy to ignore in Python
I. Input (...) and Raw_input (...)
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The |
|
PS: In the later version of python3.0, raw_input and input fit, cancel the raw_input, and input instead, so now the version of input received is a string
Two, Python three mesh operator
Although Python does not have C + + 's three-mesh operator (?:), there are similar alternatives,
That's
1. True_part If condition else false_part
?
1 2 3 4 5 6 7 8 |
>>> 1 If True else 0 1 >>> 1 if False else 0 0 >>> ' true ' if true else False ' true ' >>& Gt "True" if True else "False" ' Falser ' |
2. (Condition and [True_part] or [False_part]) [0]
?
1 2 3 4 5 |
>>> (True and [true] or ["false"]) [0] ' true ' >>> (False and ["true"] or ["false"]) [0] ' false ' >> > |
Third, get the specified string in the entire string appears the nth time index
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16-17 |
#-*-coding:cp936-*-def findStr (String, SubStr, findcnt): Liststr = A.split (substr,findcnt) If Len (liststr) <= find Cnt:return-1 return len (String)-len (Liststr[-1])-len (subStr) #test a = "12345 (1) 254354 (1) 3534 (1)" Sub = "(1)" N = 2 #查 Find the 2nd time position print findStr (a,sub,n) N = #查找第10次出现的位置 print findStr (a,sub,n) #结果 #>>> #14 #-1 |
Four, enumerate usage:
When traversing a sequence, you may need to use both the index and the corresponding value of the sequence, which can be traversed by the enumerate method.
Enumerate's description is as follows:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
>>> Help (enumerate) Help on class enumerate in MoD Ule __builtin__: Class Enumerate (object) | Enumerate (iterable[, start])-> iterator for index, value of Iterable | | Return an enumerate object. Iterable must be another object supports | Iteration. The enumerate object yields pairs containing a count (from | start, which defaults to zero) and a value yielded by the ITE rable argument. | Enumerate is useful to obtaining an indexed list: | (0, Seq[0]), (1, seq[1]), (2, seq[2]), ... | | Methods defined here: | | __getattribute__ (...) | x.__getattribute__ (' name ') <==> X.name | | __iter__ (...) | X.__ITER__ () <==> iter (x) | | Next (...) | X.next ()-> the next value, or raise Stopiteration | | ---------------------------------------------------------------------- | Data and Other attributes defined: | | __new__ = <built-in method __new__ of type object> | t.__new__ (S, ...)-> a new object with type S, a subtype of T |
Methods of traversing sequences
?
1 2 3 4 5 6 7 |
>>> list = [' A ', ' B ', ' C '] >>> for index, value in enumerate (list): Print index, value 0 a 1 B 2 c >> ;> |
Six, using the Python random module's sample function randomly select a group of elements from the list
?
1 2 3 4 5 6 |
Import List = [1, 2, 3, 4, 5, 6, 7, 8, 9, ten] slice = Random.sample (List, 5) #从List中随机获取5个元素, return print slice print Li as a fragment St #原有序列并没有改变. |
Print a list dictionary containing Chinese in JSON, etc.
?
1 2 3 4 5 6 |
#-*-coding:utf-8-*-import json #你的列表 lista = [{' Path ': [' [AWS] Xe7xbex8exe5xb0x91xe5xa5xb3xe6x88x98xe5xa3xab sailor Mo On Crystal-moon Pride Mv[big5][bdrip 1080p x264 aac][6e5cfe86].mp4 '], ' length ': 131248608L}, {' Path ': [' [AWS] Xe7xbex8ex E5xb0x91xe5xa5xb3xe6x88x98xe5xa3xab sailor Moon crystal-moon Pride Mv[big5][bdrip 720p x264 '], ' aac][639d304a].mp4 Th ': 103166306L}, {' Path ': [' [AWS] xe7xbex8exe5xb0x91xe5xa5xb3xe6x88x98xe5xa3xab sailor Moon crystal-moon Pride Mv[big5 ][bdrip 480p x264 aac][5a81baca].mp4 '], ' length ': 75198408L}] #打印列表 print json.dumps (lista, encoding= ' UTF-8 ', ensure_ Ascii=false) |
Output results:
?
1 2 |
>>> [{"Path": ["[AWS] American maiden warrior sailor Moon Crystal-moon Pride Mv[big5][bdrip 1080p x264"], "Len Gth ": 131248608}, {" Path ": [[AWS] American maiden warrior sailor Moon crystal-moon Pride mv[big5][bdrip 720p x264 aac][639d304a].mp4"], " Length ": 103166306}, {" Path ": [[AWS] American maiden warrior sailor Moon crystal-moon Pride mv[big5][bdrip 480p x264 aac][5a81baca].mp4"] , "Length": 75198408}] |
I hope this article will help you with your Python programming.