finding elements in python association lists efficiently
Posted
by user248237
on Stack Overflow
See other posts from Stack Overflow
or by user248237
Published on 2010-06-14T19:40:20Z
Indexed on
2010/06/14
19:42 UTC
Read the original article
Hit count: 134
I have a set of lists that look like this:
conditions = [
["condition1", ["sample1", "sample2", "sample3"]],
["condition2", ["sample4", "sample5", "sample6"],
...]
how can I do the following things efficiently and elegantly in Python?
- Find all the elements in a certain condition?
e.g. get all the samples in condition2. Right now I can do:
for cond in conditions:
cond_name, samples = cond
if cond_name == requested_cond:
return samples
but that's clunky.
Find the ordered union of a list of conditions? E.g. ordered_union(["condition1", "condition2"], conditions) should return:
["sample1", "sample2", "sample3", "sample4", "sample5", "sample6"]
How can I do this efficiently in Python? There are probably clever one liners?
© Stack Overflow or respective owner