python-列表

数组:存储同一种数据类型的集合 scores = [1,2,3]
列表:可以存储任意数据类型的集合

In [1]: name1 = 'tom'                                                   

In [2]: name2 = 'Tony'                                                  

In [3]: name3 = 'coco'                                                  

In [4]: name1                                                           
Out[4]: 'tom'

In [5]: name2                                                           
Out[5]: 'Tony'

In [6]: name3                                                           
Out[6]: 'coco'

In [7]:                                                                 

In [7]: name = ['tom','Tony','coco']                                    

In [8]:                                                                 

In [8]: name                                                            
Out[8]: ['tom', 'Tony', 'coco']

In [9]: type(name)                                                      
Out[9]: list

In [10]:  
#列表里:可以存储不同的数据类型
li = [1,1.2,True,'hello']
print(li)
print(type(li))

#列表里也可以嵌套列表(列表:本身也是一种数据类型)
li1 = [1,1.2,True,'hello',[1,2,3,4,5]]
print(li1)
print(type(li1))



列表的特性:
service = ['http','ssh','ftp']

#索引
print(service[0])
print(service[-1])

#切片
print(service[1:])
print(service[:-1])
print(service[::-1])

#重复
print(service * 3)

#连接
service1 = ['mysql','firewalld']
print(service + service1)

#成员操作符
print('firewalld' in service)
print('firewalld' in service1)

#for循环遍历
for se in service:
    print(se)

#列表里嵌套列表
service2 = [['http',80],['ssh',22],['ftp',21]]

#索引
print(service2[1][1])
print(service2[-1][1])

#切片
print(service2[:][1])
print(service2[:-1][0])
print(service2[0][:-1])
列表练习-01判断季节:
[kiosk@foundation15 day3]$ /usr/local/python3.6/bin/python3 判断季节.py 
请输入月份:12
12月的季节为冬
[kiosk@foundation15 day3]$ /usr/local/python3.6/bin/python3 判断季节.py 
请输入月份:5
5月的季节为春
[kiosk@foundation15 day3]$ /usr/local/python3.6/bin/python3 判断季节.py 
请输入月份:8
8月的季节为夏
[kiosk@foundation15 day3]$ cat 判断季节.py 
"""
# _*_coding:utf-8_*_
Name:判断季节.py
Date:1/15/19
Author:westos-liming
Connect:liming.163.com
Desc:
"""

season=[['春',3,4,5],['夏',6,7,8],['秋',9,10,11],['冬',12,1,2]]
month=int(input("请输入月份:"))
for i in range(4):
    for j in range(1,4):
        if month == season[i][j]:
            print("%s月的季节为%s" %(month,season[i][0]))
        else:
            continue
列表的增加:
service = ['http','ssh','ftp']

#1.
# print(service + ['firewalld'])

#2.append:追加一个元素到列表中
service.append('firewalld')
print(service)

#3.extend:拉伸 追加多个元素到列表中
service.extend(['mysql','firewalld'])
print(service)

#4.insert:在指定索引位置插入元素
service.insert(1,'samba')
print(service)
列表的删除:
service = ['http','ssh','ftp']
1.service.pop()  #删除列表最后一项
2.service.remove('ssh')  #删除列表制定元素
3.del service   删除列表内存
列表的修改:
service = ['http','ssh','ftp']

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

#通过切片
print(service[:2])
service[:2] = ['samba','ldap']
print(service)


>>> service = ['http','ssh','ftp']
>>> service[0] = 'mysql'
>>> service
['mysql', 'ssh', 'ftp']
>>> service[:2] = ['firewalld','samba']
>>> service
['firewalld', 'samba', 'ftp']
#列表的查看:
service = ['http','ssh','ftp']
#查看列表中的元素出现的次数:
print(service.count('ftp'))  #打印service列表中ftp元素出现的次数
#查看指定元素的索引值:(可以指定索引范围查看)
print(service.index('ssh'))   #如果查看的该元素是重复元素,则返回最小的索引值
print(service.index('ftp',0,3))  #按指定的索引范围0-3查看ftp元素的索引值
#列表的排序:
service=['http','ftp','ssh']
service.sort()    #按照首字母的ASCII码的大小排序
service.sort(key=str.lower)    #首字母不区分大小写
service.sort(key=str.upper)    #首字母不区分大小写

#将原有的列表顺序打乱:
random.shuffle(service)

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值