我注意到我写的一个Python脚本是松散的,并将其追溯到一个无限循环,循环条件是while line is not ''.在调试器中运行它,事实证明该行''.当我改为!=''而不是is not '',它工作得很好.
另外,通常认为默认情况下使用'=='会更好,即使在比较int或Boolean值时也是如此?我一直喜欢使用'是'因为我发现它更美观和pythonic(这就是我陷入这个陷阱...),但我想知道它是否只是为了保留当你关心找到两个具有相同id的对象.
我认为python中的最大整数可以通过调用获得sys.maxint.
最大的是什么float或long在Python?
注意:虽然接受的答案达到了我想要的结果,并且@ecatmur答案提供了更全面的选项,但我觉得强调我的用例首先是一个坏主意是非常重要的.这在@Jason Orendorff的答案中得到了很好的解释.
注意:这个问题不是关于的问题sys.maxint的重复.它与此无关sys.maxint; 即使在sys.maxint可用的python 2中,它也不代表最大整数(参见接受的答案).
我需要创建一个比任何其他整数都大的整数,这意味着一个int对象True在与任何其他int对象相比时返回>.用例:库函数需要一个整数,强制某种行为的唯一简单方法是传递一个非常大的整数.
在python 2中,我可以使用sys.maxint(编辑:我错了).在python 3中,math.inf是最接近的等价物,但我无法将其转换为int.
每当我的选项超出一定限度时,pyyaml会将其转换为两行.
怎么避免这个?
例如
在[1]中:x =" - c/home/user/test/test2/test23/tet/2s/test1/stest/longdirectory1/directory2/--optnion12 --verbose"
在[2]中: import yaml
在[3]中: print yaml.dump([dict(ATTRIBUTES=[dict(CONFIG=x)])], default_flow_style=False)
错了一个
- ATTRIBUTES:
- CONFIG: -c /home/user/test/test2/test23/tet/2s/test1/stest/longdirectory1/directory2/
--optnion12 --verbose
Run Code Online (Sandbox Code Playgroud)
应该是这样的
- ATTRIBUTES:
- CONFIG: -c /home/user/test/test2/test23/tet/2s/test1/stest/longdirectory1/directory2/ --optnion12 --verbose
Run Code Online (Sandbox Code Playgroud) 我想知道如何定义inf和-inf作为intPython 2.7.我想,似乎inf并-inf只能作为一个工作float.
a = float('-inf') # works
b = float('inf') # works
c = int('-inf') # compile error, ValueError: invalid literal for int() with base 10: 'inf'
d = int('inf') # compile error, ValueError: invalid literal for int() with base 10: 'inf'
Run Code Online (Sandbox Code Playgroud) 我试图从leetcode解决这个问题,为了方便起见,这里要复制
Given an integer array, find three numbers whose product is maximum and output the maximum product.
Example 1:
Input: [1,2,3]
Output: 6
Example 2:
Input: [1,2,3,4]
Output: 24
Note:
The length of the given array will be in range [3,104] and all elements are in the range [-1000, 1000].
Multiplication of any three numbers in the input won't exceed the range of 32-bit signed integer.
Run Code Online (Sandbox Code Playgroud)
在(尝试不成功)尝试之后,我搜索了解决方案,这是有效的
class Solution(object):
def maximumProduct(self, nums):
"""
:type nums: List[int]
:rtype: int …Run Code Online (Sandbox Code Playgroud) 与列表中给出的数字相比,您将如何找到最接近的数字?
这是我迄今为止尝试过的,但没有成功:
setted_list = [2, 9, 6, 20, 15]
value_chosen = 17
while True:
final_value = setted_list[0]
if setted_list[1] - value_chosen < setted_list[0] - value_chosen:
final_value = setted_list[1]
if setted_list[2] - value_chosen < setted_list[1] - value_chosen:
final_value = setted_list[2]
if setted_list[3] - value_chosen < setted_list[2] - value_chosen:
final_value = setted_list[3]
if setted_list[4] - value_chosen < setted_list[3] - value_chosen:
final_value = setted_list[4]
print(final_value)
Run Code Online (Sandbox Code Playgroud)
我的输出始终是setted_list[2]. 我的算法哪里出错了?
我从中学到了inf:如何在Python中表示无限数?
我希望找到包含'inf'的数组的最小值
In [330]: residuals
Out[330]:
array([[ 2272.35651718, 1387.71126686, 1115.48728484],
[ 695.08009848, inf, inf],
[ 601.44997093, inf, inf]])
In [331]: min(residuals)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-331-69b09e4201cf> in <module>()
----> 1 min(residuals)
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Run Code Online (Sandbox Code Playgroud)
然而,似乎'inf'是模棱两可的?有没有一种聪明的方法来找到最小值而不是在这个数组的每个值上运行一个循环?
我试图在Python中做零对数.
from math import log
log(0)
Run Code Online (Sandbox Code Playgroud)
这引起了我的例外.
ValueError: math domain error
Run Code Online (Sandbox Code Playgroud)
我理解log一个负数在数学上是未定义的,这就是Python log可以引发这个ValueError异常的原因.但是为什么在用零值调用它时也会引发异常呢?它应该返回一个-inf值,我已经看到无限数字可以在Python中表示(如此处所示).
我可以在没有亲自对待的情况下处理这个问题吗?我的意思是在做这样的事情时亲自.
from math import log, inf
foo = ... # Random value.
if foo != 0: return log(foo)
else: return -inf
Run Code Online (Sandbox Code Playgroud) python ×9
algorithm ×2
infinity ×2
python-2.7 ×2
comparison ×1
dump ×1
equality ×1
int ×1
line-breaks ×1
logarithm ×1
long-integer ×1
long-lines ×1
max ×1
python-3.x ×1
string ×1
valueerror ×1
yaml ×1