day04-python列表

python列表是最具有灵活性的有序集合对象类型。它可以包含任意的对象:数字、字符串以及其他的列表。与字符串不同的是列表是可变对象,可以在原地进行修改。

一.列表的基本操作

1.确定列表的长度

使用len()函数可以快速的确定列表的长度,如下:

list_info = ['car','cat','audi','dd']
print('列表list_info的长度是:%d' % len(list_info))
# 列表list_info的长度是:4

2.列表的合并

s = [1,2,3,4,5]+[6,7,8,9,10]
print(s)
# [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
 

s = [1,2,3,4,5]
s.extend([5,6,7,8,9,10])
print(s)
# [1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10]

 

二.列表的迭代和解析

1. 判断某个元素是否在列表中

print('food' in ['food','tea','apple'])
# True

2.遍历整个列表

infos = ['food','tea','apple']
for info in infos:
    print(info)

#food
#tea
#apple

3. 列表的解析

3.1 列表解析表达式

res = [i*4 for i in [1,2,3,4]]
print(res)
# [4, 8, 12, 16]


res = []
for i in [1,2,3,4]:
    res.append(i * 4)
print(res)

ps:张两段代码的意思是相同的

3.2 enumerate方法解析列表

res = ['food','tea','apple']
print(enumerate(res))
# <enumerate object at 0x00000000024ED798>

for index,value in enumerate(res):
    print(index,value)

# 0 food
# 1 tea
# 2 apple

 

三.列表的基本方法

1. 访问列表的元素

name = ['wl','tom','jack','wtx','lhc']
print(name[0])
# wl

2. 切片

name = ['wl','tom','jack','wtx','lhc','Eric','Amy','tanlan']

print('获取下标1-4的数据,包括1,不包括4:',name[1:4])
# 获取下标1-4的数据,包括1,不包括4: ['tom', 'jack', 'wtx']

print('获取下标1- -1的元素,不包括-1:',name[1:-1])
# 获取下标1- -1的元素,不包括-1: ['tom', 'jack', 'wtx', 'lhc', 'Eric', 'Amy']

print('从头开始0-3:',name[0:3],name[:3])
# 从头开始0-3: ['wl', 'tom', 'jack'] ['wl', 'tom', 'jack']

print('获取最后一个值:',name[len(name)-1])
# 获取最后一个值: tanlan

print('每隔几个元素获取一个:',name[1::2],name[::-1])
# 每隔几个元素获取一个: ['tom', 'wtx', 'Eric', 'tanlan'] ['tanlan', 'Amy', 'Eric', 'lhc', 'wtx', 'jack', 'tom', 'wl']

3. 列表的追加

name = ['wl','tom','jack','wtx']
name.append('china')  # 在列表的末尾追加
print(name)
# ['wl', 'tom', 'jack', 'wtx', 'china']


name.insert(2,'vict'# 指定位置追

print(name)
# ['wl', 'tom', 'vict', 'jack', 'wtx', 'china']

4. 修改

name = ['wl','tom','jack','wtx']
name[2] = 'rain'
print(name)
# ['wl', 'tom', 'rain', 'wtx']

5. 删除

5.1 根据下标删除元素

name = ['wl','tom','jack','wtx']
del name[2]
print(name)
# ['wl', 'tom', 'wtx']

5.2 删除指定元素

name = ['wl','tom','jack','wtx']
name.remove('tom')
print(name)
# ['wl', 'jack', 'wtx']

5.3 删除最后一个元素

name = ['wl','tom','jack','wtx']
s = name.pop()
print(s,name)
# wtx   ['wl', 'tom', 'jack']

 

6. 排序和翻转

6.1 排序

name = ['wl','tom','jack','wtx']
name.sort() # 对列表进行永久的排序
print(name)
# ['wl', 'tom', 'jack', 'wtx']



#sorted() 对列表进行临时排序
print(sorted(name)) #按照字母顺序
# ['jack', 'tom', 'wl', 'wtx']
print(name)
# ['wl', 'tom', 'jack', 'wtx']

6.2 倒着打印列表(翻转列表)

name = ['wl','tom','jack','wtx']
name.reverse()
print(name)
# ['wtx', 'jack', 'tom', 'wl']

for i in reversed(name):
print(i)
#
'wtx', 'jack', 'tom', 'wl'

7. 获取元素的下标

name = ['wl','tom','jack','wtx','wl']
print(name.index('wl'))
# 0-只会获取第一次出现时的下标

8. 拷贝列表

import copy

name = ['wl','tom','jack','wtx','wl']
name1 = copy.copy(name)
print(name1)
# ['wl', 'tom', 'jack', 'wtx', 'wl']

name2 = name[:]
print(name2)
# ['wl', 'tom', 'jack', 'wtx', 'wl']

name3 = list(name)
print(name3)
# ['wl', 'tom', 'jack', 'wtx', 'wl']

 

posted @ 2018-12-05 21:50  炫风真是风  阅读(174)  评论(0编辑  收藏  举报