作者:小晴天9927 | 来源:互联网 | 2023-07-01 18:17
一.list列表扩展的方式有3种后缀扩展list.后输入append2.插入数值list.insert3.合并列表list.extend2.对[cherry,litc
一. list列表扩展的方式有3种
后缀扩展
list.后输入append
2.插入数值
list.insert
3.合并列表
list.extend
2. 对["cherry", "litchi", "strawberry", "mangosteen", "pomelo", "pineapple", "pitaya", "durian"]进行默认排序
fruit_list = ["cherry", "litchi", "strawberry", "mangosteen", "pomelo", "pineapple", "pitaya", "durian"]
fruit_list.sort()
print(fruit_list)
按照第三个字母排序
def sort_func(x):return (x[3])
fruit_list = ["cherry", "litchi", "strawberry", "mangosteen", "pomelo", "pineapple", "pitaya", "durian"]
fruit_list.sort(key=sort_func)
print(fruit_list)
三. dict中所有方法的使用
1.pop移除序列中一个指定元素,并返回该元素的值
dict_y = {int: 1, bytes: 2+2j}
dict_y.pop(int)
print(dict_y)
2.clear清除全部元素。
dict_y = {int: 1, bytes: 2+2j}
dict_y.clear()
print(dict_y)
3.copy分为深浅靠背
dict_y = {int: 1, bytes: 2+2j, float: 1.1}
dict_2y = dict_y
dict_3y = dict_y.copy()
dict_y['int: 1'] = 'int: 3'
print(dict_y)
print(dict_2y)
print(dict_3y)
dict3为深拷贝不会因为父目录的变化而变化,dict2为dict1的 引名会随着父目录改变
4.fromkeys:创造一个新的字典并返回
dict_y = dict.fromkeys([1, 2, 3], 'hello')
print(dict_y)
dict_y = dict.fromkeys([1, 2, 3], ['hello', 'hi'])
print(dict_y)
5.get返回指定的赋值如果没有为缺省值
dict_y = {int: 1, bytes: 2+2j }
print(dict_y.get(int))
6.items以列表形式返回可遍历元素组
dict_y = {int: 1, bytes: 2+2j }
print(dict_y.items())
7.keys返回一个可迭代xulie
dict_y = {int: 1, bytes: 2+2j}
print(dict_y.keys())
8. popitem删除元素只留列表最后一个元素
dict_y = {int: 1, bytes: 2+2j, float: 1.1}
print(dict_y.popitem())
dict_y = {int: 1, bytes: 2+2j}
print(dict_y.popitem())
9,setdefault接受一个值或者缺省值并返回该值
dict_y = {int: 1, bytes: 2+2j, float: 1.1}
print(dict_y.setdefault('com', 2+2j))
10.update使用可使原字典所含元素更新
dict_y = {int: 1, bytes: 2+2j, float: 1.1}
dict_2y = {float: 2.2}
dict_y.update(dict_2y)
print(dict_y)
11.values返回字典中所包含的值
dict_y = {int: 1, bytes: 2+2j, float: 1.1}
print(dict_y.values())