2012-07-06 30 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