列表中的Python,变量或列表

时间:2014-09-29 14:37:52

标签: python

试图找出在python中编写它的最佳方法。我理解为什么我的代码不起作用,只是不确定正确的方法来解决它。如果VER有多于一个,则测试将失败b / c它不会分成单个变量。

我希望有一个程序根据标志打印出以下内容:all,open(仅当lvl匹配),closed(仅当lvl匹配),NA(仅当所有lvls都是NA)

这是我到目前为止所做的:

#!/usr/bin/python

import getopt, sys

flago=''  #show open tickets
flagl=''  #show out of lvl tickets
flagc=''  #show closed tickets
flaga=''  #show all
fname=''

options, remainder = getopt.gnu_getopt(sys.argv[1:], 'olca')

for opt, arg in options:
    if opt in ('-o'):
        flago = True
    elif opt in ('-l'):
        flagl = True
    elif opt == '-c':
        flagc = True
    elif opt == '-a':
        flaga = True
#    fname = remainder[0]

#only show tickets if lvl matches regardless of status
reqlvls = ("lvl1", "lvl2", "lvl3" )

#loop through a file with following variables
# number, status(open/closed), lvl#,  comments
file = open ('test.conf')
for line in file:
  fields = line.strip().split(":")
  NUM=fields[0]
  STAT=fields[1]
  VER=fields[2]
  COMM=fields[3]

  #print all
  if flaga:
      print NUM, STAT, VER, COMM
  #show open 
  elif flago:
      # VER is messing up if it is set to VER:[lvl1, lvl3]
      # Need to iterate check open and iterate over VER
      if STAT == "open" and VER in reqlvls:
          print NUM, STAT, VER, COMM
  #show NA
  elif flagl:
      if VER not in reqlvls:
          print NUM, STAT, VER, COMM
  #hide if not reqlvl
  elif flagc:
      if STAT  == "closed" and VER in reqlvls:
          print NUM, STAT, VER, COMM

这是测试文件:$ cat test.conf:

10:open:lvl1:"should show w/ -o"
11:open:lvl5:"should not show w/ -o, NA b/c of lvl"
12:open:lvl3, lvl5:"should w/ -o, req lvl > na lvl"
13:open:lvl1, lvl3:"should w/ -o"
14:open:lvl4, lvl5:"should not w/ -o NA b/c of lvl"
20:closed:lvl2:"should show closed"
21:closed:lvl5:"should not show w/ -c, NA b/c of lvl"
22:closed:lvl3, lvl5:"should w/ -c, req lvl > na lvl"
23:closed:lvl1, lvl3:"should w/ -c"
24:closed:lvl4, lvl5:"should not w/ -c NA b/c of lvl"

这里是输出:$

./test.py -a
10 open lvl1 "should show w/ -o"
11 open lvl5 "should not show w/ -o, NA b/c of lvl"
12 open lvl3, lvl5 "should w/ -o, req lvl > na lvl"
13 open lvl1, lvl3 "should w/ -o"
14 open lvl4, lvl5 "should not w/ -o NA b/c of lvl"
20 closed lvl2 "should show closed"
21 closed lvl5 "should not show w/ -c, NA b/c of lvl"
22 closed lvl3, lvl5 "should w/ -c, req lvl > na lvl"
23 closed lvl1, lvl3 "should w/ -c"
24 closed lvl4, lvl5 "should not w/ -c NA b/c of lvl"

$ ./test.py -o #should show 10,12,13
10 open lvl1 "should show w/ -o"

$ ./test.py -c  #should show 20,22,23
20 closed lvl2 "should show closed"

$ ./test.py -l  #should show 11,14,21,24
11 open lvl5 "should not show w/ -o, NA b/c of lvl"
12 open lvl3, lvl5 "should w/ -o, req lvl > na lvl"
13 open lvl1, lvl3 "should w/ -o"
14 open lvl4, lvl5 "should not w/ -o NA b/c of lvl"
21 closed lvl5 "should not show w/ -c, NA b/c of lvl"
22 closed lvl3, lvl5 "should w/ -c, req lvl > na lvl"
23 closed lvl1, lvl3 "should w/ -c"
24 closed lvl4, lvl5 "should not w/ -c NA b/c of lvl"

我尝试过使用以下列出的函数:

def checkreq(VER):
   for s in  reqlvls:
     for item in VER:
       if item in s:
            print 'LVL: ', s
            return s

然后改变:

  elif flago:
      if STAT == "open" and checkreq(VER):

但这也行不通。

得到了这个功能 Check if list item contains items from another list

1 个答案:

答案 0 :(得分:0)

您可以尝试相反的方法:检查 reqlvls 中的任何级别是否在 VER 中。例如:

>>> VER = "lvl2"
>>> sum([x in VER for x in reqlvls])
1
>>> VER = "lvl2, lvl5"
>>> sum([x in VER for x in reqlvls])
1
>>> VER = "lvl6, lvl7"
>>> sum([x in VER for x in reqlvls])
0

所以你的代码最终会像:

elif flago:
      # VER is messing up if it is set to VER:[lvl1, lvl3]
      # Need to iterate check open and iterate over VER
      if STAT == "open" and sum([x in VER for x in reqlvls]):
          print NUM, STAT, VER, COMM