Common list methods in Python3 (method)
See: >>> Help (list)
See document: Python_base_docs/list_xxxx.html
Deep copy and shallow copy
Shallow copy shallow copy
Example:
L = [3.1, 3.2]
L1 = [1, 2, L] # L1 = [1, 2, [3.1, 3.2]]
L2 = L1.copy () # shallow copy L2 = [1, 2, [3.1, 3.2]]
Print (L1)
Print (L2)
L1[2][0] = 3.14 # change the No. 0 element in the L-list
Print (L1)
Print (L2)
Deep copy
A deep copy is a copy of an object's associated objects when the object is copied.
Example:
Import Copy # Importing a replication module
L = [3.1, 3.2]
L1 = [1, 2, L] # L1 = [1, 2, [3.1, 3.2]]
L2 = Copy.deepcopy (L1) # deep copy
Print (L1)
Print (L2)
L1[2][0] = 3.14 # change the No. 0 element in the L-list
Print (L1)
Print (L2)
Note:
A deep copy is usually copied only for objects that contain mutable objects, and immutable objects are usually the same
List and string comparisons:
Lists and strings are sequences, with sequential relationships between elements
A string is an immutable sequence, and a list is a mutable sequence.
Each element in a string can be a character only, and a list can store any type of element
Lists and strings are iterative objects
String literal parsing method split () and join ()
S.split (sep=none) uses Sep as a delimiter to split s strings, returning a segmented list of strings, separated by white space characters as separators when no arguments are given.
S.join (iterable) returns an intermediate string separated by S with a string in an object that can be iterated
Note:
s represents a string
Example:
s = "A,BB,CCC,DDDD"
L = s.split (', ') # l = [' A ', ' BB ', ' CCC ', ' DDDD ')
Gets the number of words entered by the user in English:
s = input ("Please Enter:") # "Welcome to Beijing"
L = S.split () # No arguments will be delimited with white space characters
Print (len (L)) # 3
Path = ["C:", "Programe Files", "Python3"]
' \ '. Join (PATH) # "C:\Programe Files\python3"
Practice:
There is a string "hello", using this string to generate "H e l o" and ' H-e-l-l-o '
s = "Hello"
". Join (s)
There are some numbers in the list. such as:
L = [1, 3, 2, 1, 6, 4, 2, ..., 98, 82]
Require a number that appears only once in the list to be deposited in another list L2
(10 minutes)
(There are more than a few!)
For x in L: # Remove all elements from the list
If L.count (x) = = 1:
Put it in the list L2
...
...
List Deduction lists comprehension
Role:
Build a list with an iterative object and an expression combination
Grammar:
[Expression for variable in iteration object]
Or
[Expression for variable in iteration object if truth expression]
Thinking:
Enter an integer with n bound to generate a list of squares from 1 to n
Input: 9
Generated [1, 4, 9, 5,2, 62, ..... 81]
Combining lists and for statements
n = Int (input ("Enter:"))
L = []
For x in range (1, n + 1):
L.append (x * * 2)
Print (L)
Practice:
Use the list derivation to generate the following list:
L = [1,4,7,10, .... 100]
Print (L)
Practice:
Enter a starting integer to bind with begin
Enter an ending integer bound with end
All even numbers ending with end (without end) are saved in the list, starting with begin
(It is recommended to complete with list deduction)
List-derived nesting:
Grammar:
[Expression 1 for Variable 1 in iterator object 1 if Truth expression 1
For variable 2 in Iteration object 2 if truth expression 2]
Example:
There are two of lists:
L1 = [2,3,5]
L2 = [7,11,13]
Generates a new list L3, L3 the elements in L1 and the elements in the L2, respectively, by multiplying the
l= [27, 2, 2, 37, 311, ... 513]
L = [x * y
For x in L1
For y in L2]
Print (L)
Practice:
The following list is generated using a list-derived nesting:
The following list is generated with the string "ABC" and "123":
["A1", "A2", "A3", "B1", "B2", "B3", "C1", "C2", "C3"]
Tuple tuples
Defined:
Tuples are immutable sequences, like lists, where tuples can hold any type of element
How to represent a tuple:
Enclosed in parentheses (), a single element is enclosed with a comma (,) that distinguishes a single object or tuple
Create a literal value for an empty tuple
t = () # Empty tuple
To create a literal value for a non-empty tuple:
t = 200,
t = (200,)
t = (--)
t = 100, 200, 300
Type (x) function to return the types of x
Tuple construct (create) function tuples
Tuple () generates an empty tuple, equivalent to ()
Tuple (iterable) generates a tuple with an iterative object
Example:
t = tuple () # equals t = ()
t = Tuple (range (5)) # t = (0,1,2,3,4)
t = tuple ("ABCD")
t = tuple ([5,6,7,8])
Operations of tuples:
Arithmetic operations:
+= =
Rule same as List and string
Comparison operation:
< <= > >= = = =!
Rule same as List and string
In/not in operation
Determines whether an element exists within a tuple, or returns true if one exists, otherwise false
Indexed index
The usage is equivalent to the index of the list
Tuple cannot index assignment
Sliced slice
Use a slice that is equivalent to a list
The slice of the tuple returns a new tuple
Tuples cannot be slice-assigned
Tuple's method:
T represents a tuple:
T.index (V[,begin[,end]) returns the index subscript for the corresponding element, begin index, end index, and trigger ValueError error when V does not exist
T.count (x) returns the number of corresponding elements in a tuple
See: >>> Help (tuple)
Three of the five types of sequences:
Str
List
Tuple
Summary of sequence-related functions:
Len (SEQ) returns the length of the sequence
Max (x) returns the maximum element of a sequence
MIN (x) returns the minimum element of a sequence
SUM (x) returns all the elements in the sequence and
Any (x) truth test, as long as one is true
All (x) truth test, all elements are true to True
STR (obj) serializes the object obj to a string
List (iterable) uses an iterative object to generate a listing
Tuple (iterable) uses an iterative object to generate an element
Reversed (SEQ) returns an iterator object in reverse order
Sorted (iterable, Reverse=false) returns the sorted list
Practice:
Enter a string arbitrarily:
All whitespace in this string is stripped, resulting in a post-return string:
Such as:
Input: ABC def g< return >
Output: GFEDCBA
(Hint: can be reversed with reversed)
Practice:
- There are some numbers in the list. such as:
L = [1, 3, 2, 1, 6, 4, 2, ..., 98, 82]
Deposit only numbers in the list into another list L2,
Requirements:
Numbers that repeat multiple times only keep one copy in the L2 list
For all primes within 100, the prime number is stored in the list L
Last Print List L
- Figure out the number of daffodils within 100 ~ 1000 (narcissistic numbers)
Narcissus number refers to the number of hundreds of 3 times plus 10 bits of 3 square plus bits of 3 square equals the original number
For example:
153 = 13 + 53 + 3**3
Answer:
153, 370, ...
Common list methods in Python3 (method)