Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5848701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:53:46+00:00 2026-05-22T12:53:46+00:00

Say for an example, I’ve a list like this: q_list = [(’94.vmtest2′, ‘sgmops’, ‘Andy

  • 0

Say for an example, I’ve 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 the log files. and then I have a dictionary like this:

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

which is formed from a query string (the user input). What I want is to print [first 8 items of] the only lines from the list match with the value in the dictionary. In this case, I’ll only print these 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

In fact it doesn’t have to be a dictionary at all; the user input (command-line argument) is like this:

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

and I’m creating the dictionary out of that. Any idea how can I do that? Thanks in advance for any help. Cheers!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T12:53:47+00:00Added an answer on May 22, 2026 at 12:53 pm

    The most common approach is to use a list comprehension, for instance to filter by username:

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

    You can also use filter:

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

    You can thus use your q_dict to filter by user and/or queue easily.

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

    As suggested in Space_C0wb0y’s answer, using namedtuple would be a good approach, you can map out the fields rather easily:

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

    For the sake of the argument, let’s convert the tuples in your list to namedtuples:

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

    And then you can filter dynamically based on the query params. For instance to get a list of items where all query params match the given fields:

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

    That is assuming the username in q_dict actually matched the field for username…it doesn’t in your example but you can work around that. Similarly you could create a list of dicts, that’d make working with your list a bit easier:

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

    Which would give you a list of dicts like:

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

    Then you can filter similarly:

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

    Personally I think I prefer the dict approach. Whole things rolled up using for loops instead (and one generator expression):

    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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a webpage (say www.example.com/a.asp) .. I am having an iframe in this
I have a PHP script on a domain (say example.com) and this domain has
Say for example I have this table: Items: ------- id title price And I
Hey, I'm not sure if this is possible but anyway. Say for example: <div
Say for example you're getting a web app project that interacts with a database.
Say for example I have the following string: var testString = Hello, world; And
An instance of class A instantiates a couple of other objects, say for example
I want to add items in a LaTeX-document. Say for example, that I want
How would I run both of them under one main website, say www.example.com, which
If I was to send a URL to a DNS server, lets say: dev.example.com/?username=daniel,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.