python-matlab comparison
- Target
- Linux related
- Python related
- stdout in tabular form
- Three fundamentals in a python object
- Delete a file using python
- json package(1)
- json package(2)
- Regarding '='
- Regarding 'None'
- Iterate over multiple lists
- List comprehension within nested 'for' loops and multiple conditionals
- Remove an element from a list(not index- necessary)
- Remove an element from a list(index- necessary)
- Traverse the keys of a dictionary
- Skip over a value in the range function
- The weird 'round'
- Great 'zip'
- Markdown related
Target
This post is aimed to mark some common knowledge notes that I employ when coding but is too trivial to keep in mind. I have realized that to make outcome activately is pretty important for studying.
Linux related
Copy a folder VS. Copy contents of a folder
credit to link-1
Python related
stdout in tabular form
link-table
Note the last example, i.e., PrettyTable, is used in your case.
Three fundamentals in a python object
id(identity)、type(data type)、value(data value);
Delete a file using python
os.remove(path)
json package(1)
All of the first, one should open a file first:
import json
with open(==path==,'w+') as fp:
json.load() # or json.dump
The highlight part is replaced by your own file path.‘w+’ is important for json.dump()in case you did not create a json file in advance.
json package(2)
The difference b.t. json.dump()
and json.dumps()
is in a sense like the. In
jsondecode()
in the matlab environment. jsondecode() load the content from file into matlab own data structurejson.dumps()
‘s’ represents the ‘string’, and it export a json-formated string. The common usage should be json_str = json.dumps()
Regarding ‘=’
As we all know in python ‘=’ just means to pass a reference to another variable other than copying vlaues. Nevertheless, it seems if operate variables by function (list(),.append()), the value change of a variable does not affect other vriables’ value. Instead, the arithmetic operation including +,-,*,/ just makes it synchronous for each variable’s values.
Reference to Link0, along with Link0.5
Regarding ‘None’
To check if a path is existed or not, just use is if somepath is None
Do not care why.
Iterate over multiple lists
See Link1.
Note: it is a reference.
List comprehension within nested ‘for’ loops and multiple conditionals
All the for above is gradually streched from outer to inner. The essence is to iterate and filter the elements
Example 1 transpose a matrix
#-*-encoding:utf-8-*-
matrix1 = [
[1, 2, 9, 4],
[5, 6, 7, 8],
[9, 52, 6, 12]
]
matrix2 = [[row[i] for row in matrix1] for i in range(len(matrix1[0]))]
Comments: This is a good way of changing precedance by using brackets!!
Example 2 aggregate sequences by length
lens = np.unique([len(x) for x in X])
ldict = {l : [[x,y] for x,y in zip(X, y) if (len(x) == l)] for l in lens}
Note the output key is multiple lists without outer [] bracket.
Example 3 (quoted)
One final, more complex example: Let’s say that we have a list of lists of words and we want to get a list of all the letters of these words along with the index of the list they belong to but only for words with more than two characters. Using the same for-loop syntax for the nested list comprehensions we’ll get:
strings = [ ['foo', 'bar'], ['baz', 'taz'], ['w', 'koko'] ]
[ (letter, idx) for idx, lst in enumerate(strings) for word in lst if len(word)>2 for letter in word]
[('f', 0), ('o', 0), ('o', 0), ('b', 0), ('a', 0), ('r', 0), ('b', 1), ('a', 1), ('z', 1), ('t', 1), ('a', 1), ('z', 1), ('k', 2), ('o', 2), ('k', 2), ('o', 2)]
Remove an element from a list(not index- necessary)
flag = 2
a.remove(flag)
print(a) #[0,1,3,4,5]
Remove an element from a list(index- necessary)
del a[index]
or del a[begin:end]
Note remove()
anddel
method does not have the return values.
Traverse the keys of a dictionary
Skip over a value in the range function
# Assume 'flag' is the one you wanna skip..
ls = list(range(number))
ls.remove(flag)
for i in ls:
...
The weird ‘round’
A great discovery for round()
method in python that goes:
While rounding the input number to an integer, if both round up and round down values are equally close then even number is returned. For example, 10.5 will be rounded to 10 whereas 11.5 will be rounded to 12.
Great ‘zip’
See Link2.
But note in the content of the link the map
function should be further surrounded by list()
otherwise the output is a map object:
list(map(list,zip(*a)))
Markdown related
Markdown on CSDN
Post a picture
After posting a picture via the silly drag functionality, one should identify the ‘.PNG’ as the trail of the picture link and then append the ‘#pic_center=x300’ to the link trail. Notably, ‘x’ is the letter ‘x’ in the lowercase rather than ‘*’ or the symbol ‘×’ typed by some chinese input methods.