0
lambda x : '%x' % x

関数は 10 進数から 16 進数ですが、原理は何ですか? 私はPythonの初心者です、事前に感謝します

4

2 に答える 2

4

文字列形式の表記では、'%x' は 16 進出力のプレースホルダーです。

この関数は値を受け取り、それを 16 進文字列としてフォーマットして返します。

「10進数から16進数へ」ではなく、「(与えられたものは何でも)16進数表記の文字列として返す」のです。

例えば、

print '%x' % 0b11111111   # -> 'ff'  (from binary)
print '%x' % 0377         # -> 'ff'  (from octal)
print '%x' % 255          # -> 'ff'  (from decimal)
print '%x' % 0xff         # -> 'ff'  (from hex)
于 2012-06-24T11:31:38.547 に答える
1
a = 255

#use a hexadecimal format string to display the value of a - prints ff
print "%x" % a 

#create a function that takes a value and returns its hexadecimal representation
tohex = lambda x : '%x' % x

#call the function - prints ff
print tohex(255)
于 2012-06-24T11:30:16.987 に答える