python中的序列是什麼 Python中的序列

2021-10-12 01:31:17 字數 3342 閱讀 2516

什麼是序列?

列表、元組和字串都是序列。為了讓我們方便的在序列中抓取乙個特定的專案,具備這樣的特點:

1、成員檢驗

2、索引操作符

一些基本操作:

operationresultnotes

x in s

true if an item of s is equal to x, else false

x not in s

false if an item of s is equal to x, else true

s + t

the concatenation of s and t

s * n, n * s

equivalent to adding s to itself n times

s[i]

ith item of s, origin 0

s[i:j]

slice of s from i to j

s[i:j:k]

slice of s from i to j with step k

len(s)

length of s

min(s)

smallest item of s

max(s)

largest item of s

s.index(x)

index of the first occurrence of x in s

s.count(x)

total number of occurrences of x in s

下標操作:

使用索引來取得序列中的單個專案

name= 'piayie'print('item 0 is', shoplist[0])

print('item 1 is', shoplist[1])

print('item 2 is', shoplist[2])

print('item 3 is', shoplist[3])

print('item -1 is', shoplist[-1])

print('item -2 is', shoplist[-2])

print('character 0 is', name[0])

輸出:item1 ismango

item2 iscarrot

item3 isbanana

item-1 isbanana

item-2 iscarrot

character0 is p

當用方括號中的乙個數來指定乙個序列時候,python會為我們抓取序列對應位置的專案。注意:python從0開始計數。

當索引標是負數時候,位置是從列尾開始計算的,shoplist[-1]表示最後乙個元素。

切片操作

切片操作符是序列名後跟乙個方括號,方括號中有一對可選的數字,並用冒號分割。注意這與你使用的索引操作符十分相似。記住數是可選的,而冒號是必須的。

切片操作符中的第乙個數表示切片開始的位置,第二個表示切片結束的位置。

如果不指定第乙個數,預設從序列首部開始。

如果不指定第二個數,python會在系列尾部停止。

可以用負數做切片,得到從序列尾[-1]開始計算的位置。

特別的,返回的序列是從指定開始位置起始,在指定結束位置之前終止,也就是說切出的序列不包括尾部專案。

print('item 1 to 3 is', shoplist[1:3])

print('item 2 to end is', shoplist[2:])

print('item 1 to -1 is', shoplist[1:-1])

print('item start to end is', shoplist[:])

print('characters 1 to 3 is', name[1:3])

print('characters 2 to end is', name[2:])

print('characters 1 to -1 is', name[1:-1])

print('characters start to end is', name[:])

輸出:item 1 to 3 is ['mango', 'carrot']

item2 to end is ['carrot', 'banana']

item1 to -1 is ['mango', 'carrot']

characters1 to 3 isia

characters2 to end isayie

characters1 to -1 isiayi

characters start to endis piayie

我們必須使得這個切片是正向的,也就是說起始位置不能落後於結束位置,否則就會引發syntaxerror

print('characters -1 to 2 is', name[1-:2])

輸出:file "d:\piayie\vscode\python學習筆記\subscription.py", line 20print('characters -1 to 2 is', name[1-:2])^syntaxerror: invalid syntax

d:\piayie\vscode\python學習筆記》

和大部分的pick方法一樣,切片操作符還有第三個引數,我們把它叫做步長。

print('characters -1 to 2 is', name[::2])

輸出:characters begin to end via step = 2 is pai

特別的,我們應該注意切片操作是一種建立副本的operation

若要在迴圈內部修改正在遍歷的序列(例如複製某些元素),首先應該製作副本。

在序列上迴圈不會隱式地建立副本。切片表示法使這尤其方便:

>>> words = ['cat', 'window', 'defenestrate']>>> for w in words[:]: #loop over a slice copy of the entire list.

... if len(w) > 6:

... words.insert(0, w)

...>>>words

['defenestrate', 'cat', 'window', 'defenestrate']

序列拆封

>>> t = 12345, 54321, 'hello!'

>>> x, y, z =t>>>x12345

>>>y54321

>>>z'hello!'

這個呼叫等號右邊可以是任何線性序列,稱之為 序列拆封 非常恰當。序列拆封要求左側的變數數目與序列的元素個數相同。

要注意的是可變引數(multiple assignment)其實只是元組封裝和序列拆封的乙個結合。

python中的檔案是什麼 python中的檔案

python檔案 1 概述 檔案物件不僅可以用來訪問普通的磁碟檔案,也可以訪問任何其他型別抽象層面上的檔案。內建函式open 以及file 提供了初始化輸入輸出 i o 操作的通用介面。open 內建函式成功開啟檔案之後會返回乙個檔案物件,否則引發乙個錯誤,當操作失敗的時候,會出現乙個ioerror...

python中ch是什麼 Python中的函式

函式是實現某個功能的介面,通過給函式傳入引數來實現某個功能。函式 function 的定義通常分為傳統定義和近代定義,函式的兩個定義本質是相同的,只是敘述概念的出發點不同,傳統定義是從運動變化的觀點出發,而近代定義是從集合 對映的觀點出發。函式的近代定義是給定乙個數集a,假設其中的元素為x,對a中的...

python中的super是什麼?

python中的super,名為超類,可以簡單的理解為執行父類的 init 函式。由於在python中不論是一對一的繼承,還是一子類繼承多個父類,都會涉及到執行的先後順序的問題。那麼本文就著重看下super的具體作用。通過設計這樣乙個案例,我們可以明確super的前後邏輯關係 先定義乙個父類init...