2.remove
A.remove(“what”) #通过元素名称,移除
3.del
del A[2] #索引移除
**
**
1、reverse
A.reverse() #列表翻转 例如:
[3,1,‘what’]变为[‘what’,1,3]
**
**
1、count
C=[‘Hello’,‘Google’,‘Hello’,‘baidu’,
‘Hello’,‘baidu’,‘mofa’,‘guiyi’]
print(count(‘Hello’))
结果: 3
**
**
1、sort
D=[2,8,44,5,12,1]
D.sort()
=================================================================
定义元组
ll=(1,2,3)
print(ll)
print(ll.type())
结果 : (1,2,3)
<class ‘tuple’>
注:如果元组中只有一个元素,则这个元组后面必须要有一个",",否则元素就还是原来的类型
例如 : A=(1,)
正确
是元组
A=(1)
****错误
不是元组
**
**
由于元组不能修改,所以元组也不能删除部分元素,要删除只能删除整个元组
1、count
C=(‘Hello’,‘Google’,‘Hello’,‘baidu’,
‘Hello’,‘baidu’,‘mofa’,‘guiyi’)
print(count(‘Hello’))
结果: 3
**
**
A=(‘cal’,‘what’,1,‘2020-1-1’,‘join’)
print(“what index is:”,A.index(“what”))
结果是 : what index is:1
**
=================================================================
**
字典定义:字典类型就和他的名字一样,可以向查字典一样去找,其他语言也有类似的类型。例如:Java中的HashMap , PHP中的Array等
chinese={“we”:“我们”,
“are from”:“来自”,
“China”:“中国”
}
print(chinese)
print(“China:”,chinese[“China”]) #键查找
结果: {‘we’: ‘我们’, ‘are from’: ‘来自’, ‘China’: ‘中国’}
China:中国
注意:字典的键必须是唯一的,不重复的,如果是空字典,直接用{}表示
empty={}
print(type(empty))
结果 : <class ‘dict’>
**
**
chinese={“we”:“我们”,
“are from”:“来自”,
“China”:“中国”
}
print(chinese)
chinese[“China”]=“未来”
print(chinese)
结果:{‘we’: ‘我们’, ‘are from’: ‘来自’, ‘China’: ‘中国’}
{‘we’: ‘我们’, ‘are from’: ‘来自’, ‘China’: ‘未来’}
**
**
chinese={“we”:“我们”,
“are from”:“来自”,
“China”:“中国”
}
print(chinese)
del chinese[“China”]
print(chinese)
**
**
chinese={“we”:“我们”,
“are from”:“来自”,
“China”:“中国”
}
print(chinese)
chinese[“China”]=“未来”
del chinese[“China”]
chinese.clear()
print(chinese)
**
**
用于修改复制的字典,相当于复制一个新的字典作为修改,原有的字典不变
chinese={“we”:“我们”,
“are from”:“来自”,
“China”:“中国”
}
Chi=chinese.copy()
**
**
SQ={“name”,“age”,“sex”}
student=dict.fromkeys(SQ)
print(student)
student1=dict.fromkeys(SQ,15)
print(student1)
chinese={“we”:“我们”,
“are from”:“来自”,
“China”:“中国”
}
print(chinese.get(“we”))
**
**
chinese={“we”:“我们”,
“are from”:“来自”,
“China”:“中国”
}
print(chinese.keys())
**
**
chinese={“we”:“我们”,
“are from”:“来自”,
“China”:“中国”
}
print(chinese.values())
**
**
chinese={“we”:“我们”,
“are from”:“来自”,
“China”:“中国”
}
print(chinese.items())