0x00: Introduction
Personal programming, but often encountered piecemeal knowledge-based, easy to forget, always repeat Baidu waste of time, here to record it.
0x01: Content
Theory classes:
The difference between (1) import and from import of
(2) the string sections
The actual programming categories:
(1) Type Conversion
1)bytes str
2)list str
-
List to string
-
The values in the list are converted into a type
-
TO string list
(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
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.