使用循环和条件搜索 python 个列表
Searching python lists with loops and conditionals
def ListCreate(*args):
items = none
for i in range(len(args)-1,-1,-1):
items = join(args[i], items)
return items
a = ListCreate(1,2,3,4,5,6)
b = ListCreate(20150602, 20150603, 20150604, 20150605, 20150606, 20150607)
python中的列表是有序的,所以我希望列表a中的element 0
与列表b中的element 0
相对应。
即
(1,20150602)(2,20150603)(3,20150604)
#etc
现在,我想创建一个搜索循环,要求 if a(n)==a(n+1) and b(n+1) - b(n) <= 3
...打印条件保持为真的所有元素。
很难理解您想要的输出,但这是我最好的猜测:
In [39]:
a = [1,1,3,4,4,6]
b = [20150602, 20150603, 20150604, 20150605, 20150606, 20150607]
c = zip(a, b)
print c
for i in range(0, len(c)-1):
if c[i][0] == c[i+1][0] and c[i+1][1] - c[i][1] <= 3:
print c[i]
[(1, 20150602), (1, 20150603), (3, 20150604), (4, 20150605), (4, 20150606), (6, 20150607)]
(1, 20150602)
(4, 20150605)
def ListCreate(*args):
items = none
for i in range(len(args)-1,-1,-1):
items = join(args[i], items)
return items
a = ListCreate(1,2,3,4,5,6)
b = ListCreate(20150602, 20150603, 20150604, 20150605, 20150606, 20150607)
python中的列表是有序的,所以我希望列表a中的element 0
与列表b中的element 0
相对应。
即
(1,20150602)(2,20150603)(3,20150604)
#etc
现在,我想创建一个搜索循环,要求 if a(n)==a(n+1) and b(n+1) - b(n) <= 3
...打印条件保持为真的所有元素。
很难理解您想要的输出,但这是我最好的猜测:
In [39]:
a = [1,1,3,4,4,6]
b = [20150602, 20150603, 20150604, 20150605, 20150606, 20150607]
c = zip(a, b)
print c
for i in range(0, len(c)-1):
if c[i][0] == c[i+1][0] and c[i+1][1] - c[i][1] <= 3:
print c[i]
[(1, 20150602), (1, 20150603), (3, 20150604), (4, 20150605), (4, 20150606), (6, 20150607)]
(1, 20150602)
(4, 20150605)