申请:
我有一个设备,它根据我从驱动程序或用户界面设置的浮点值设置温度。内部浮点值转换为十六进制,并要求设备设置所需的温度。
但是当我们想要从设备读取完整的详细信息时,它将采用具有多个字节的十六进制格式。如果我只需要一个与其二进制值相反的字节数据,那么切片是提取该值的最佳方法。
但我认为Python效率更高。当我学习更多概念时,一些与十六进制转二进制或处理十六进制数据相关的包会有一些功能。
学习:
我已经尝试并从本次会议中学到了以下内容
############################################### # 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中文网其他相关文章!