finding elements in python association lists efficiently -


I have a set of such lists that look like this:

  Terms = [["Condition1", ["sample1", "sample2", "sample3"]], ["condition2", ["sample4", "sample5", "sample6"], ...]  

How can I do the following things in Python efficiently and beautifully?

  1. Find all the elements in a certain position?

    For example, get all samples in Condition2 I can now:

      In situations: cond_name, samples = cond if cond_name == request_cond: return samples < / Code> 

    but it is clanky.

  2. A list of conditions has been ordered? Such as <"> sample 1", "sample 2", "sample 3", "sample 4" (["condition1", "condition2"], conditions) should be returned:

    , "sample5", "sample6"]  

How can I do this in Python efficiently? Maybe a clever liner?

Ah ok, if you get that clay data structure, you can not expect too much of your first solution The one-liner equivalent will be:

  DIF samples (requested, condition): to come back (if C == is requested, conditions are in case)  

And for the other, if you insist on one-liners, then something like this is happening:

  def command_inian (the_conds, terms): second Problem There are fast ways to lend, but they are all multi-line such as: << code>  

/ P>

  aux_set = set (the_conds) samples_by_cond = dict For c_conds (for c, s) in c, if c is aux_set) for c_conds [c] returns to [samples_by_cond [c]]  

Note that The key to the reasons behind the latter approach is that it uses correct data structures (one set and one word) - unfortunately it needs to be created manually, because incoming terms Ested list is the wrong data structure.

Do you encapsulate conditions as a member variable of a class which once creates the correct (accurate, fast) assistant data structure? Example:

  class intelligent (object): def __init __ (self, terms): for self .seq = [] self.dic = {}, under circumstances: self.seq. Append (C) DACC [C] = S DIF samples themselves (self, requested abortion): returns safe order [order to be requested] DRF order_inian (self, the_conds): SD Dick [C]]  < / Pre> 

Now that is fast and elegant!

I'm assuming that you have a self.seq (a sequence of circumstances) something else (this is definitely not necessary for two operations!), And in that sequence There is no recurrence in the samples (it is not difficult to include those who are your real specs, but when you do not tell anything about them then they are trying to guess indiscriminately Will be very hard and in vain; -)


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -