Python中定义list:
classmates = ['ZHANG', 'LI', 'WANG']
scores = [95, 75, 85]
定义二维的list:
L = [
['Apple', 'Google', 'Microsoft'],
['Java', 'Python', 'Ruby', 'PHP'],
['Adam', 'Bart', 'Lisa']
]
访问二维list中的元素:
print(L[0][0])
print(L[1][1])
print(L[2][2])
与C++中的二维数组很像啊。
Python中定义Dict:
mydict = {'ZHANG':95,'LI':75,'WANG':85}
访问Dict:
print(mydict['ZHANG'])
结果:
95
Key增删改:
mydict['NewAdd'] = 100
print(mydict['NewAdd'])
mydict.pop('WANG')
mydict['ZHANG'] = 88
print(mydict['ZHANG'])
Python定义Set:
Set的程序必须是list,如下所示:
两个Set的交集和并集:class1 = set(['ZHANG','LI','WANG']) class1.add('NewStudent') print(class1) class1.remove('NewStudent') print(class1)
结果显示:class2 = set(['ZHANG','ZHANG2','LI2','WANG2']) classor = class1|class2 print(classor) classand = class1&class2 print(classand)
{'LI2', 'ZHANG2', 'WANG2', 'ZHANG', 'LI', 'WANG'} {'ZHANG'}
清空:
结果(空的set):class1.clear() print(class1)
set()