user1478503
user1478503

Reputation: 11

iterate over a list with some conditions

I have two lists, valid and locations. valid contains IDs represented by string numbers, and location contains id + strings (path) belonging to the id they follow.

My goal is to check if my id is part of the valid group. If TRUE for the valid ID and following items I will call some functions. When an INAVLID ID is detected I should skip it and move item to the next ID.

My code goes like this:

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

My probelm is that after the else: statment my item value is '55' == INVALID. At this point I wish to move item forward in the list to the place where value is the next ID (in this case '3').

my current output is:

###########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            

I wish it to be :

###########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    

Upvotes: 0

Views: 134

Answers (2)

Hugh Bothwell
Hugh Bothwell

Reputation: 56654

I'd suggest changing your data structure,

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

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

then your code becomes

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

Failing that, try

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

Upvotes: 1

MRAB
MRAB

Reputation: 20654

Instead of changing your data structures (although changing valid to a set would be a good idea):

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

Upvotes: 1

Related Questions