Fragmentary knowledge (continually updated)

0x00: Introduction

Personal programming, but often encountered piecemeal knowledge-based, easy to forget, always repeat Baidu waste of time, here to record it.

Reference links

 

0x01: Content

Theory classes:

The difference between (1) import and from import of

Note the difference in import python and from import of
First clear: Try not for the easy use from xxx import *
 
There are two ways in python import module, one is import xxx, the other is from xxx import yyy, the difference between them is that the first introducing only one module, and the module executed again, if __main__ = = "__ main__" there is no execution.
Meanwhile, for introducing variable in the current namespace, import module requires the use of variables, functions, etc. by way of xxx.yyy;
The second module will be in the variable yyy imported in the current namespace, so you can call directly to yyy use, when to use this import method, you need to pay attention to the current namespace if there are duplicate names, from xxx import * this try not to use way, because it undermines the management of the namespace.
 
ps: from xxx import * When using a single property can not be imported in order to protect the leading underscore and private property begins with a double-underlined

(2) the string sections

String [start: stop]
str [-1] the last character
 
With steps
String [start: stop: stride]

 

The actual programming categories:

(1) Type Conversion

1)bytes     str
b1 = b'Hello'
s1 = 'Hello'
print(type(b1))
print(type(s1))
# Bytes type to type str
Method # 1 str () function
s2 = str(b1, encoding="utf-8")
 
Method # 2 bytes.decode () function
s3 = bytes.decode(b1)
2)list    str
  • List to string
      list = ['a','b','c']
      result = ''.join(list)
  • The values ​​in the list are converted into a type
[str(i) for i in list]
  • TO string list
list(str)

(2) add elements to the list

  • list.append (single element): increase in a list at the end of the list element;
  • list.extend ([element 1, element 2]): increase the number of list elements in the list ends;
  • list.insert (element number, element): Add a list element in an arbitrary position list

(3) intersection of the two lists

list(set(a).intersection(set(b)))
#method one:
a=[2,3,4,5]
b=[2,5,8]
tmp = [val for val in a if val in b]
print tmp
#[2, 5]
# Method two print list (set (a) .intersection (set (b)))

Method two faster

(4) Reverse string

# _*_  coding:utf-8 _*_
#方法一
str = "hello world"
print(str[::-1])
#方法二
from functools import reduce
print(reduce(lambda x,y:y+x,str))

Flip string, first the simplest method is to use a slice operation, flip achieved, secondly reduce function may be utilized to implement flip, reduce function needs to import from the functools in python3.

After reversing the string, it can be used to determine whether a palindrome

(5) capitalization

= str1 " Hell world " 
str2 = " UP " 
Print (str1.title ()) # of each word capitalized 
Print (str1.capitalize ()) # whole string capitalized 
Print (str1.upper ()) # All capitalized 
Print (str2.lower ()) # all lowercase letters

 (6) splitting string

= Str3 " the I Love the Python " 
str3_1 = " the I / Love / the Python " 
str3_2 = "     the I Love the Python     " 
Print (str3.split ()) # default split on spaces, returns a list of 
Print (str3_1.split ( ' / ' ))
 Print (str3_2.strip ()) # default string clear space is removed, return the string

Split string split function can be used directly, be implemented, returns a list, and the function for removing the strip head and tail specified character string (default space or line feed).

(7) are combined into a list of strings

list1 = ['I','Love','Python']
print(' '.join(list1))
print('++'.join(list1))

 

This can be considered an article counterexample 6, where the string is a string merge list. Article 6 may be combined with Article 7, item character string you do not want to remove the left.

 (8) to find the only element in the string

str4 = "PPPPyyyttthhhnnnnoooo"
print(''.join(set(str4)))

list1 = [2,2,0,66,100,-1,1,1,1]
print(list(set(list1)))

In python, for the screening of unique values, we should first think to use set of set of screening can help us to quickly duplicate elements, the above program, set not only for strings, but also for the screening list .

(9) the list of the list expanded

 

 First, the method 1, we call that iteration_utilities in deepflatten function, the second method directly using recursive method, to achieve our own complex list of flattening, you can get a list of the expansion.

 

Guess you like

Origin www.cnblogs.com/liqik/p/12452471.html