Again the study is a Thu God, the website posted
http://nbviewer.jupyter.org/github/lijin-THU/notes-python/tree/master/
I'm just a little bit familiar with the basics of other programming languages.
Chat 4 List
The addition of the list, which is equivalent to connecting two lists sequentially
= [123= [3.2‘hello‘+= [12.0‘hello‘*2# 列表与整数相乘,相当于将列表重复相加
We know that a string cannot be modified directly, so what if we want to modify one of those types?
It's a lot easier if you convert the list.
>>>S=' abcdef ' #原字符串>>>S1=List(s)#将字符串转换为列表>>>S1 [' A ',' B ',' C ',' d ',' E ',' F ']#列表的每一个元素为一个字符>>>s1[4]=' E ' #将列表中的第5个字符修改为E>>>s1[5]=' F ' #将列表中的第5个字符修改为E>>>s1[' A ',' B ',' C ',' d ',' E ',' F ']>>>S="'. Join (S1)#用空串将列表中的所有字符重新连接为字符串>>>S' AbcdEF ' #新字符串
Full paragraph substitution in list
= [1011121314]a[1:3= [1234]print a
When you modify a fragment that is not contiguous (interval step is not 1), the number of elements must be the same:
Otherwise it will be an error
= [1011121314]a[::2= [123]a[::2= []# 上面这一句 右边为空,所以无法运行
Delete Element
= [1002‘a‘‘b‘‘c‘]del a[0]del a[1:]del a[::2]
Test dependencies
= [1011121314]print10in aprint10notin a# 同样可以用于字符串=‘hello world‘print‘he‘in sprint‘world‘notin s
List.count (object) returns the number of elements
List.index (object) returns an element for the first time
List.append (object) Adds the element object to the list last
List.extend (newlist) Add newlist to the list at the end, and this is the same as List + = NewList
L.insert (IDX, OB) inserts an OB at index IDX, followed by an element that moves back
L.remove (OB) removing elements
L.pop (IDX) removes the element at the index
= [1011121311]# 在索引 3 插入 ‘a‘a.insert(3‘a‘)print= [1011121311]# 移除了第一个 11a.remove(11)print= [1011121311]a.pop(2)
Sorted can not affect the original element
= [1011113112=sorted(a)print aprint b
In addition to the inverse method of a.reverse (), there are the following ways
= [123456= a[::-1]
Describes a deep copy and shallow copy problem, knowing that C + + should be familiar with this concept
The following example should be very obvious.
= [1223= bb[1=99a# 下面这个例子是所谓的深拷贝= [1223]import= copy.copy(b)b[1=99a# 还有一种deepCopy,是比copy更深的拷贝,对于list内部的list也是深拷贝
Chat 5 Tuple and dictionary
Like lists, tuples Tuple are also ordered sequences, but tuples are immutable and () generated.
t = (10, 11, 12, 13, 14)
Tuple of an element
= (10,)type(a)# 下面这种写法其实不是元组= (10)type(a)
In addition to this, A.count (OBT) a.index (obj) is similar to the list of functions
Below is a little comparison of the next tuple and list
Before the test I thought that the tuple speed was faster than the list
%timeit can test a time
%Timeit [1,2,3,4,5,6,7,8,9,Ten, One, A, -, -, the, -, -, -, +, -, +, A, at, -, -]%Timeit (1,2,3,4,5,6,7,8,9,Ten, One, A, -, -, the, -, -, -, +, -, +, A, at, -, -)
fromimport= rand(10000,4= [listforin=tuple(tupleforin values)%forinlist(row)%forintuple(row)
The final conclusion is that the tuple will generate much faster than the list.
The following is the introduction of the next dictionary
For hash purposes, the keys in Python that require these key-value pairs must be immutable , and the value can be any Python object.
synonyms = {}synonyms[] = [ ' changeable ' , ' variable ' , ' varying ' , Span class= "st" > ' fluctuating ' , ' shifting ' , ' inconsis Tent ' , ' unpredictable ' , ' inconstant ' , ' fickle ' , ' uneven ' , ' unstable ' , ' protean ' ] Synonyms[ ' immutable ' ] = [ ' fixed ' , , ' rigid ' , , Span class= "CO" > ' permanent ' , ' established ' , ' carved in stone ' ]synonyms
Dict can also nest Dict, as in the following example
# 定义四个字典= {‘mag‘0.05‘width‘20= {‘mag‘0.04‘width‘25= {‘mag‘0.05‘width‘80= {‘mag‘0.03‘width‘30}# 以字典作为值传入新的字典= {50076030014180: e4}events
In addition to the usual definition methods, dictionaries can also be generated by dict () conversions
=dict( [(‘foozelator‘123), (‘frombicator‘18), (‘spatzleblock‘34), (‘snitzelhogen‘23) ])inventory[‘frombicator‘+=1# 直接更新每种键值inventory
= {}person[‘first‘="Jmes"person[‘last‘="Maxwell"person[‘born‘=1831print= {‘first‘‘James‘‘middle‘‘Clerk‘}person.update(person_modifications)# 除了正常的person的表示,还可以使用
Don't know if a key exists? can try to use
= {}a["one"="this is number 1"a["two"="this is number 2"a.get("one")a.get("three""undefined")# 如果没有这个key,就输出undefined
Dictionary can use pop to delete elements, here and list similar
Careful children's shoes should remember
The list deletion is A.pop (2), which deletes the first few elements
Dict Delete is A.pop ("one"), delete the corresponding key value
a.pop("two"‘not exist‘)# 如果不存在就输出not existdel a["one"]# del和pop同样的功能
D.keys () Returns a list of all keys;
D.values () Returns a list of all values;
D.items () returns a list of tuples consisting of all key-value pairs;
Python Learning Note 3