在某些条件下迭代列表

时间:2012-07-06 22:16:22

标签: python python-2.7

我有两个列表,validlocationsvalid包含由字符串编号表示的ID,location包含属于他们所遵循的ID的id +字符串(路径)。

我的目标是检查我的ID是否属于有效组。如果有效ID和以下项目为TRUE,我将调用一些函数。当检测到INAVLID ID时,我应该跳过它并将项目移动到下一个ID。

我的代码是这样的:

valid = ['1', '2', '3', '4', '5', '6', '27', '28', '29']
locationList = ['1', '1_path1','1_path2','1_path3','2', '2_path1','2_path2','2_path3', '55','55_path1','55_path2', '3', '3_path1' ]

for item in locationList:
    if len(item)< 3: 
        if item in valid:
            print "###########lib ID found in item %s############" %item
            print "Call to bring file name function - %s" %item
            continue
        else:  
            continue            
    print "call the fix path function - %s" %item
    print "Call the Search file function -%s" %item

我的疑问是,在else:声明后,我的项目值为'55' ==无效。此时我希望将列表中的项目向前移动到值为下一个ID的位置(在本例中为'3')。

我目前的输出是:

###########lib ID found in item 1############
Call to bring file name function - 1
call the fix path function - 1_path1
Call the Search file function -1_path1
call the fix path function - 1_path2
Call the Search file function -1_path2
call the fix path function - 1_path3
Call the Search file function -1_path3
###########lib ID found in item 2############
Call to bring file name function - 2
call the fix path function - 2_path1
Call the Search file function -2_path1
call the fix path function - 2_path2
Call the Search file function -2_path2
call the fix path function - 2_path3
Call the Search file function -2_path3
call the fix path function - 55_path1
Call the Search file function -55_path1
call the fix path function - 55_path2
Call the Search file function -55_path2
###########lib ID found in item 3############
Call to bring file name function - 3
call the fix path function - 3_path1
Call the Search file function -3_path1            

我希望它是:

###########lib ID found in item 1############
Call to bring file name function - 1
call the fix path function - 1_path1
Call the Search file function -1_path1
call the fix path function - 1_path2
Call the Search file function -1_path2
call the fix path function - 1_path3
Call the Search file function -1_path3
###########lib ID found in item 2############
Call to bring file name function - 2
call the fix path function - 2_path1
Call the Search file function -2_path1
call the fix path function - 2_path2
Call the Search file function -2_path2
call the fix path function - 2_path3
Call the Search file function -2_path3
###########lib ID found in item 3############
Call to bring file name function - 3
call the fix path function - 3_path1
Call the Search file function -3_path1    

2 个答案:

答案 0 :(得分:1)

我建议您更改数据结构,

valid = set([1, 2, 3, 4, 5, 6, 27, 28, 29])

locations = [
    (1, ['path1', 'path2', 'path3']),
    (2, ['path1', 'path2']),
    (55, ['path1', 'path2'])
]

然后您的代码变为

for i,paths in locations:
    if i in valid:
        for path in paths:
            fix_path(path)
            search_file(path)

如果失败,请尝试

valid = ['1', '2', '3', '4', '5', '6', '27', '28', '29']
locationList = ['1', '1_path1','1_path2','1_path3','2', '2_path1','2_path2','2_path3', '55','55_path1','55_path2', '3', '3_path1' ]

for item in locationList:
    item = item.split('_')
    if item[0] in valid:
        if len(item)==1:
            print "###########lib ID found in item %s############" %item
            print "Call to bring file name function - %s" %item
        else:  
            print "call the fix path function - %s" %item
            print "Call the Search file function -%s" %item

答案 1 :(得分:1)

而不是更改您的数据结构(尽管将valid更改为集合是个好主意):

valid = ['1', '2', '3', '4', '5', '6', '27', '28', '29']
locationList = ['1', '1_path1','1_path2','1_path3','2', '2_path1','2_path2','2_path3', '55','55_path1','55_path2', '3', '3_path1' ]

accept = False
for item in locationList:
    if len(item) < 3:
        accept = item in valid
        if accept:
            print "###########lib ID found in item %s############" % item
            print "Call to bring file name function - %s" % item
    elif accept:
        print "call the fix path function - %s" % item
        print "Call the Search file function -%s" % item