Python data structure: conversion exploration

Source: Internet
Author: User
Tags ord

Tag: Orm RoCE represents return Exce www. repr number www

*********************************First Part*************************************************************************************************************** # Enter the line number of Excel and output the ordinal number of the corresponding column (starting from 0) # Equivalent: 26 binary with A-Z to convert it to 10, namely: input a output 0, input B Output 1, input AA output 26 .... STRs = input (' Please enter a 1-2-letter string: '). lower () List_char = list (strs) sum = 0 def sub_str (str_a, Str_b): Return ord (str_a)-ord (St R_b) for index, str in enumerate (list_char[::-1]): Sum + = (sub_str (str, ' a ') +1) * * * * index print (sum-1) print (List_cha R[::-1]) *********************************Part II*************************************************************************************************************** Interpretation of the topic: that is a=0,z=25 z-a=25 according to the conversion of the system, Get the following formula: A = (a-a+1) *26^0-1 = 0Z = (z-a+1) *26^0-1 = 25AA = (a-a+1) *26^1 + (a-a+1) *26^0-1 = 26AZ = (a-a+1) *26^1 + (z-a+1) * 26^0-1 = 51BA = (b-a+1) *26^1 + (a-a+1) *26^0-1 = 52ZA = (z-a+1) *26^1 + (a-a+1) *26^0-1 = 26*26=676...............ZBFA = ( z-a+1) *26^3 + (b-a+1) *26^2 + (f-a+1) +26^1 + (a-a+) *26^0-1 if input: ZA, then List_char = [' Z ', ' A '], indexed to 0,1enumerate (list_char[::- 1] Reverse the list, i.e. [' A ', ' Z '], to reverse the index *********************************Part III*************************************************************************************************************** So, by this method, the hexadecimal output is decimal #!/usr/bin/ env/python35#-*-coding:utf-8-*-# Author:keekuun # 16 binary is every 16 into 1def distance (NUM1, num2): # Find the distance from the first number (0) if Ord (NUM1) >= 65:# input is a,b,c,d,e return ord (NUM1)-Ord (num2)-7else:# input is 0-9 return ord (NUM1)-Ord (NUM2) def sixteen_to_ten (num) : result = 0 for index, value in enumerate (Num[::-1]): # Converts each number of bits to 10, then sums result + = distance (value, str (' 0 ')) * 16 * * Index # print (' result=%s '%result) return result num = list (input (' 16 binary number (not added 0x): '). Upper ()) Print (Sixteen_to_ten ( num)) *********************************Fourth Part*************************************************************************************************************** **********************************************************************************
  • Decimal: Decimal system, each bit up to 9, up to 10

  • Binary: Binary system, each bit up to 1, up to 2

  • Octal: Octonary number system, each bit up to 7, up to 8

  • Hex: hexadecimal, each with a maximum of the15 or0xf 0xf, less than 16 (that's 0xG0xG,:-D).

Inference:

  • If a number is 25, then its binary is not less than 6 binary;

  • Naturally it can also be understood that if a number of a certain bit of the value range is[0,m? 1] [0,m?1], then the number is m-m in the system;

>>> 0b101010             # 也即python原生语法是支持二进制表示>> 0xff255 # 自然也支持八进制
Convert to Decimal
int(string, base)            # 第一个参数标识:需要转换的原始的数据,以字符串的形式表示            # 第二个参数标识:原始数据的base或者叫本身的进制表示 # 2:二进制 # 8:八进制 # 16:表示16进制 # 最终转化为十进制

Binary? Decimal
int(‘1010‘, 2)10

Hexadecimal? Decimal
int(‘f‘, 16)15>>> int(‘0xf‘, 16)15>>> int(‘0xff‘, 16)255

Octal? Decimal
int(‘17‘, 8)15              # 15 = 7*8^0+1*8^1

Convert to 16 binary
hex(string)            # 也即没有进制的设置            # 只接受10进制            # 为实现其他进制的转换,可先转换为十进制使用int()            # 返回位字符串类型
hex(1033)‘0x409‘>>> hex(int(‘101010‘, 2))‘0x2a‘>>> hex(int(‘17‘, 8))‘0xf‘

Convert to Binary
bin(十进制整型)
>>> bin(10)‘0b1010‘>>> bin(int(‘ff‘,16))‘0b11111111‘>>> bin(int(‘17‘,8))‘0b1111‘
Convert to Octal
oct()            # 不同于hex/bin            # 通过参数进行判断            # 其是二进制、十进制、16进制            # 也即oct函数可将任意进制的数转换为8进制
>>> oct(0b1010)‘012‘>>> oct(11)‘013‘>>> oct(0xf)‘017‘
To m进制The conversion

Constant to the m modulus of redundancy, the remainder of the current position (low to high), the new dividend, the support quotient of 0.

example, we convert from 25 to 3 in decimal;

25/3? 8 (1)
8/3? 2 (2)
2/3? 0 (2)

Then 25 of the three-binary is represented as221,1 3 0+2 3 1+2 3 2=25 1?30+2?31+2?32=25

def base(x, m):    ms = []    while x: ms.append(x%m) x //= m # python 3 # //:表示整除,保留整数部分 // /:得float类型 return ms

Python data structure: conversion exploration

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.