整数与整数的比较或在集合或元组中的存在
Comparison of integer against integer or presence in a set or tuple
我遇到了一个问题,我尝试使用 or 将整数类型的变量与其他两个整数进行比较,但没有意识到这是不可能的。
原码
model = 4004
if model == 3002 or 3003:
print("model be 300X")
elif model == 4004 or 4003:
print("model be 400X")
else:
print("model type not covered")
Returns
300X
#什么时候应该是400X.
我还有另外两个解决方法,在我看来这两个都不是 pythonic,但它们有效。
有更好的方法吗?
工作代码
import itertools
model = 4004
if model in (3002, 3003):
print("model be 300X")
cycle_dict = {x: z for x, z in zip(range(1, 1001, 1), itertools.cycle(["C", "C", "A", "A", "B", "B"]))}
elif model in (4004, 4003):
print("model be 400X")
cycle_dict = {x: z for x, z in zip(range(1, 2001, 1), itertools.cycle(["C", "C", "C", "C", "A", "A", "A", "A", "B", "B", "B", "B"]))}
else:
print("model type not covered")
if model == 3002:
print("this is a 3002")
cycle_dict = {x: z for x, z in zip(range(1, 1001, 1), itertools.cycle(["C", "C", "A", "A", "B", "B"]))}
if model == 3003:
print("this is a 3003")
cycle_dict = {x: z for x, z in zip(range(1, 1001, 1), itertools.cycle(["C", "C", "A", "A", "B", "B"]))}
if model == 4004:
print("this is a 4004")
cycle_dict = {x: z for x, z in zip(range(1, 2001, 1), itertools.cycle(["C", "C", "C", "C", "A", "A", "A", "A", "B", "B", "B", "B"]))}
if model == 4003:
print("this is a 4003")
cycle_dict = {x: z for x, z in zip(range(1, 2001, 1), itertools.cycle(["C", "C", "C", "C", "A", "A", "A", "A", "B", "B", "B", "B"]))}
print("cycle_dict : ", cycle_dict)
它returns一个字典
cycle_dict : {1: 'C', 2: 'C', 3: 'C', 4: 'C', 5: 'A', 6: 'A', 7: 'A', 8: 'A', 9: 'B', 10: 'B', 11: 'B', 12: 'B', ...
您的初始尝试无效,因为 model == 3002 or 3003
被翻译为 (model == 3002) or 3003
。第一个条件为假,但 3003 为真值。您需要明确使用 model == 3002 or model == 3003
.
>>> model = 4004
>>> model == 3002 or 3003
3003
>>> bool(model == 3002 or 3003)
True
>>> model == 3002 or model == 3003
False
现在,至于用哪种方法,我比较喜欢
model in {a, b}
比元组快,因为 lookup time complexity is O(1)。如果您只有几张支票,这几乎不重要,但要记住这一点。一般来说,value in tuple
或 value in set
需要更少的击键来键入,并且其意图很明确。
根据您的基本逻辑,以下可能更好:
series = model // 1000
if series == 3:
pass
elif series == 4:
pass
我遇到了一个问题,我尝试使用 or 将整数类型的变量与其他两个整数进行比较,但没有意识到这是不可能的。
原码
model = 4004
if model == 3002 or 3003:
print("model be 300X")
elif model == 4004 or 4003:
print("model be 400X")
else:
print("model type not covered")
Returns
300X
#什么时候应该是400X.
我还有另外两个解决方法,在我看来这两个都不是 pythonic,但它们有效。
有更好的方法吗?
工作代码
import itertools
model = 4004
if model in (3002, 3003):
print("model be 300X")
cycle_dict = {x: z for x, z in zip(range(1, 1001, 1), itertools.cycle(["C", "C", "A", "A", "B", "B"]))}
elif model in (4004, 4003):
print("model be 400X")
cycle_dict = {x: z for x, z in zip(range(1, 2001, 1), itertools.cycle(["C", "C", "C", "C", "A", "A", "A", "A", "B", "B", "B", "B"]))}
else:
print("model type not covered")
if model == 3002:
print("this is a 3002")
cycle_dict = {x: z for x, z in zip(range(1, 1001, 1), itertools.cycle(["C", "C", "A", "A", "B", "B"]))}
if model == 3003:
print("this is a 3003")
cycle_dict = {x: z for x, z in zip(range(1, 1001, 1), itertools.cycle(["C", "C", "A", "A", "B", "B"]))}
if model == 4004:
print("this is a 4004")
cycle_dict = {x: z for x, z in zip(range(1, 2001, 1), itertools.cycle(["C", "C", "C", "C", "A", "A", "A", "A", "B", "B", "B", "B"]))}
if model == 4003:
print("this is a 4003")
cycle_dict = {x: z for x, z in zip(range(1, 2001, 1), itertools.cycle(["C", "C", "C", "C", "A", "A", "A", "A", "B", "B", "B", "B"]))}
print("cycle_dict : ", cycle_dict)
它returns一个字典
cycle_dict : {1: 'C', 2: 'C', 3: 'C', 4: 'C', 5: 'A', 6: 'A', 7: 'A', 8: 'A', 9: 'B', 10: 'B', 11: 'B', 12: 'B', ...
您的初始尝试无效,因为 model == 3002 or 3003
被翻译为 (model == 3002) or 3003
。第一个条件为假,但 3003 为真值。您需要明确使用 model == 3002 or model == 3003
.
>>> model = 4004
>>> model == 3002 or 3003
3003
>>> bool(model == 3002 or 3003)
True
>>> model == 3002 or model == 3003
False
现在,至于用哪种方法,我比较喜欢
model in {a, b}
比元组快,因为 lookup time complexity is O(1)。如果您只有几张支票,这几乎不重要,但要记住这一点。一般来说,value in tuple
或 value in set
需要更少的击键来键入,并且其意图很明确。
根据您的基本逻辑,以下可能更好:
series = model // 1000
if series == 3:
pass
elif series == 4:
pass