Python notes (2)
- 2020-04-02 09:36:00
- OfStack
Continue to List:
Delete elements:
To empty the list
List is used as a stack (last in, first out) :
Use negative index:
"+" combination list:
Find the number of elements in the list:
Sort the list:
Function pointer problem:
The parameters in the function are:
* parameter name: represents an arbitrary number of parameters
** : represents the dictionary parameter
Control statement:
IF:
FOR:
WHILE:
Pass: empty action statement
Dictionary: data structure for key-value pairs
Use list to construct a dictionary:
Interesting comparison:
The content is relatively miscellaneous, learn what write down. Make full use of the spare time and spare time in the work to complete, more fulfilling.
Delete elements:
a =[1, 2, 3, 4]
a[2:3] = [] #[1, 2, 4]
del a[2] #[1, 2]
To empty the list
a[ : ] = []
del a[:]
List is used as a stack (last in, first out) :
stack = [3, 4, 5]
stack.append(6)
stack.append(7)
stack.pop() # 7
stack.pop() # 6
stack.pop() # 5
Use negative index:
b=[1, 2, 3, 4]
b[-2] #3
"+" combination list:
end = ['st', 'nd'] + 5*['th'] + ['xy'] # ['st', 'nd', 'th', 'th', 'th', 'th', 'th', 'xy']
Find the number of elements in the list:
lst.('hello') # hello The number of
Sort the list:
sort()
# Sort the elements of the list appropriately.
reverse()
# Invert the elements in the list
Function pointer problem:
def f2(a, L=[])
L.append(a)
return L
print(f2(1)) # 1
print(f2(2)) # 1 . 2 L At this function call [1]
print(f2(3)) # 1 . 2 . 3
The parameters in the function are:
* parameter name: represents an arbitrary number of parameters
** : represents the dictionary parameter
Control statement:
IF:
if x < 0:
x = 0
print 'Negative changed to zero'
elif x == 0:
print 'Zero'
elif x == 1:
print 'Single'
else:
print 'More'
FOR:
a = ['cat', 'window', 'defenestrate']
for x in a:
print x, len(x)
WHILE:
a, b = 0, 1
while b < 1000:
print b,
a, b = b, a+b
#1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
Pass: empty action statement
while True:
pass
Dictionary: data structure for key-value pairs
Use list to construct a dictionary:
items = [('name', 'dc'), ('age', 78)]
d = dict(items) #{'age': 78, 'name': 'dc'}
Interesting comparison:
x = [] #list
x[2] = 'foo' # error
x = {} #dictionary
x[2] = 'foo' # correct
The content is relatively miscellaneous, learn what write down. Make full use of the spare time and spare time in the work to complete, more fulfilling.