首頁 > 後端開發 > Python教學 > 索引和切片

索引和切片

WBOY
發布: 2024-07-26 10:40:33
原創
648 人瀏覽過

Indexing and Slicing

  • 索引和切片對我來說是重要的概念。
  • 可能的應用是數位電子中的LSB和MSB

申請:
我有一個設備,它根據我從驅動程式或用戶介面設定的浮點值來設定溫度。內部浮點值轉換為十六進制,並要求設備設定所需的溫度。

但是當我們想要從設備讀取完整的詳細資訊時,它將採用具有多個位元組的十六進位格式。如果我只需要一個與其二進制值相反的位元組數據,那麼切片是提取該值的最佳方法。

但我認為Python效率更高。當我學習更多概念時,一些與十六進位轉二進位或處理十六進位資料相關的套件會有一些功能。

學習:
我已經嘗試並從本次會議中學到了以下內容

  • 積極索引
  • 負索引
  • 使用正索引進行切片
  • 使用負索引進行切片
  • 逆序切片需要第三個參數Step
###############################################
#         INDEXING
###############################################

'''
Positive Indexing:
H E L L O   W O R L D
0 1 2 3 4 5 6 7 8 9 10
'''

#Positive indexing
message = 'HELLO WORLD'
print ('Postive indexing:', message[0], message [1], message [2], message [3], message [4])  # H E L L O

'''
Here indexing starts with 0. Python is able to strip the string like array elements
Negative indexing
H   E   L   L   O
-5  -4 -3  -2  -1
'''

#Negative indexing
message1 = '  Hello'
print ('Negative Indexing:', message1[-1], message1[-2], message1[-3], message1[-4], message1[-5]) # o l l e H

'''
Length is always number of characters or elements in string. 
  - length > last element index
  - length = last index +1

when we define out of range indexing, string index out of range error would come
In the above example,
'''

print('Length of string:',len(message), len(message1))     # 11 , 7


###############################################
#         SLICING
###############################################

#Message[Start:Stop:Step]
print('\nSlicing 1 to 4th index elements of HELLO WORLD using message[1:5]:', message[1:5])
print('Slicing before 6th index elements of HELLO WORLD using message[:6]:', message[:6])
print('Slicing from 6th index elements of HELLO WORLD using message[6:]:', message[6:])
print('Slicing from 6th index elements of HELLO WORLD using message[6: 100]:', message[6:100])


# Slicing using negative index also possible
print('\nSlicing using negative indexing in HELLO WORLD using message[-11:5]:', message[-11:5])
# Here number 5 is STOP, it refers 5th index
print('Slicing using negative indexing in HELLO WORLD using message[-11:-4]:', message[-11:-4])

'''
Reversing the message contents can be done using step definition
message [5:-10:-1]
'''
print('\nSlicing in reverse order using step (message [5:-12:-1]):',message [5:-12:-1])
print('Slicing in reverse order only ROW(NI) from HELLO WORLD (message [-3:-7:-1]):',message [-3:-7:-1])
print('Slicing in reverse order only ROW(PI) from HELLO WORLD (message [8:4:-1]):',message [8:4:-1])

登入後複製

結果:

PS C:\Projects\PythonSuresh> python Class7.py
Postive indexing: H E L L O
Negative Indexing: o l l e H
Length of string: 11 7

Slicing 1 to 4th index elements of HELLO WORLD using message[1:5]: ELLO
Slicing before 6th index elements of HELLO WORLD using message[:6]: HELLO 
Slicing from 6th index elements of HELLO WORLD using message[6:]: WORLD
Slicing from 6th index elements of HELLO WORLD using message[6: 100]: WORLD

Slicing using negative indexing in HELLO WORLD using message[-11:5]: HELLO
Slicing using negative indexing in HELLO WORLD using message[-11:-4]: HELLO W

Slicing in reverse order using step (message [5:-12:-1]):  OLLEH
Slicing in reverse order only ROW(NI) from HELLO WORLD (message [-3:-7:-1]): ROW
Slicing in reverse order only ROW(PI) from HELLO WORLD (message [8:4:-1]): ROW
登入後複製

以上是索引和切片的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板