인덱싱 및 슬라이싱

WBOY
풀어 주다: 2024-07-26 10:40:33
원래의
547명이 탐색했습니다.

Indexing and Slicing

  • 인덱싱과 슬라이싱은 나에게 중요한 개념입니다.
  • 디지털 전자 분야의 LSB 및 MSB 적용 가능

신청:
드라이버나 사용자 인터페이스에서 설정한 부동 값을 기반으로 온도를 설정하는 장치가 있습니다. 내부적으로 부동 소수점 값을 16진수로 변환하고 장치에 필요한 온도를 설정하도록 요청합니다.

그러나 장치에서 전체 세부 정보를 읽으려는 경우 여러 바이트의 16진수 형식이 됩니다. 바이너리 값의 역순으로 1바이트의 데이터만 필요한 경우 해당 값을 추출하는 가장 좋은 방법은 슬라이싱입니다.

하지만 저는 Python이 더 효율적인 것 같아요. 더 많은 개념을 배우면 16진수에서 2진수로 관련되거나 16진수 데이터를 다루는 패키지에 몇 가지 기능이 있을 것입니다.

학습:
저는 이번 세션을 통해 아래 사항을 시도하고 배웠습니다

  • 긍정적 인덱싱
  • 음수 색인
  • 포지티브 인덱싱을 사용한 슬라이싱
  • 음수 인덱싱을 사용한 슬라이싱
  • 역순으로 슬라이싱하려면 세 번째 인수가 필요합니다.
###############################################
#         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으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!