2012-07-06 34 views
0

我有兩個列表,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: statment後我的項目值爲'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  
+0

爲什麼沒有位置映射到列表?例如。 {'1':['1_path1','1_path2','1_path3']} ...只是一個想法 – 2012-07-06 22:23:47

回答

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 
+0

謝謝大家,我是新來的python,但肯定會檢查設置結構選項。現在提供的解決方案已經夠好了:-) – user1478503 2012-07-06 23:17:57

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