回顾一下组元以及列表的累加累乘
names = ('dewei', 'xiaomu', 'xiaowang')
names_add =names+names
names_c = names * 10
names += ('abc', )
print(names)
names *= 10
print(names)
name_list = ['dewei', 'xiaomu']
name_list += ['xiaowang']
print(name_list)
name_list *= 5
print(name_list)
print('dewei' in name_list)
print('dewei' not in name_list)
append函数使用
用法:
list.append(new_item)
#coding:utf-8
books = []
print(id(books))
books.append('python')
print(books)
print(id(books))
number =1.1 #数字:浮点类型
tuple_test =(1, )#元组类型
dict_test = {'name':'dewei'}#字典类型
books.append(number)
books.append(tuple_test)
books.append(dict_test)
#一次只能给一个参数
print(books)
insert函数使用方法
list.insert(index, new_item)
students = [
{'name': 'dewei', 'age':33, 'sex':'男', 'id':1, 'height':'174'},
{'name': 'xiaomu', 'age':10, 'sex':'男', 'id':2, 'height':'183'}
]
xiaoyuan = {
'name': 'xiaoyuan', 'age':18, 'sex':'女', 'id':3, 'height':'170'
}
students.insert(0,xiaoyuan)
print(students)
count使用方法
inttype = list.count(item)
animals = ['cat','dog','rabbit','dog','cat']
cat = animals.count('cat')
dog = animals.count('dog')
rabbit = animals.count('rabbit')
print('我家院子里有很多小动物')
print('我家院子里有%s只小猫' % cat)
print('小狗有{}只'.format(dog))
print(f'兔子有{rabbit}只')
animals_dict = [
{'name': 'dog'},
{'name': 'dog'},
{'name': 'cat'}
]
dog_dict_count = animals_dict.count({'name': 'dog'})
print('小狗在动物的字典中有%s只' %dog_dict_count)
remove使用方法
list.remove(item)
shops = ['可乐','洗发水','可乐','牛奶','牙膏']
print('我们的超市有这些内容:%s' % shops)
print('我们的可乐有%s件产品' % shops.count('可乐'))
print('我们的牛奶有%s件产品' %shops.count('牛奶'))
print('购买一件可乐')
shops.remove('可乐')
print('现在可乐还剩%s件' % shops.count('可乐'))
del shops
print(shops)
reverse使用方法
反转列表顺序
list.reverse()
sort使用方法
排序
列表中元素类型必须相同
list.sort(key = None, reverse = False)
clear使用方法
list.clear()
copy
list.copy()
extend使用方法
将其他列表或元组的元素倒入到当前列表中
manhua = []
history = []
code = []
new_manhua = ('a', 'b', 'c')
new_history = ('中国历史', '日本历史', '韩国历史')
new_code = ('python', 'django', 'flask')
manhua.extend(new_manhua)
print(manhua,history,code)
history.extend(manhua)
del manhua
print(history)