It is easy to record some pythonic methods that are exposed to and seen on the internet for the future.
1. List derivation:
seq_list=[1,2,3,4,5]new_listforif i%2==0]
2. Generator expression (reduces memory footprint)
for in range (0,1000))
3. Powerful built-in function zip that iterates through two iterations of an object. is particularly useful when converting rows to classes. (But it seems that the zip function is slower to handle large data)
list_1 = [1,2,3,4,5,6,7,8= [7,6,5,4,3,2,1] for in Zip (list_1,list_2) Print (Element) # ############## (1,7)(2,6)(3,5)(bis) (5,3)(6,2) (# the extra elements in the list_1 are automatically ignored
4.* used to deconstruct an iterative object
When a function, such as person, needs to pass all the elements in the list person_list as arguments, only the person (*person_list) is required.
5. Parameter *args is used to define the function to accept any number of parameters, all parameters stored in the list named args
defPerson (*args): forNameinchargs:Print("The person is %s"%name)######################Person ('A','B','C','D','E')#Example Onenames = ['A','B','C','D','E'] Person (*names)#use * To deconstruct the names list, as in example a the person isathe Person isbthe person iscthe Person isdthe Person isE
6. Context Manager
With open ('file','r') as FH: for in fh.readlines (): print(line)
___________________________
Use with without using the last method of adding fh.close () in the old version. With the WITH statement, the file is automatically closed when a file is read or an exception occurs halfway through. The Fh.close () method does not close the file normally when an exception occurs (because the code is stopped halfway through, and cannot be performed to Fh.close ())
7. Progress bar control
Import SYS Import Time for in range (1,61): sys.stdout.write ('#'+') ' +'\b\b') sys.stdout.flush () time.sleep ( 0.5)
8. List to go to heavy
a=[1,2,3,4,5,3,2,2,1]b=list (Set (a))
9. Convert nested lists to a single list
a=[[1,2],[3,4],[5,6]]import itertoolsb=list (Itertools.chain.from_iterable (a))
10. List replication
List_a = [= = list_a# This method does not replicate, except that List_a and List_b together point to the same piece of memory, that is, where [] is stored. =list_a[:]
Record elegant Pythonic code