Python 序列和字典

序列(Sequences)主要包括列表(Lists)、元组(Tuple)和字符串(Strings),其中字符串和元组是不可修改的。

序列通用操作

定义

1
ver = 'python3.6'

索引访问

1
2
3
4
5
ver[5]
> 'n'

ver[-1]
> '6'

slice

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
ver[0:6]
> 'python'

ver[0:-3]
> 'python'

ver[:6]
> 'python'

ver[6:]
> '3.6'

ver[:]
> 'python3.6'

# 步长为2
ver[0::2]
> 'pto36'

ver[6::-2]
> '3otp'

concatenation
注意:不同数据类型不能拼接

1
2
ver[:6] + '-' + ver[6:]
> python3.6

repetition

1
2
3
4
5
ver[:-1] + ver[-1:] * 2
> python3.66

# 创建固定长度空序列
[None] * 12

是否存在

1
2
3
4
5
'3' in ver
> 'True'

3 in [1, 9, 3]
> 'True'

长度、最小值、最大值

1
2
3
4
5
6
7
8
len(ver)
> '9'

max(ver)
> 'y'

min(1, 9, 0)
> '0'

列表

定义

1
2
3
4
5
6
7
ct = ['Peru', 'India', 'Japan', 'Iran']

# 可嵌套
dot = [[1, 1], [1, 2], [1, 3]]

# 字符串转换为列表
list('python')

修改

1
2
ct[2] = 'Korea'
> ['Peru', 'India', 'Korea', 'Iran']

删除

1
2
del ct[1]
> ['Peru', 'Japan', 'Iran']

分片赋值

1
2
3
4
5
6
7
8
ct[2:] = ['Chain']
> ['Peru', 'India', 'Chain']

ct[:2] = []
> ['Japan', 'Iran']

ct[::2] = ['Russia', 'Germany']
> ['Russia', 'India', 'Germany', 'Iran']

追加

1
2
ct.append('America')
> ['Peru', 'India', 'Japan', 'Iran', 'America']

count

1
2
ct.count('Iran')
> '1'

list.extend(iterable)
等效于a[len(a):] = iterable.

1
2
ct.extend(['Egypt', 'Italy'])
> ['Peru', 'India', 'Japan', 'Iran', 'Egypt', 'Italy']

index

1
2
ct.index('Peru')
> '0'

insert

1
2
ct.insert(1, 'Canada')
['Peru', 'Canada', 'India', 'Japan', 'Iran']

pop

1
2
3
4
5
ct.pop()
> ['Peru', 'India', 'Japan']

ct.pop(0)
> ['India', 'Japan', 'Iran']

remove

1
2
ct.remove('India')
> ['Peru', 'Japan', 'Iran']

reverse

1
2
ct.reverse()
> ['Iran', 'Japan', 'India', 'Peru']

sort

1
2
ct.sort()
> ['India', 'Iran', 'Japan', 'Peru']

元组

定义

1
fruit = ('apple', 'banana', 'mango')

列表转换为元组

1
2
tuple(['Perl', 'Python', 'Java'])
> ('Perl', 'Python', 'Java')

分片

1
2
fruit[1:]
> ('banana', 'mango')

字典

字典是python中唯一内建的映射(mapping)类型

创建字典

1
emails = { '163': 'xiongjiaxuans@163.com', 'google': 'xiongjiaxuans@gmail.com', 'microsoft': 'lanewf@live.com' }

通过key访问

1
2
3
4
5
emails['google']
> 'xiongjiaxuans@gmail.com'

emails.get('qq', 'no')
> 'no'

长度

1
2
len(emails)
> '3'

key是否存在

1
2
'163' in emails
> 'True'

中国事故

2021年

2020年

2019年

2016年

2015年

2014年

2011年

2010年

2008年

2007年

2006年

2005年

2004年

2003年

参考

JavaScript 内置对象

Object

object sort by value

1
2
3
4
5
6
7
const obj = {
'us': {'population': 331449281},
'jp': {'population': 125360000},
'in': {'population': 1352642280}
}

Object.keys(obj).sort((a, b) => obj[a]['population'] - obj[b]['population']).map(item => obj[item])

注意

用数字作为对象的key,内部会自动转换为string

Array

concat

concat(value0, value1, ... , valueN)

1
2
3
4
5
6
const array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
const array3 = array1.concat(array2);

console.log(array3);
// expected output: Array ["a", "b", "c", "d", "e", "f"]

map

map(function callbackFn(element, index, array) { ... }, thisArg)

1
2
3
4
5
6
7
const array1 = [1, 4, 9, 16];

// pass a function to map
const map1 = array1.map(x => x * 2);

console.log(map1);
// expected output: Array [2, 8, 18, 32]

filter

filter(function callbackFn(element, index, array) { ... }, thisArg)

1
2
3
4
5
6
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter(word => word.length > 6);

console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]

参考

Standard built-in objects Array
Array.prototype.concat()
Array.prototype.map()
Array.prototype.filter()

字体

Terminology

基线(baseline)

  • 大写字母H或小写字母n底部的假象线,O、V、W等字母底部会稍微超出基线。
  • 一般中文都超出基线。

大写字高(cap height)
指H或E等直线型大写字母从基线到字母顶部的高度,O、A等字母的顶部会稍微超出大写字高线。

x字高(x-height)
没有上下延伸的x高度。

升部(ascender)
小写字母b、d、f、l等高出x字高的部分称为升部,升部线比大写线高。

降部(descender)
小写字母g、j、p、y等低于基线的部分称为降部,

全角(em)
半角en

字间距(letter spacing)
字母间的空隙

字偶间距(kerning)
To TH

合字
书面排版fi, fl

双元音字
Æ, Œ
List of words that may be spelled with a ligature

Eszett
德文字母ß

ampersand
形状来自et

参考
Design With FontForge: What Is A Font?
PrintWiki

行高

typeface

Times New Roman

Times New Roman is a serif typeface. It was commissioned by the British newspaper The Times in 1931 and conceived by Stanley Morison, the artistic adviser to the British branch of the printing equipment company Monotype, in collaboration with Victor Lardent, a lettering artist in The Times’s advertising department. It has become one of the most popular typefaces of all time and is installed on most personal computers.

宋体和仿宋

latex-chinese-fonts