北京721,用快播看电影,特异功能猩球人粤语
#声明列表
marvel_heroes = ['jack', 'tom', 'lucy', 'superman', 'ironman']
computer_brands = []
print(id(marvel_heroes))
print(id(computer_brands)) #定义空列表
#遍历
for name in marvel_heroes:
print(name)
#获取第一个元素
print(marvel_heroes[0])
#获取最后一个元素
print(marvel_heroes[-1])
print(marvel_heroes[len(marvel_heroes)-1])
#判断是否存在
result = 'jack' in marvel_heroes
print(result)
#切片操作
print(marvel_heroes[1:3]) #包前不包后
#反向
print(marvel_heroes[-1::-2])
#增:
# append(): 在末尾追加
brands = ['hp', 'thinkpad']
brands.append('dell')
print(brands)
# extend():把字符串一个一个拆了再追加,相当于列表合并
names = []
names.extend('dell') #示范
print(names)
names.extend(brands) #正确的使用
print(names)
# insert():指定位置插入,原来的数据往后移动
brands = ['hp', 'thinkpad']
brands.insert(1, 'dell')
print(brands)
#删
brands = ['hp', 'thinkpad', 'lenovo', 'huawei', 'dell', 'mac', 'apple', 'hp', 'hp', 'acer']
l = len(brands)
i = 0
while i < l:
if 'hp' == brands[i] or 'mac' == brands[i]:
del brands[i]
l -= 1
i -= 1 #防止出现连续的情况,漏删了后面内容
i += 1
print(brands)
#防止漏删法2
brands = ['hp', 'thinkpad', 'lenovo', 'huawei', 'dell', 'mac', 'apple', 'hp', 'hp', 'acer']
l = len(brands)
i = 0
while i < l:
if 'hp' != brands[i] and 'mac' != brands[i]:
i += 1
else:
del brands[i]
l -= 1
print(brands)
#防止漏删法3
brands = ['hp', 'thinkpad', 'lenovo', 'huawei', 'dell', 'mac', 'apple', 'hp', 'hp', 'acer']
l = len(brands)
i = 0
while i < l:
if 'hp' == brands[i] or 'mac' == brands[i]:
del brands[i]
l -= 1
continue #防止漏删了连续的内容
i += 1
print(brands)
#改
brands[5] = 'xiaomi'
print(brands)
import random
random_list = random.sample(range(0, 100), 6)
print(random_list)
list_sum = sum(random_list)
print(list_sum)
random_list = sorted(random_list) #默认为升序,sorted(random_list, reverse = false)
print(random_list)
添加:append extend insert 删除:del remove(e):移除列表中第一次出现的元素,如果没有找到要删除的元素则报异常 pop():弹栈,移除列表中的最后一个元素,返回值是删除的那个元素,但是也可以指定index删除 clear():清空列表,将列表的所有内容全部删除
hotpot_list = ['海底捞', '呷哺呷哺', '热辣一号', '宽板凳']
print(hotpot_list)
print('------------------------------------------------------------')
hotpot_list.append('张亮麻辣烫')
print(hotpot_list)
print('------------------------------------------------------------')
result = hotpot_list.remove('张亮麻辣烫') #移除列表中第一次出现的元素,如果没有找到要删除的元素则报异常
print(result) #没有返回值
print(hotpot_list)
print('------------------------------------------------------------')
hotpot_list.append('张亮麻辣烫')
result = hotpot_list.pop() #弹栈,移除列表中的最后一个元素,返回值是删除的那个元素
print(result) #返回值是最后一个元素
print(hotpot_list)
result = hotpot_list.pop(2)
print(result)
print(hotpot_list)
print('------------------------------------------------------------')
hotpot_list.reverse() #将列表倒序
print(hotpot_list)
print('------------------------------------------------------------')
hotpot_list.clear()
print(hotpot_list)
l1 = ['a', 'abc', 'jk', 'poop']
for value in l1:
print(value)
for index, value in enumerate(l1):
print(index, value)
###例子:冒泡排序
numbers = [5, 7, 8, 9, 4, 2, 3, 1, 6, 10]
#numbers = sorted(numbers)
print ('排序前列表 :',numbers)
for i in range(len(numbers)):
temp = numbers[i]
for index, value in enumerate(numbers[i:]):
if temp > value:
numbers[index + i] = temp
temp = value
numbers[i] = temp
print('冒泡排序后列表:',numbers)
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Python爬虫:Request Payload和Form Data的简单区别说明
浅谈Python中threading join和setDaemon用法及区别说明
Python3-异步进程回调函数(callback())介绍
python继承threading.Thread实现有返回值的子类实例
Python中使用threading.Event协调线程的运行详解
网友评论