【问题标题】:Python, variable or list in listPython,变量或列表中的列表
【发布时间】:2014-09-29 14:37:51
【问题描述】:

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

我想让一个程序根据标志打印出以下内容:全部、打开(仅当 lvl 匹配时)、关闭(仅当 lvl 匹配时)、NA(仅当所有 lvl 都是 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

【问题讨论】:

  • 这里的代码和输出太多了。将其简化为一个简单的示例。
  • 只是想确保我得到了所有可能需要的东西

标签: python


【解决方案1】:

您可以尝试相反的方法:检查 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

【讨论】:

  • 谢谢这很好用。所以这只是将 reqlvls 中的所有 VER 相加,0 为假,1+ 为真
猜你喜欢
  • 2017-08-10
  • 2016-10-11
  • 1970-01-01
  • 2010-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-15
  • 2013-09-04
相关资源
最近更新 更多