假设我有四个数字,比如2
,3
,4
,6
,我想检查一下,它们是在一个数字之前还是之后。例如,如果我选择2
,那么我希望代码说3
就在数字后面;当我选择3
时,代码应该输出2
在3
之前,而4
在3
之后;当我选择4
时,代码应该输出3就在4
之前。如何在Python中实现这一点?
这就是我迄今为止所尝试的
a = [2,3,4,6]
def check(noToCheck, List):
if noToCheck in List:
for elem in List:
if noToCheck == elem+1:
print elem+" comes just before "+noToCheck
elif noToCheck == elem-1:
print elem+" comes just after "+noToCheck
else:
pass
这很管用,但有什么更干净的方法呢
这将接收一个名为num(例如5)的值,然后检查它的相邻数字(例如4和6)是否在数组中,如果在数组中,它将打印它们
相关问题 更多 >
编程相关推荐