Selective printing from a list

Say, for example, I have a list like this:

q_list = [('94.vmtest2', 'sgmops', 'Andy Hays', '27/04 00:27:26', 'C', '27/04 00:28:31', 'vmtest1.hep', 'express', '00:00:10', '00:01:04'), 
          ('96.vmtest2', 'sgmops', 'John Dee', '27/04 01:27:26', 'C', '27/04 01:28:33', 'vmtest1.hep', 'short', '00:00:09', '00:01:06'),
          ('99.vmtest2', 'sgmops', 'Andy Hays', '27/04 07:19:56', 'C', '27/04 07:21:12', 'vmtest1.hep', 'express', '00:00:10', '00:01:14'), 
          ('103.vmtest2', 'sgmops', 'John Dee', '27/04 14:08:00', 'C', '27/04 14:09:16', 'vmtest1.hep', 'express', '00:00:10', '00:01:16'),
          ('102.vmtest2', 'sgmops', 'John Dee', '27/04 14:02:38', 'C', '27/04 14:10:12', 'vmtest1.hep', 'short', '00:00:10', '00:01:10')]

which is formed from log files. and then I have a dictionary like this:

q_dict = {'username': 'johndee', 'queue': 'short'}

which is formed from the query string (user input). I want to print [the first 8 elements] the only lines from the list that correspond to the value in the dictionary. In this case, I will print only two lines:

96.vmtest2   sgmops  John Dee  27/04 01:27:26  C  27/04 01:28:33  vmtest1.hep  short
102.vmtest2  sgmops  John Dee  27/04 14:02:38  C  27/04 14:10:12  vmtest1.hep  short

This is actually not a dictionary at all; user input (command line argument) is as follows:

'formatDate(%m/%d) == 4/27 && username == John Dee && queue == short'

and I create a dictionary from that. Any idea how I can do this? Thank you in advance for any help. Hooray!!

+3
source share
4 answers

, , :

[x for x in q_list if x[2] == 'John Dee']

filter:

filter(lambda x: x[2] == 'John Dee', q_list)

, q_dict / .

[x for x in q_list if [x[2].lower().replace(' ', ''), x[7]] == q_dict.values()]

Space_C0wb0y, namedtuple , :

fields =  ['field1', 'field2', 'username', 'field4', 'field5', 'field6', 'field7', 'queue', 'field9', 'field10']
Item = namedtuple('Item', fields)

, namedtuples:

q_namedtuple = [Item(*x) for x in q_list]

. , , :

[item for item in q_namedtuple if all(getattr(item, k) == v for k, v in q_dict.iteritems())]

, q_dict ... , . dicts, :

q_list_of_dicts = [dict(zip(fields, x)) for x in q_list]

:

{'field1': '102.vmtest2',
 ...etc
 'queue': 'short',
 'username': 'John Dee'}

:

[item for item in q_list_of_dicts if all(item.get(k) == v for k, v in q_dict.iteritems())]

, . , ( ):

results = []
for item in q_list:
    d = dict(zip(fields, item))
    # use some other logic to filter
    if all(d.get(k) == v for k, v in q_dict.iteritems()):
         results.append(d)
+1

-, , namedtuple , . (, operator.attrgetter). - dict.

( dict s):

[item for item in q_list if all((item[attr] == q_dict[attr]) 
                                 for q_dict in filters)]

, , , , ( ||, no != ).

, , .

, , , , :

for item in filtered_items:
    print "\t".join(item[attr] for attr in attributes_to_print) + "\n"
+2

formatDate , eval:

test_string = "username == 'John Dee' && queue == 'short'"

for a, b, username, formatDate, d, e, f, queue, g, h in q_list:
    if eval(test_string.replace('&&', 'and')):
        print '%s: %s' % (username, queue)

"", "||"

( queue == short, queue == 'short')

q_list:

John Dee: short
John Dee: short
+1

after your user input has been analyzed for filter conditions, check each entry for each condition by storing the index set in q_list that matches each individual condition, then run the intersection set to find indexes that match all conditions

in pseudo code:

n = len(q_list)
accepted = set(range(n))
for condition in conditions:
  accepted = accepted.intersection({i for i in range(n) if condition_holds(q_list(i), condition)})

result = [q_list[i] for i in accepted]
0
source

All Articles