[python基礎(一)]序列操作基礎

來源:互聯網
上載者:User

標籤:匹配   多個   一個   移除   min()   括弧   abc   最小值   list   

一、python的資料結構

 

容器:包含其他對象的任意對象。

包括:序列(例如列表,元組),映射(例如字典),集合等。主要是序列和映射。

 

二、序列操作

索引,分區,加,乘,檢查某元素是否是該序列成員。此外,計算序列長度,尋找最大,最下元素。

1.索引

正數:從0開始,左至右;

負數:從-1開始,有至左

      2.分區

       兩個索引作為邊界,第一個索引元素包含在分區內,第二個不包含。

       A=’123456789’

       Print A[2:5]

        ‘345‘

      3.序列相加 條件:序列的類型相同

      4.乘法

      5.成員資格 in運算子

6.長度len() ,最大值max(),最小值min()

三、列表

1.list函數 :根據字串建立列表

2.基本的列表操作

賦值
x=[1,1,1]
x[1]=2
print x

#刪除元素
name=[‘x‘,‘y‘,‘z‘]
del name[2]
print name

#分區賦值
name=list(‘Perl‘)
name[2:]=list(‘ar‘)
print name

name[2:]=list(‘ython‘)
print name

numbers=[1,5]
numbers[1:1]=[2,3,4]
print numbers

3.列表方法

#1.append:列表末端加新對象
lst=[1,2,3]
lst.append(4)
print lst

#2.count 統計某元素出現的個數
a=[‘to‘,‘to‘,‘a‘,‘b‘,‘to‘,‘c‘].count(‘to‘)
print a

#3.extend 列表末尾一次性加多個值,跟串連有所差別
a=[1,2,3]
b=[4,5,6]
a.extend(b)
print a

#4.index  找出某個值的第一個索引位置
k=[‘a‘,‘b‘,‘c‘,‘d‘]
print k.index(‘c‘)

#5.insert 將對象插入到列表中
ll=[1,2,3,4,5]
ll.insert(3,‘four‘)
print ll

#6.pop 移除列表中的某個元素,預設是最後一個
x=[1,2,3,4,5]
print x.pop()
x.pop(1)
print x

#7.remove 移除列表中某一個值的匹配項
x=[‘a‘,‘b‘,‘c‘,‘b‘,‘e‘]
x.remove(‘b‘)
print x     #[‘a‘, ‘c‘, ‘b‘, ‘e‘]

#8.reverse 將列表中元素方向存放
x=[‘a‘,‘b‘,‘c‘,‘b‘,‘e‘]
x.reverse()
print x   #[‘e‘, ‘b‘, ‘c‘, ‘b‘, ‘a‘]

#9sort 排序
x=[4,2,6,7,3,9,4,3]
x.sort()
print x   #[2, 3, 3, 4, 4, 6, 7, 9]

#10 cmp
print cmp(12,10)   #1
print cmp(10,12)   #0
print cmp(10,12)   #-1

四.元組:不可變序列

1.元組建立:通過圓括弧括起來

(1, 2, 3)

2. tuple 函數 將序列轉換為元組
k=tuple(‘abc‘)
print k   #(‘a‘, ‘b‘, ‘c‘)

[python基礎(一)]序列操作基礎

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.