Open ahasusie opened 5 years ago
matrix:
# get matrix dimension
m = [[1,2,3],[4,5,6]]
print len(m)
print len(m[0])
# create a empty matrix
r, c = 2, 3
matrix = [[None]*r for _ in range(c)]
# zip
A = [[1,2,3],[4,5,6]]
B = zip(*A)
C = zip(*A)[::-1]
string
notes:
string[i] = 'c' # wrong!, 'str' object does not support item assignment,
l = list(string) # change string into list
l[i] = 'c'
return ''.join(l)
lists, set, dictionary: mutable number, string, tuple: immutable
-Immutable means that you can't change the content of the string once it's initialized: an immutable string cannot be modified. If you want to modify just one of the characters, you have to create a new string.
-in languages which the string is immutable, you should be careful with the concatenation operation since the string is immutable, concatenation works by first allocating enough space for the new string, copy the contents from the old string and append to the new string.
-If you did want your string to be mutable, you can convert it to a char array
-If you have to concatenate strings often, it will be better to use some other data structures like StringBuilder. The below code runs in O(n) complexity.
build in data structure in python: array(array module): which is very much like list, except that the objects stored in them are constrained, object types are indicated by type code. dynamic array(list), string(Libs/string.py)
basic operations: initialization, data access, modification, iteration, sort