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,m?1][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的三進制表示為?2211?30+2?31+2?32=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.pswp.cn/news/389455.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/389455.shtml
英文地址,請注明出處:http://en.pswp.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…