python-列表list

1.列表的创建
2.列表的特性
3.列表元素的增加、删除、修改、查看

1.列表的创建
#数组:存储同一种数据类型的集和 scores=[1,2,33,44]
#列表(打了激素的数组):可以存储任意数据类型的集和

li = [1,2.2,True,'hello']
print(li,type(li))

[1, 2.2, True, 'hello'] <class 'list'>

##列表里面是可以嵌套列表的

li = [1,2,3,False,'python',[1,2,3,4,5]]
print(li,type(li))
[1, 2, 3, False, 'python', [1, 2, 3, 4, 5]] <class 'list'>

练习
随机输出一个乱序的列表:

import random
li = list(range(10))
# 快速生成列表
random.shuffle(li)
print(li)

2.列表的特性
索引、切片、重复、连接、成员操作符、嵌套列表、嵌套列表的索引

service = ['http','ssh','ftp']

#索引
#正向索引

print(service[0])	#输出第一个索引值

http

#反向索引

print(service[-1])	#输出最后一个索引值

ftp

#切片

print(service[::-1])  # 列表的反转,倒序输出
['ftp', 'ssh', 'http']

print(service[1::])   # 除了第一个之外的其他元素
['ssh', 'ftp']

print(service[:-1])   # 除了最后一个之外的其他元素
['http', 'ssh']

#重复

print(service * 3)
['http', 'ssh', 'ftp', 'http', 'ssh', 'ftp', 'http', 'ssh', 'ftp']

#连接

service1 = ['mysql','firewalld']
print(service + service1)
['http', 'ssh', 'ftp', 'mysql', 'firewalld']

#成员操作符

print('firewalld' in service)
False

print('ftp' in service)
True

print('firewalld' not in service)
True

print('ftp' not in service)
False

#列表里面嵌套列表

service2 = [['http',80],['ssh',22],['ftp',21]]

#索引

print(service2[0][0])	#第一个列表中的第一个元素
http

print(service2[-1][1])	#最后一个列表的第二个元素
21

练习1:
根据用于指定月份,打印该月份所属的季节。
提示: 3,4,5 春季 6,7,8 夏季 9,10,11 秋季 12, 1, 2 冬季

month = int(input('Month:'))
if month in [3,4,5]:
    print('春季')
elif month in [6,7,8]:
    print('夏季')
elif month in [9,10,11]:
    print('秋季')
else:
    print('冬季')

练习2:
假设有下面这样的列表:
names = [‘fentiao’,‘fendai’,‘fensi’,‘fish’]
输出的结果为:‘I have fentiao,fendai,fensi and fish’

names = ['fentiao', 'fendai', 'fensi', 'fish']
print('I have ' + ','.join(names[:3]) + ' and ' + names[-1])

3.列表元素的增加
-1用+[’’]的方式
-2append(‘元素’)追加一个元素
-3extend([‘元素1’,'元素2 '])追加多个元素
-4insert(索引值,‘元素’)

-1

service = ['http', 'ssh', 'ftp']
print(service + ['firewalld'])

['http', 'ssh', 'ftp', 'firewalld']

-2
#append:追加,追加一个元素到列表中

service.append('firewalld')
print(service)
['http', 'ssh', 'ftp', 'firewalld']

-3
#extend:拉伸,追加多个元素到列表中

service.extend(['hello','python'])
print(service)
['http', 'ssh', 'ftp', 'hello', 'python']

-4
#insert:在索引位置插入元素

service.insert(0,'firewalld')	#在索引值为0的位置插入
print(service)
['firewalld', 'http', 'ssh', 'ftp']

4.列表的删除
-1 pop() 弹出最后一个元素
pop(1) 弹出索引值为1的元素
-2 remove(‘元素’) 移除元素
-3 clear() 清空列表里所有元素
-4 del(python关键字) 从内存中删除列表

-1.pop() 弹出索引元素

In [1]: li = [1,2,3,4]

In [2]: li.pop()#弹出最后一个元素
Out[2]: 4

In [3]: li.pop(0)#弹出索引为0的元素
Out[3]: 1
In [4]:li.pop(3) #弹出索引为3的元素 ,这里已经弹出了所以没有索引为3的元素
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-4-e9f9f299015e> in <module>()
----> 1 li.pop(3)

-2 remove()移除某元素

service = ['http', 'ssh', 'ftp']

service.remove('ftp')	#移除列表中ftp
print(service)
['http', 'ssh']

service.remove('https')	#列表中没有https,输出会报错
print(service)
Traceback (most recent call last):
  File "/home/kiosk/Documents/python/python1124/day03/09_列表元素的删除.py", line 25, in <module>
    service.remove('https')
ValueError: list.remove(x): x not in list

-3 clear()清空列表里面的所有元素

service = ['http', 'ssh', 'ftp']

service.clear()	#清空列表里面的所有元素
print(service)

[]

-4 del(python关键字) 从内存中删除列表

service = ['http', 'ssh', 'ftp']

del service	#从内存中移除service,之后再打印报错service不存在
print(service)

Traceback (most recent call last):
  File "/home/kiosk/Documents/python/python1124/day03/09_列表元素的删除.py", line 34, in <module>
    print(service)
NameError: name 'service' is not defined

print('删除列表第一个索引对应的值:',end='')
del service[1]	#删除列表第一个索引对应的值
print(service)

删除列表第一个索引对应的值:['http', 'ftp']

print('删除前两个元素之外的其他元素:',end='')
del service[2:]	#删除前两个元素之外的其他元素
print(service)


删除前两个元素之外的其他元素:['http', 'ssh']

5.列表元素的修改

-1索引
-2 slice切片

service = ['ftp','http', 'ssh', 'ftp']

#1通过索引,重新赋值
service[0] = 'mysql'
print(service)

['mysql', 'http', 'ssh', 'ftp']

#2通过slice(切片)
service[:2] = ['mysql','firewalld']
print(service)

['mysql', 'firewalld', 'ssh', 'ftp']

6.列表元素的查看

-1查看元素出现的次数
-2查看制定元素的索引值(也可以指定范围)
-3 排序查看(按照ascii码进行排序)
-4 对字符串排序不区分大小写

1查看元素出现的次数

service = ['ftp','http','ssh','ftp']
#查看元素出现的次数
print(service.count('ftp'))

2查看制定元素的索引值(也可以指定范围)

#查看指定元素索引值 ,也可以指定[1:4]范围
print(service.index('ftp'))
print(service.index('ftp',1,4))

3 排序查看(按照ascii码进行排序)

#排序查看 按照ASCII顺序
service.sort()
print(service)
#逆序排序
service.sort(reverse=True)
print(service)

4 对字符串排序不区分大小写

phones = ['alice','bob','harry','Borry']
phones.sort(key=str.lower)	#输出结果['alice', 'bob', 'Borry', 'harry']
phones.sort(key=str.upper) 	#输出结果['alice', 'bob', 'Borry', 'harry']
print(phones)

猜你喜欢

转载自blog.csdn.net/weixin_43067754/article/details/84546966