0

我有两个列表,valid并且locations. valid包含由字符串编号表示的 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'== 无效。此时我希望将列表中的项目向前移动到 value 是下一个 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    
4

2 回答 2

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
于 2012-07-06T22:29:23.750 回答
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
于 2012-07-06T22:39:30.440 回答