为什么 python 从字符串中返回数字?

Why is python returning a numbers from a string?

好的,这完全让我感到困惑,我正在打印一个字符串及其返回的数字。 我不明白它是如何从字符串中返回数字的。

这是代码片段。

string = "String"
print int(string[0:min(5,len(string))],36)

该片段的输出是

48417935

我的朋友告诉我这与计算机从字符串生成数字有关,但我很困惑。

有人可以解释一下为什么会这样吗?

你正在拍这片

>>> string[0:min(5,len(string))]
'Strin'

并将其转换为 base36 数字(类似于 hexadecimal 但使用全部 26 个字母)

>>> int('Strin', 36)
48417935

得出该数字的另一种方法是:

>>> ["0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".index(x) for x in 'STRIN']
[28, 29, 27, 18, 23]
>>> 28*36**4 + 29*36**3 + 27*36**2 + 18*36**1 + 23*36**0
48417935

您正在打印 int 方法的 return 值,return 是一个数字,而不是字符串。

您正在使用 int 内置函数将 string 转换为 integer

来自,Python docs

class int(x=0) class int(x, base=10)

Return an integer object constructed from a number or string x, or return 0 if no arguments are given. If x is a number, it can be a plain integer, a long integer, or a floating point number. If x is floating point, the conversion truncates towards zero. If the argument is outside the integer range, the function returns a long object instead.

If x is not a number or if base is given, then x must be a string or Unicode object representing an integer literal in radix base. Optionally, the literal can be preceded by + or - (with no space in between) and surrounded by whitespace. A base-n literal consists of the digits 0 to n-1, with a to z (or A to Z) having values 10 to 35. The default base is 10. The allowed values are 0 and 2-36. Base-2, -8, and -16 literals can be optionally prefixed with 0b/0B, 0o/0O/0, or 0x/0X, as with integer literals in code. Base 0 means to interpret the string exactly as an integer literal, so that the actual base is 2, 8, 10, or 16.

如果您将代码分解成小片段,它对您来说将更具可读性和清晰性::

string = "String"
len_of_str = len(string) # get length of "string" i.e., 6
var_a_minimum = min(5, len_of_str) # get the minimum of 5 or len_of_str
var_x_string = string[0:var_a_minimum ] # slice the string from 0 to var_a_minimum 
var_y_integer = int(var_x_string,36) # get the integer value of var_x_string to the base-36
print var_y_integer 

你的第一行代码创建了一个变量字符串

string = "String"

让您的打印语句更易于理解。

length=len(string)   #finds the length of text in string variable in this case 6

那么你正在做 min(5,length)。该表达式找到 5 到 6 之间的最小整数。因此它的计算结果为 5。

最后是 string[0:5] 这会分割字符串并获取从 0 到 4.So 的所有字符,在您的示例中它的计算结果为 "Strin" Read about slicing here

然后最后你在做 print int("Strin",36) 这将 return 一个 base36 整数。 Read about int() here

您应该始终逐个尝试 python shell 中让您感到困惑的内容,这样您就可以了解正在发生的事情。使用更好的 shell,例如 ipython,并自由地使用其 ? 来查看后台发生的情况。 Python 是一种 "self documenting" 语言。

让我们逐个分析您的代码:

print int(string[0:min(5,len(string))],36)

好的,让我们从 min(5,len(string))

开始
In [2]: string = "String"

In [3]: min(5,len(string))
Out[3]: 5

In [4]: min?
Docstring:
min(iterable[, key=func]) -> value
min(a, b, c, ...[, key=func]) -> value

With a single iterable argument, return its smallest item.
With two or more arguments, return the smallest argument.
Type:      builtin_function_or_method

不言自明。

好吧,让我们向前迈出一步:

string[0:min(5,len(string))]

我们已经从 min() 调用中得到了一个值,所以这归结为:

string[0:5]

正如我们已经从 python 的列表切片方式中,它将 return 字符串的 5 个元素从 string[0] 开始并以 string[4] 结束.

所以在我们给定的字符串的情况下它将 return :

In [5]: string[0:min(5,len(string))]
Out[5]: 'Strin'

现在 int('Strin',36) 是什么意思?

让我们回到 shell :

In [6]: int??
Docstring:
int(x=0) -> int or long
int(x, base=10) -> int or long

Convert a number or string to an integer, or return 0 if no arguments
are given.  If x is floating point, the conversion truncates towards zero.
If x is outside the integer range, the function returns a long instead.

所以它将它转换为基于 36 的数字系统中的数字。让我们最后一次看看默认调用...

In [7]: int('Strin')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-7-a13c6c79aa49> in <module>()
----> 1 int('Strin')

ValueError: invalid literal for int() with base 10: 'Strin'

这是预料之中的,因为 10 进制数字系统没有符号 ST 等。16 进制有额外的符号 AF。所以这意味着 base 36 系统将有 36-10=26 个符号。这意味着它将拥有所有英文字母作为它的符号 table。这就是为什么它不会引发异常并且能够将任何字符串文字转换为数字表示形式的原因。

string[0:min(5,len(string))]的产物是Strin。然后你对它使用 int(),这意味着你试图让一个字符串成为一个 int。正如 documentation for int() 所述,

f x is not a number or if base is given, then x must be a string or Unicode object representing an integer literal in radix base. Optionally, the literal can be preceded by + or - (with no space in between) and surrounded by whitespace. A base-n literal consists of the digits 0 to n-1, with a to z (or A to Z) having values 10 to 35.

integer literal 被维基百科定义为:

an integer literal is an integer whose value is directly represented in source code. For example, in the assignment statement x = 1, the string 1 is an integer literal indicating the value 1, while in the statement x = 0x10 the string 0x10 is an integer literal indicating the value 16, which is represented by 10 in hexadecimal (indicated by the 0x prefix).

By contrast, in x = cos(0), the expression cos(0) evaluates to 1 (as the cosine of 0), but the value 1 is not literally included in the source code. More simply, in x = 2 + 2, the expression 2 + 2 evaluates to 4, but the value 4 is not literally included. Further, in x = "1" the "1" is a string literal, not an integer literal, because it is in quotes. The value of the string is 1, which happens to be an integer string, but this is semantic analysis of the string literal – at the syntactic level "1" is simply a string, no different from "foo".

所以解释器接受 'Strin' 并使用 36 进制将其计算为一个数字。你可以尝试一下,看看你至少需要 30 进制才能不抛出错误,因为 0 - 9和a-t一共30个字符