日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python取反函数_Python优雅的反函数int(string,base)

發布時間:2024/4/19 python 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python取反函数_Python优雅的反函数int(string,base) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我曾經用同樣的目標編寫了自己的函數,但現在卻令人尷尬地復雜化了。

from math import log, ceil, floor

from collections import deque

from itertools import repeat

from string import uppercase, digits

import re

__alphanumerals = (digits + uppercase)

class InvalidBaseError(ValueError): pass

class FloatConvertError(ValueError): pass

class IncorrectBaseError(ValueError): pass

def getbase(number, base=2, frombase = 10):

if not frombase == 10:

number = getvalue(number, frombase)

#getvalue is also a personal function to replicate int(number, base)

if 1 >= base or base >= len(__alphanumerals) or not floor(base) == base:

raise InvalidBaseError("Invalid value: {} entered as base to convert

to. n{}".format(base,

"Assert that the base to convert to is a decimal integer."))

if isinstance(number, str):

try:

number = atof(number)

except ValueError:

#The first check of whether the base is 10 would have already corrected the number

raise IncorrectBaseError("Incorrect base passed as base of number -> number: {} base: {}".format(number, frombase))

#^ v was supporting float numbers incase number was the return of another operation

if number > floor(number):

raise FloatConvertError("The number to be converted must not be a float. {}".format(number))

isNegative = False

if number < 0:

isNegative = True

number = abs(number)

logarithm = log(number, base) if number else 0 #get around number being zero easily

ceiling = int(logarithm) + 1

structure = deque(repeat(0, ceiling), maxlen = ceiling)

while number:

if number >= (base ** int(logarithm)):

acceptable_digit = int(number / (base ** floor(logarithm)))

structure.append(acceptable_digit if acceptable_digit < 10 else __alphanumerals[acceptable_digit])

number -= acceptable_digit * (base ** floor(logarithm))

else:

structure.append(0)

logarithm -= 1

while structure[0] == 0:

#the result needs trailing zeros

structure.rotate(-1)

return ("-" if isNegative and number else "") + reduce(lambda a, b: a + b, map(lambda a: str(a), structure))

我認為函數strbase應該只支持base> = 2和&lt; = 36以防止與python中的其他工具沖突,例如int。

另外,我認為只應該使用一個字母大小寫字母,最好是大寫,以防止與其他函數如int沖突,因為它會認為“a”和“A”都是10。

from string import uppercase

dig_to_chr = lambda num: str(num) if num < 10 else uppercase[num - 10]

def strbase(number, base):

if not 2 <= base <= 36:

raise ValueError("Base to convert to must be >= 2 and <= 36")

if number < 0:

return "-" + strbase(-number, base)

d, m = divmod(number, base)

if d:

return strbase(d, base) + dig_to_chr(m)

return dig_to_chr(m)

總結

以上是生活随笔為你收集整理的python取反函数_Python优雅的反函数int(string,base)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。