trivialstuff

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
jsondecode()in the matlab environment. jsondecode() load the content from file into matlab own data structure
. In json.dumps()‘s’ represents the ‘string’, and it export a json-formated string. The common usage should be json_str = json.dumps()
matlab's counterpart, a little bit controversial

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 NoneDo not care why.

Iterate over multiple lists

See Link1.
Note: it is a reference.

List comprehension within nested ‘for’ loops and multiple conditionals

Format of complex the list comprehension.Image is quoted
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()anddelmethod does not have the return values.

Traverse the keys of a dictionary

Collect keys into a list

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 mapfunction 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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值