python数据结构:进制转化探索

*********************************第一部分*******************************************************************
***********************************************************************************************************************
# 输入excel的行号,输出对应列的序号(从0开始编号)
# 相当于:26进制用a-z表示,将其转化为10进制,即:输入a输出0,输入b输出1,输入aa输出26.。。。
strs = input('请输入1-2个字母组成的字符串:').lower()
list_char = list(strs)
sum = 0
def sub_str(str_a, str_b):
return ord(str_a)-ord(str_b)
for index, str in enumerate(list_char[::-1]):
sum += (sub_str(str, 'a')+1) * 26 ** index
print(sum-1)
print(list_char[::-1])
*********************************第二部分*******************************************************************
***********************************************************************************************************************
题目解读:
即 A=0,Z=25
则Z-A=25
根据进制换算,得到以下公式:
A = (A-A+1)*26^0 -1 = 0
Z = (Z-A+1)*26^0 -1 = 25
AA = (A-A+1)*26^1 + (A-A+1)*26^0 -1 = 26
AZ = (A-A+1)*26^1 + (Z-A+1)*26^0 -1 = 51
BA = (B-A+1)*26^1 + (A-A+1)*26^0 -1 = 52
ZA = (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
如果输入:ZA,那么list_char = ['Z','A'],索引为0,1
enumerate(list_char[::-1]反转了列表,即['A','Z'],目的是反转索引
*********************************第三部分*******************************************************************
***********************************************************************************************************************
那么,下面由这种方法,将十六进制输出为十进制
#!/usr/bin/env/python35
# -*- coding:utf-8 -*-
# author: Keekuun
# 16进制就是逢16进1
def distance(num1, num2):
# 求与第一个数(0)的距离
if ord(num1) >= 65:
# 输入的是A,B,C,D,E
return ord(num1) - ord(num2) - 7
else:
# 输入的是0-9
return ord(num1) - ord(num2)
def sixteen_to_ten(num):
result = 0
for index, value in enumerate(num[::-1]):
# 分别将各个位数转化为10进制,然后求和
result += distance(value, str('0')) * 16 ** index
# print('result=%s'%result)
return result
num = list(input('请输入16进制数(不添加0x):').upper())
print(sixteen_to_ten(num))
*********************************第四部分**************************************************************************
***********************************************************************************************************************
  • 十进制:decimal system,每一位最高为9,达不到10

  • 二进制:binary system,每一位最高为1,达不到2

  • 八进制:octonary number system,每一位最高为7,达不到8

  • 十六进制:hexadecimal,每一位最高为 1515 或者 0xf0xf,取不到16(那就是0xG0xG了,:-D)。

推论:

  • 如果一个数为25,则它的进制不低于6进制;

  • 自然也可以这样理解,如果一个数的某一位的取值范围为 [0,m1][0,m−1],则该数即为 mm进制;

>>> 0b1010
10 # 也即python原生语法是支持二进制表示
>> 0xff 255 # 自然也支持八进制

向十进制转换

int(string, base)# 第一个参数标识:需要转换的原始的数据,以字符串的形式表示# 第二个参数标识:原始数据的base或者叫本身的进制表示 # 2:二进制 # 8:八进制 # 16:表示16进制 # 最终转化为十进制

 

二进制 ⇒ 十进制

>>> int('1010', 2)
10

 

十六进制 ⇒ 十进制

>>> int('f', 16)
15
>>> int('0xf', 16) 15 >>> int('0xff', 16) 255

 

八进制 ⇒ 十进制

>>> int('17', 8)
15  # 15 = 7*8^0+1*8^1

 

向16进制转化

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

 

向二进制转换

bin(十进制整型)
>>> bin(10)
'0b1010'>>> bin(int('ff',16)) '0b11111111' >>> bin(int('17',8)) '0b1111'

向八进制转换

oct()# 不同于hex/bin# 通过参数进行判断# 其是二进制、十进制、16进制# 也即oct函数可将任意进制的数转换为8进制
>>> oct(0b1010)
'012'
>>> oct(11) '013' >>> oct(0xf) '017'

向 m进制 的转换

不断的对m求模取余,余数为当前位(低位向高位),商新的被除数,支持商为0。

例,我们以十进制的25向3进制转换;

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

则25的三进制表示为 221130+231+232=251⋅30+2⋅31+2⋅32=25

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

转载于:https://www.cnblogs.com/zkkysqs/p/9175209.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/389455.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Keras框架:人脸检测-mtcnn思想及代码

人脸检测-mtcnn 概念: MTCNN,英文全称是Multi-task convolutional neural network,中文全称是多任务卷积神经网络, 该神经网络将人脸区域检测与人脸关键点检测放在了一起。 从工程实践上,MTCNN是一种检测速度和准确率…

python中格式化字符串_Python中所有字符串格式化的指南

python中格式化字符串Strings are one of the most essential and used datatypes in programming. It allows the computer to interact and communicate with the world, such as printing instructions or reading input from the user. The ability to manipulate and form…

Javassist实现JDK动态代理

提到JDK动态代理,相信很多人并不陌生。然而,对于动态代理的实现原理,以及如何编码实现动态代理功能,可能知道的人就比较少了。接下一来,我们就一起来看看JDK动态代理的基本原理,以及如何通过Javassist进行模…

数据图表可视化_数据可视化如何选择正确的图表第1部分

数据图表可视化According to the World Economic Forum, the world produces 2.5 quintillion bytes of data every day. With so much data, it’s become increasingly difficult to manage and make sense of it all. It would be impossible for any person to wade throug…

Keras框架:实例分割Mask R-CNN算法实现及实现

实例分割 实例分割(instance segmentation)的难点在于: 需要同时检测出目标的位置并且对目标进行分割,所以这就需要融合目标检测(框出目标的位置)以及语义分割(对像素进行分类,分割…

机器学习 缺陷检测_球检测-体育中的机器学习。

机器学习 缺陷检测🚩 目标 (🚩Objective) We want to evaluate the quickest way to detect the ball in a sport event in order to develop an Sports AI without spending a million dollars on tech or developers. Quickly we find out that detec…

莫烦Pytorch神经网络第二章代码修改

import torch import numpy as np""" Numpy Torch对比课程 """ # #tensor与numpy格式数据相互转换 # np_data np.arange(6).reshape((2,3)) # print(np_data) # # torch_data torch.from_numpy(np_data) # print(\n,torch_data) # # tensor2ar…

自定义字符类

当 VC不使用MFC,无法使用属于MFC的CString,为此自定义一个,先暂时使用,后续完善。 头文件: #pragma once#define MAX_LOADSTRING 100 // 最大字符数class CString {public:char *c_str, cSAr[MAX_LOADSTRING];WCHAR *w…

使用python和javascript进行数据可视化

Any data science or data analytics project can be generally described with the following steps:通常可以通过以下步骤来描述任何数据科学或数据分析项目: Acquiring a business understanding & defining the goal of a project 获得业务理解并定义项目目…

Android 事件处理

事件就是用户对图形的操作,在android手机和平板电脑上,主要包含物理按键事件和触摸屏事件两类。物理按键事件包含:按下、抬起、长按等;触摸屏事件主要包含按下、抬起、滚动、双击等。 在View中提供了onTouchEvent()方法&#xff0…

莫烦Pytorch神经网络第三章代码修改

3.1Regression回归 import torch import torch.nn.functional as F from torch.autograd import Variable import matplotlib.pyplot as plt""" 创建数据 """x torch.unsqueeze(torch.linspace(-1,1,100),dim1) y x.pow(2) 0.2*torch.rand(x…

为什么饼图有问题

介绍 (Introduction) It seems as if people are split on pie charts: either you passionately hate them, or you are indifferent. In this article, I am going to explain why pie charts are problematic and, if you fall into the latter category, what you can do w…

New Distinct Substrings(后缀数组)

New Distinct Substrings&#xff08;后缀数组&#xff09; 给定一个字符串&#xff0c;求不相同的子串的个数。\(n<50005\)。 显然&#xff0c;任何一个子串一定是后缀上的前缀。先&#xff08;按套路&#xff09;把后缀排好序&#xff0c;对于当前的后缀\(S_i\)&#xff0…

Android dependency 'com.android.support:support-v4' has different version for the compile (26.1.0...

在项目中加入react-native-camera的时候 出现的错误. 解决方案: 修改 implementation project(:react-native-camera)为 implementation (project(:react-native-camera)) {exclude group: "com.android.support"}查看原文 Could not find play-services-basement.aa…

先知模型 facebook_使用Facebook先知进行犯罪率预测

先知模型 facebookTime series prediction is one of the must-know techniques for any data scientist. Questions like predicting the weather, product sales, customer visit in the shopping center, or amount of inventory to maintain, etc - all about time series …

莫烦Pytorch神经网络第四章代码修改

4.1CNN卷积神经网络 import torch import torch.nn as nn from torch.autograd import Variable import torch.utils.data as Data import torchvision import matplotlib.pyplot as pltEPOCH 1 BATCH_SIZE 50 LR 0.001 DOWNLOAD_MNIST False #如果数据集已经下载到…

github gists 101使代码共享漂亮

If you’ve been going through Medium, looking at technical articles, you’ve undoubtedly seen little windows that look like the below:如果您一直在阅读Medium&#xff0c;并查看技术文章&#xff0c;那么您无疑会看到类似于以下内容的小窗口&#xff1a; def hello_…

loj #6278. 数列分块入门 2

题目 题解 区间修改&#xff0c;询问区间小于c的个数。分块排序&#xff0c;用vector。至于那个块的大小&#xff0c;好像要用到均值不等式 我不太会。。。就开始一个个试&#xff0c;发现sizsqrt(n)/4时最快&#xff01;&#xff01;&#xff01;明天去学一下算分块复杂度的方…

基于Netty的百万级推送服务设计要点

1. 背景1.1. 话题来源最近很多从事移动互联网和物联网开发的同学给我发邮件或者微博私信我&#xff0c;咨询推送服务相关的问题。问题五花八门&#xff0c;在帮助大家答疑解惑的过程中&#xff0c;我也对问题进行了总结&#xff0c;大概可以归纳为如下几类&#xff1a;1&#x…

莫烦Pytorch神经网络第五章代码修改

5.1动态Dynamic import torch from torch import nn import numpy as np import matplotlib.pyplot as plt# torch.manual_seed(1) # reproducible# Hyper Parameters INPUT_SIZE 1 # rnn input size / image width LR 0.02 # learning rateclass…