Python 中 isnumeric 和 isdecimal 的区别
Difference between isnumeric and isdecimal in Python
字符串的 isnumeric 和 isdecimal 函数有什么区别 (https://www.tutorialspoint.com/python3/python_strings.htm)?他们似乎给出了相同的结果:
>>> "123456".isnumeric()
True
>>> "123456".isdecimal()
True
>>> "123.456".isnumeric()
False
>>> "123.456".isdecimal()
False
>>> "abcd".isnumeric()
False
>>> "abcd".isdecimal()
False
我预计“123.456”的 isdecimal()
到 return 正确,但事实并非如此。
这两种方法测试特定的 Unicode 字符 classes。如果字符串中所有个字符都来自指定字符class(具有特定的Unicode 属性),则测试为真。
isdecimal()
不测试字符串是否为十进制数。见 documentation:
Return true if all characters in the string are decimal characters and there is at least one character, false otherwise. Decimal characters are those that can be used to form numbers in base 10, e.g. U+0660, ARABIC-INDIC DIGIT ZERO. Formally a decimal character is a character in the Unicode General Category “Nd”.
.
句点字符不是 Nd
类别的成员;它不是十进制字符。
str.isdecimal()
个字符是 str.isnumeric()
的子集;这将测试所有数字字符。同样,来自 documentation:
Return true if all characters in the string are numeric characters, and there is at least one character, false otherwise. Numeric characters include digit characters, and all characters that have the Unicode numeric value property, e.g. U+2155, VULGAR FRACTION ONE FIFTH. Formally, numeric characters are those with the property value Numeric_Type=Digit, Numeric_Type=Decimal or Numeric_Type=Numeric.
Nd
这里是 Numeric_Type=Digit
。
如果你想测试一个字符串是否是一个有效的十进制数,只需尝试将它转换为一个浮点数:
def is_valid_decimal(s):
try:
float(s)
except ValueError:
return False
else:
return True
数字字符比十进制字符多很多:
>>> "٣".isdecimal()
True
>>> "¼".isdecimal()
False
>>> "¼".isnumeric()
True
字符串的 isnumeric 和 isdecimal 函数有什么区别 (https://www.tutorialspoint.com/python3/python_strings.htm)?他们似乎给出了相同的结果:
>>> "123456".isnumeric()
True
>>> "123456".isdecimal()
True
>>> "123.456".isnumeric()
False
>>> "123.456".isdecimal()
False
>>> "abcd".isnumeric()
False
>>> "abcd".isdecimal()
False
我预计“123.456”的 isdecimal()
到 return 正确,但事实并非如此。
这两种方法测试特定的 Unicode 字符 classes。如果字符串中所有个字符都来自指定字符class(具有特定的Unicode 属性),则测试为真。
isdecimal()
不测试字符串是否为十进制数。见 documentation:
Return true if all characters in the string are decimal characters and there is at least one character, false otherwise. Decimal characters are those that can be used to form numbers in base 10, e.g. U+0660, ARABIC-INDIC DIGIT ZERO. Formally a decimal character is a character in the Unicode General Category “Nd”.
.
句点字符不是 Nd
类别的成员;它不是十进制字符。
str.isdecimal()
个字符是 str.isnumeric()
的子集;这将测试所有数字字符。同样,来自 documentation:
Return true if all characters in the string are numeric characters, and there is at least one character, false otherwise. Numeric characters include digit characters, and all characters that have the Unicode numeric value property, e.g. U+2155, VULGAR FRACTION ONE FIFTH. Formally, numeric characters are those with the property value Numeric_Type=Digit, Numeric_Type=Decimal or Numeric_Type=Numeric.
Nd
这里是 Numeric_Type=Digit
。
如果你想测试一个字符串是否是一个有效的十进制数,只需尝试将它转换为一个浮点数:
def is_valid_decimal(s):
try:
float(s)
except ValueError:
return False
else:
return True
数字字符比十进制字符多很多:
>>> "٣".isdecimal()
True
>>> "¼".isdecimal()
False
>>> "¼".isnumeric()
True