关于Python中比较整数符号的函数实现的反馈
我创建了一个小函数,给定一个元组,比较该元组中的所有元素是否具有相同的符号。
例如,tuple = [-1, -4, -6, -8]
是好的,而 [-1, -4, 12, -8]
是坏的。我不确定我是否已经做出了最明智的实施,所以我知道这是提问的地方。
def check_consistent_categories(queryset):
try:
first_item = queryset[0].amount
if first_item < 0:
for item in queryset:
if item > 0:
return False
return True
else:
for item in queryset:
if item < 0:
return False
return True
except:
return False
I've made a small function which, given a tuple, compares if all elements in this tuple is of the same sign.
E.g., tuple = [-1, -4, -6, -8]
is good, while [-1, -4, 12, -8]
is bad. I am not sure I've made the smartest implementation, so I know this is the place to ask.
def check_consistent_categories(queryset):
try:
first_item = queryset[0].amount
if first_item < 0:
for item in queryset:
if item > 0:
return False
return True
else:
for item in queryset:
if item < 0:
return False
return True
except:
return False
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这可能对您有帮助:
您可能想要更改<和>到 <= 和 >= 取决于您想要如何处理 0。
This might help you:
You may want to change < and > to <= and >= depending on how you want to treat 0.
在 @EOL 的解决方案之后,但无需列表索引或多次迭代即可工作。
我也想到了这一点,但没有利用所有/任何短路:
After @EOL's solution, but works without list indexing or iterating multiple times.
This also occurred to me, but doesn't take advantage of all/any short-circuiting:
只是一个不相关的问题,因为您正在这样做:
您忽略了所有异常,请非常小心,我的朋友,这将隐藏很多错误,而不是在进行异常处理时始终保持精确,例如:
等等。在编写更大的 Python 应用程序时请记住这一点。
Just one unrelated nit, since you are doing this:
You are ignoring
all
of the exceptions, be very careful my friend, this will be hiding lots of bugs, instead always be precise while doing exception handling e.g:etc. Keep this in mind while coding a larger python application.
这是一种很好的 Python 方法(使用
all()
):(对于 Python 2.6+,它引入了
math.copysign()
函数)。该解认为 0 为正。不过,马克·拜尔斯的解决方案更加灵活,而且也可以说更加清晰。
Here is a nice pythonic way of doing this (using
all()
):(for Python 2.6+, which introduced the
math.copysign()
function). This solution considers that 0 is positive.Mark Byers' solution is more flexible, though, and it is also arguably more legible.
为什么不利用这样一个事实:如果所有数字的符号相同,那么每个单独数字的绝对值之和将等于每个数字之和的绝对值?
显示数学细节的示例
情况 1: 所有数字都具有相同的符号
两种方法的结果都是 13,因此符号相同。
情况 2: 并非所有数字都具有相同的符号
这些方法会生成不同的数字(11 和 13),因此符号并不全部相同。
Why not take advantage of the fact that if all numbers are the same sign, then the sum of the absolute value of each individual number will be equal to the absolute value of the sum of each number?
Example Showing Details of the Math
Case 1: All numbers have the same sign
Both methods yield 13, so the signs are the same.
Case 2: Not all numbers have the same sign
The methods yield different numbers (11 and 13), so the signs are not all the same.
这是一个也可以与生成器等一起正常工作的方法。
如果
ints
为空,则会出现 StopIteration 异常。此版本将 0 与负数组合在一起。如果您希望与正数分组,请使用
>=
Here is one that works fine with generators etc. too
If
ints
is empty, you get a StopIteration exception.This version gorups 0 with the negative numbers. Use
>=
if you wish to group with the positive numbers instead如果您的数字已排序,您只需比较两端即可。如果不是,您可以对它们进行排序:
如果您将其更改为
>=0
零将被视为符号中性。我不确定这是否是比当前答案更好的实现,但对于大量数据来说它可能会更快。If your numbers are sorted you only need to compare the ends. If not you could sort them:
If you changed this to
>=0
zero would be considered sign neutral. I'm not sure if this is a better implementation than the current answers, but it could be faster for large sets of data.使用这样的原理:如果数字相同,则相乘得出正结果,否则得出负结果,
但这不能处理零......
Using the principal that multiplying your numbers gives a positive result if they all the same, else negative,
This doesn't handle zeros though...