日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

元组、列表及其特性

發(fā)布時間:2023/11/27 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 元组、列表及其特性 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1、

#元組:帶了緊箍咒的列表,不可直接修改元組內(nèi)容

#元組本身不可變數(shù)據(jù)類型,沒有增刪改差
#元組內(nèi)可以存儲任意數(shù)據(jù)類型

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

#元組內(nèi)包含可變數(shù)組類型,可以間接修改元組內(nèi)容
t1=([1,2,3.3],4,5)
t1[0].append(4)
print t1

2、

元組如果只有一個元素的時候,后面一定要加逗號,否則數(shù)據(jù)類型不確定

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

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

3、

# 1.變量交換數(shù)值
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, score4.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]

# 重復(fù)
print allowUsers * 3


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


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

5、

列表的創(chuàng)建

數(shù)組:存儲同一種數(shù)據(jù)類型的集合 scores=[12,13,14]
列表:(打了激素的數(shù)組):可以存儲任意數(shù)據(jù)類型的集合

# 列表里:可以存儲不同的數(shù)據(jù)類型
li = [1,1.2,True,'hello']
print li
print type(li)

# 列表里面也可以嵌套列表(列表:也是一種數(shù)據(jù)類型)
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 關(guān)鍵字 從內(nèi)存中刪除列表
print service
del service
print service

注意:使用del時須謹慎,刪除后是將其從內(nèi)存中刪除。

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()
#對字符串排序不區(qū)分大小寫
?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']

# 查看列表中元素出現(xiàn)的次數(shù)
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]???? # 列表的翻轉(zhuǎn)
print service[1:] ? ??? # 除了第一個元素之外的其他元素
print service[:-1]?? ?? # 除了最后一個元素之外的其他元素


# 重復(fù)
print service * 3


# 連接
?service1 = ['mysql','firewalld']
?print service + service1


# 成員操作符
?print 'firewalld' in service
?print 'firewalld' in service1
?print 'firewalld' not in service1


# for循環(huán)遍歷
print '顯示服務(wù)'.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]

?

總結(jié)

以上是生活随笔為你收集整理的元组、列表及其特性的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

歡迎分享!

轉(zhuǎn)載請說明來源于"生活随笔",并保留原作者的名字。

本文地址:元组、列表及其特性