#列表: Lists can be nested.
Word=[' A ', ' B ', ' C ']
A=WORD[2] #通过索引访问列表
Print "A is:" +a
A is:c
Word=[' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' G ']
B=word[1:3] #取从1开始到3前面的元素
Print B
[' B ', ' C ']
B=word[1:2]
Print B
[' B ']
Print Word[1:4] #取从1开始到4前面的元素
[' B ', ' C ', ' d ']
Print Word[2:3]
Word[-1]#-1 Table Countdown
Aa=a[-4:-1] #倒数截取, the result is a row
Delete the first value of a list
Del Sample_list[0]
Insert a value in the list
SAMPLE_LIST[0:0] = [' Sample value ']
Get the length of the list
List_length = Len (sample_list)
List traversal
For element in Sample_list:
Print (Element)
Method of List
L.append (Var) #追加元素
L.insert (Index,var)
L.pop (Var) #返回最后一个元素 and removed from the list
L.remove (Var) #删除第一次出现的该元素
L.count (Var) #该元素在列表中出现的个数
L.index (Var) #该元素的位置, no exception thrown
L.extend (list) #追加list, that is, merge list to L
L.sort () #排序
L.reverse () #倒序
List operator:, +,*, Keyword del
A[1:] #片段操作符, for the extraction of sub-list
[1,2]+[3,4] #为 [1,2,3,4]. With Extend ()
[2]*4 #为 [2,2,2,2]
Del L[1] #删除指定下标的元素
Del L[1:3] #删除指定下标范围的元素
Copy of List
L1 = L #L1为L的别名, in C is the same pointer address, the L1 operation is the operation of L. This is how the function arguments are passed.
L1 = l[:] #L1为L的克隆, that is, another copy.
Tuples: tuples are immutable, tuples can be nested
Atuple= (the "Hello")
Atuple
(1, 2, 3, ' hello ')
ATUPLE[0]
1
ATUPLE[-1]
' Hello '
Atuple[2:]
(3, ' hello ')
Atuple[:2]
(1, 2)
Atuple[0:3]
(1, 2, 3)
Atuple[0]=5 #出错
For element in Atuple:
Print element
#字典: There is no order for key/value pairs in the dictionary
X={' a ': ' AAA ', ' B ': ' BBB ', ' C ': 12}
Print x[' a ']
Aaa
Print x[' C ']
12
From Python version 2.2
Fdict = Dict ([' X ', 1], [' Y ', 2])
You can use a number, a tuple as a key, but not a list, a dictionary as a key, and a key that must be an immutable element.
A={1:11}
B={2:22}
x[a]=11111# Error
x[b]=22222
Dictionary additions:
x[' d ']= ' DDD '
#字典修改
x[' d ']= ' dddd '
#字典删除
#del x[' a ']
#x. Clear ()
B=x.pop (' B ')
For key in x:
Print ("%s=%s"% (Key,x[key]))
#----------------Set-----------------------
Set
Set is like pulling the key out of the dict, similar to a list, but the content cannot be duplicated, creating by calling the Set () method
s = set ([' A ', ' B ', ' C '])
S.add (' Hello ')
S.add (' SS ')
S.add (' Hello ')
S.remove (' Hello ')
S.update (' ZW ') #是把要传入的元素拆分, as an individual passed into the set
For key in S:
Print key
#---------------Convert each other----------------------
#1, Dictionaries
Dict = {' name ': ' Zara ', ' age ': 7, ' class ': ' First '}
#字典转为字符串, return: <type ' str ' > {' age ': 7, ' name ': ' Zara ', ' class ': ' First '}
Print type (str (dict)), str (DICT)
#字典可以转为元组, return: (' age ', ' name ', ' class ')
Print tuple (dict)
#字典可以转为元组, return: (7, ' Zara ', ' first ')
Print tuple (dict.values ())
#字典转为列表, return: [' age ', ' name ', ' class ']
Print List (dict)
#字典转为列表
Print dict.values ()
#2, tuples
tup= (1, 2, 3, 4, 5)
#元组转为字符串, return: (1, 2, 3, 4, 5)
Print tup.__str__ ()
#元组转为列表, return: [1, 2, 3, 4, 5]
Print List (TUP)
#元组不可以转为字典
#3, List
Nums=[1, 3, 5, 7, 8, 13, 20];
#列表转为字符串, return: [1, 3, 5, 7, 8, 13, 20]
Print str (nums)
#列表转为元组, return: (1, 3, 5, 7, 8, 13, 20)
Print tuple (nums)
#列表不可以转为字典
#4, String
#字符串转为元组, return: (1, 2, 3)
Print tuple (eval ("())")
#字符串转为列表, return: [1, 2, 3]
Print List (eval ("(+)"))
#字符串转为字典, return: <type ' dict ' >
Print type (eval ("{' Name ': ' Ljq ', ' Age ': 24}"))
Python Basics 2