当前位置: 移动技术网 > IT编程>脚本编程>Python > 7.Python列表

7.Python列表

2020年01月06日  | 移动技术网IT编程  | 我要评论

北京721,用快播看电影,特异功能猩球人粤语

 

list列表

 

序列是python中最基本的数据结构。序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推

python有6个序列的内置类型,但最常见的是列表和元组。

序列都可以进行的操作包括索引,切片,加,乘,检查成员。

此外,python已经内置确定序列的长度以及确定最大和最小的元素的方法。

列表是最常用的python数据类型,它可以作为一个方括号内的逗号分隔值出现。

列表的数据项不需要具有相同的类型

 

定义

创建一个列表,只要把逗号分隔的不同的数据项使用方括号括起来即可。如下所示:

in [1]:
#声明列表
marvel_heroes = ['jack', 'tom', 'lucy', 'superman', 'ironman']   
computer_brands = []

print(id(marvel_heroes))
print(id(computer_brands))     #定义空列表

#遍历
for name in marvel_heroes:
    print(name)
 
1633246958920
1633246958856
jack
tom
lucy
superman
ironman
 

in [2]:
#获取第一个元素
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])
 
jack
ironman
ironman
true
['tom', 'lucy']
['ironman', 'lucy', 'jack']
 

增 :append() extend() insert(

删:删除时注意删了一个元素继续原来的遍历可能会造成访问越界。因为删除一个元素直接会在内存减少空间。

in [3]:
#增:
# 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)
 
['hp', 'thinkpad', 'dell']
['d', 'e', 'l', 'l']
['d', 'e', 'l', 'l', 'hp', 'thinkpad', 'dell']
['hp', 'dell', 'thinkpad']
in [4]:
#删
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)
 
['thinkpad', 'lenovo', 'huawei', 'dell', 'apple', 'acer']
['thinkpad', 'lenovo', 'huawei', 'dell', 'apple', 'acer']
['thinkpad', 'lenovo', 'huawei', 'dell', 'apple', 'acer']
in [5]:
#改
brands[5] = 'xiaomi'
print(brands)
 
['thinkpad', 'lenovo', 'huawei', 'dell', 'apple', 'xiaomi']
 

排序

in [6]:
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)
 
[25, 5, 22, 88, 38, 70]
248
[5, 22, 25, 38, 70, 88]
 

列表函数

添加:append extend insert 删除:del remove(e):移除列表中第一次出现的元素,如果没有找到要删除的元素则报异常 pop():弹栈,移除列表中的最后一个元素,返回值是删除的那个元素,但是也可以指定index删除 clear():清空列表,将列表的所有内容全部删除

in [7]:
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)
 
['海底捞', '呷哺呷哺', '热辣一号', '宽板凳']
------------------------------------------------------------
['海底捞', '呷哺呷哺', '热辣一号', '宽板凳', '张亮麻辣烫']
------------------------------------------------------------
none
['海底捞', '呷哺呷哺', '热辣一号', '宽板凳']
------------------------------------------------------------
张亮麻辣烫
['海底捞', '呷哺呷哺', '热辣一号', '宽板凳']
热辣一号
['海底捞', '呷哺呷哺', '宽板凳']
------------------------------------------------------------
['宽板凳', '呷哺呷哺', '海底捞']
------------------------------------------------------------
[]
 

enumerate():函数用于将一个可遍历的数据对象(如列表、元祖或字符串)组合为一个索引序列。

in [8]:
l1 = ['a', 'abc', 'jk', 'poop']
for value in l1:
    print(value)

for index, value in enumerate(l1):
    print(index, value)
 
a
abc
jk
poop
0 a
1 abc
2 jk
3 poop
in [9]:
###例子:冒泡排序
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)
 
排序前列表    : [5, 7, 8, 9, 4, 2, 3, 1, 6, 10]
冒泡排序后列表: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网