循环调用数组outsid时

2024-09-27 20:21:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我重新开始,谢谢大家的回复

我有一个日志文件,我采取的条目,并删除它的所有垃圾。 剩下的数组或列表是

23
23.23.23.23
45
45.45.45.45
100
34.34.54.13

我想怎么打每一个电话都是用这个

a = 1
while a < 18:
    a = a + 2
    #logging.debug(line.split(PID_ANDROID)[a])
    countIP = a
    trySomething()

    if a == 20:
        break

但我打电话后必须做些事情。 我想使用第一个入口

> do something 
> see if something is happening
> if its not goto 3rd entry
> try the same thing again.

这就是我一直坚持的。 因为当我从其他内部调用它时,我使用全局存储。 Python告诉我我不能给我们一个str或turp。或者用下面的代码给我一个列表中所有内容的连续输出

我有这个密码

def trySomething():
global countIP
global LOG_SPLITER
#logging.debug('Processing Number: %s' % (countIP,))
logging.debug(LOG_SPLITER.split(PID_ANDROID)[countIP])
time.sleep(.5)
clearScreen()
#grabBox90()
#lineGoto()

我的问题是。 我怎样才能做一个循环,一次只抽出一个来做一些事情,当我完成一个循环时,就转到下一个循环


Tags: 文件debuglog列表iflogging事情pid
2条回答

如果您只想记录line中的每个条目,可以执行以下操作:

entries = line.split(PID_ANDROID)
for e in entries[::2]:  # take every other element
    logging.debug(e)

迭代条目是“更具pythonic性的”

看起来应该使用初始索引为1、步长为2的for循环。或者,对值1使用显式debug语句,然后从3开始循环其余部分,以避免if测试。如果代码的剩余部分是按1而不是2递增,那么这允许您在仍然有循环的情况下正确地执行初始跳过

而不是

c = 1
#do my stuff
while c < 20:
    if c == 1:
        logging.debug(line.split(PID_ANDROID)[c])
        c = + 2
    else:
        logging.debug('Moving on to a refresh')
    # You do not incremennt c
    # c += 2 should go here to increment every time

Python2

for i in xrange(1,20,2):
  # do your processing

Python3

for i in range(1,20,2):
  # do you processing

相关问题 更多 >

    热门问题