元组、列表及其特性

1、

#元组:带了紧箍咒的列表,不可直接修改元组内容

#元组本身不可变数据类型,没有增删改差
#元组内可以存储任意数据类型

t=(1,2,3,4.2,'star')
print t,type(t)

#元组内包含可变数组类型,可以间接修改元组内容
t1=([1,2,3.3],4,5)
t1[0].append(4)
print t1

2、

元组如果只有一个元素的时候,后面一定要加逗号,否则数据类型不确定

如下:
t2=('hello')
t3=(1)
print type(t2),type(t3)

t4=('hello',)
t5=('1',)
print type(t4),type(t5)

3、

# 1.变量交换数值
a = 1
b = 2
b, a = a, b
# 先把(a,b)封装成了一个元组(1,2)
# b =(1,2)[0] a=(1,2)[1]
print a, b


# 2.打印变量值
name = 'westos'
age = 10
t = (name,age)
print 'name: %s,age: %d' %(name,age)
print 'name: %s,age: %d' %t


# 3.元组的赋值:有多少个元素,就用多少个变量接收
t = ('westos', 10, 100)
name, age, score = t
print name, age, score

4.scores = (100, 89, 45, 78, 65)
scoresLi = list(scores)
scoresLi.sort()     # 正序输出
print scoresLi
scores = sorted(scores)  # 正序输出
print scores

4、

元组的特性

allowUsers = ('root','westos','lilly')
allowPasswd = ('123','456','789')

# 索引及切片
print allowUsers[0]
print allowUsers[-1]
print allowUsers[1:]
print allowUsers[2:]
print allowUsers[:-1]
print allowUsers[::-1]

# 重复
print allowUsers * 3


# 连接
print allowUsers + ('jenny','danny')


# 成员操作符
print 'westos' in allowUsers
print 'westos' not in allowUsers

5、

列表的创建

数组:存储同一种数据类型的集合 scores=[12,13,14]
列表:(打了激素的数组):可以存储任意数据类型的集合

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

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

6、

列表的修改

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

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

# 通过切片
print service[:2]
service[:2] = ['samba','ladp']
print service

7、

列表的删除

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

 #1.如果pop()不传递值的时候,默认弹出最后一个元素
 print service.pop()
#  pop()也可以传递索引值
 print service.pop(0)

# 2.remove:删除指定的元素
service.remove('ssh')

print service

# 3.del 关键字 从内存中删除列表
print service
del service
print service

注意:使用del时须谨慎,删除后是将其从内存中删除。

8、

列表的在增加

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,'samab')
print service

9、

列表的排序

service = ['http', 'ssh', 'ftp','ftp']
 # 按照Ascii码进行排序的
 service.sort()
 print service


#按Ascii码倒序输出

service.sort(reverse=True)
 print service

phones = ['bob', 'harry', 'Lily', 'Alice']

#按Ascii码进行输出
phones.sort()
#对字符串排序不区分大小写
 phones.sort(key=str.lower)

#phones.sort(key=str.upper)

 print phones

import random
li = list(range(10))
print li
# 将原有的列表顺序打乱
random.shuffle(li)
print li

10、

列表的查看

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

# 查看列表中元素出现的次数
print service.count('ssh')
print service.count('ftp')
# 查看指定元素的索引值
print service.index('ssh')
print service.index('ftp')

11、

列表的特性

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
 print 'firewalld' not in service1


# for循环遍历
print '显示服务'.center(50,'*')
for se in service:
     print se

# 列表里嵌套列表
service2 = [['http',80],['ssh',22],['ftp',21]]
# 索引
print service2[0][1]
print service2[-1][1]
# 切片
print service2[:][1]
print service2[:-1][0]
print service2[0][:-1]

 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值