小编Cat*_*ami的帖子

"&"与"&&"实际上是否会对编译时标志产生影响?

我习惯在编译时标志中使用以下语法:

#if (defined(A) & defined(B))
Run Code Online (Sandbox Code Playgroud)

通常建议我这样做&&如下:

#if (defined(A) && defined(B))
Run Code Online (Sandbox Code Playgroud)

我知道两个运算符之间的区别,并且在正常代码中 &&会发生短路.但是,以上都是由编译器处理的.甚至重要的是我用的是什么?它是否会影响编译时间的某些无穷小量,因为它不会评估第二个define()

c c++ compilation

20
推荐指数
2
解决办法
2396
查看次数

为什么 Python 2.7 报告的上限值不正确?

我最近将一个脚本从 3.4 转换为使用 Python 2.7(应一位同事的要求),我发现 math.ceil 函数无法正常工作......

当我除以 5/2 并用 math.ceil() 取整时,我希望得到 math.ceil(5/2) = 3。

鉴于以下代码:

from __future__ import print_function
import math

a = 5
b = 2
c = math.ceil(a/b)

print("Ceiling output: {}".format(c))
Run Code Online (Sandbox Code Playgroud)

Python 2.7 将报告 2 作为答案,Python 3.4 按预期报告 3。

为什么是这样?

我知道如果我投射ab作为,我可以让 2.7 工作float

c = math.ceil(float(a)/float(b)
Run Code Online (Sandbox Code Playgroud)

但是,投射除法也不起作用:

c = math.ceil(float(a/b))
Run Code Online (Sandbox Code Playgroud)

3.4 如何解决这个问题?

发现这一点让我怀疑我需要在脚本的 2.7 版本中重新检查多少数学。

python rounding python-2.7 python-3.x

2
推荐指数
1
解决办法
745
查看次数

如何使用生成器表达式合并Python中的多个集合?

我想加入从类的实例获得的多个集合。下面是我正在使用的内容和尝试过的示例。改变班级不是一个选择。

class Salad:
   def __init__(self, dressing, veggies, others):
      self.dressing = dressing
      self.veggies = veggies
      self.others = others

SALADS = {
   'cesar'  : Salad('cesar',  {'lettuce', 'tomato'},  {'chicken', 'cheese'}),
   'taco'   : Salad('salsa',  {'lettuce'},            {'cheese', 'chili', 'nachos'})
}
Run Code Online (Sandbox Code Playgroud)

我想OTHER_INGREDIENTS成为{'chicken', 'cheese', 'chili', 'nachos'}。所以我尝试:

OTHER_INGREDIENTS = sum((salad.others for salad in SALADS.values()), set())
Run Code Online (Sandbox Code Playgroud)

这给了我一个错误“+ 不支持的操作数类型:‘set’和‘set’。我该怎么做?

如果可能的话,我更愿意使用 Python 2.7,而不需要额外的导入。

python set

2
推荐指数
2
解决办法
4697
查看次数

标签 统计

python ×2

c ×1

c++ ×1

compilation ×1

python-2.7 ×1

python-3.x ×1

rounding ×1

set ×1