and so on...what i need to do is some list comprehension that returns me
something like
result = [
{
'service_id' : 1, 'values': [ {'value': 10},
{'value': 15}]
},
{
'service_id' : 2, 'values': [ {'value': 5}, {'value': 15}]
}
My problem now is i cant avoid have "repeteated" entries, lets say, in
this particular case, 2 entries for "service_id = 1", and other 2 for
"service_id =2".
Ill keeping blew off my hair and drinking more cofee while searching for
this damn onliner im looking for.
Thanks dudes.
Gerardo
Is three lines ok?
>>> output=dict()
>>> for record in result:
output.setdefault(record['service_id'], list()).append(record
['value'])
>>> output
{1: [10, 15], 2: [5, 15]}
>>>
Creating the more verbose output that you specified should be
pretty straighforward from there.
max
Okay...while I'm not sure the opacity of a one-liner is actually
productive, it *can* be done. Whether it should, I leave that to
your discernment. :)
>>> [{'service_id': i, 'values':[{'value':d2['value']} for d2 in
result if d2['service_id'] == i ]} for i in set(d['service_id']
for d in result)]
[{'service_id': 1, 'values': [{'value': 10}, {'value': 15}]},
{'service_id': 2, 'values': [{'value': 5}, {'value': 15}]}]
There's no claiming it's efficient, as it looks like there may be
some O(N^2) logic going on under the hood (or possibly O(N*M)
where N is the size of the result-set and M is the count of
unique service_id values), as it's iterating over the result-set
in two dimensions...once to create the set of top-level indices,
and once for each result.
If you didn't have to have all those dictionaries around, it
might come out more cleanly to just have some result-set that
came out to be
{1: [10,15], 2: [5,15]}
Just a few thoughts...
-tkc
If you import itertools and have your DB query return in order of
service_id (or sort the list after retrieving result), then...
[ [dict(service_id=key),[dict(value=n['value']) for n in value]] for
key,value in itertools.groupby(result,lambda x: x['service_id']) ]
... is more or less what you want.
Jon.
Gerardo
>> result = [{'service_id' : 1, 'value': 10},
>> {'service_id': 2, 'value': 5},
>> {'service_id': 1, 'value': 15},
>> {'service_id': 2, 'value': 15},
>> ]
>>
>> and so on...what i need to do is some list comprehension that returns
>> me something like
>>
>> result = [
>> {
>> 'service_id' : 1, 'values': [ {'value': 10},
>> {'value': 15}]
>> },
>> {
>> 'service_id' : 2, 'values': [ {'value': 5}, {'value': 15}]
>> }
>>
>> My problem now is i cant avoid have "repeteated" entries, lets say,
>> in this particular case, 2 entries for "service_id = 1", and other 2
>> for "service_id =2".
>
>
I'm confused, can't you write a multi-line function and pass that?
> Thanks a lot dudes, i hope someday ill turn myself into some of you guys
> who can actually answer questions ;)
if you want to become a good Python programmer, you really need to get
over that "I need a oneliner" idea.
</F>
You don't _need_ a list comprehension, you just _want_ one :)
[...]
> Ill keeping blew off my hair and drinking more cofee while searching for
> this damn onliner im looking for.
I know, trying to put complex logic in one line makes you do all that.
Go for the multiliner!
--
Roberto Bonvallet
+1 QOTW
-Carsten