インデックス作成とスライス

WBOY
リリース: 2024-07-26 10:40:33
オリジナル
547 人が閲覧しました

Indexing and Slicing

  • インデックス作成とスライスは私にとって重要な概念です。
  • デジタルエレクトロニクスにおける LSB と MSB への応用が考えられます

アプリケーション:
ドライバーまたはユーザーインターフェイスから設定した浮動小数点値に基づいて温度を設定するデバイスがあります。内部的に float 値が 16 進数に変換され、デバイスは必要な温度を設定するように求められます。

しかし、デバイスから完全な詳細を読み取りたい場合、それは複数バイトの 16 進形式になります。バイナリ値の逆順で 1 バイトのデータのみが必要な場合、その値を抽出するにはスライスが最適な方法です。

しかし、私は Python の方が効率的だと思います。もっと概念を学ぶと、16 進数からバイナリへの変換や 16 進数データの処理に関連するパッケージにいくつかの機能が含まれるようになります。

学習:
このセッションで以下のことを試して学びました

  • ポジティブインデックス
  • ネガティブインデックス作成
  • ポジティブインデックスを使用したスライス
  • 負のインデックスを使用したスライス
  • 逆の順序でスライスするには 3 番目の引数が必要です。
###############################################
#         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 中国語 Web サイトの他の関連記事を参照してください。

ソース:dev.to
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!