I often use PHP to convert records retrieved from the database as follows:
<?php
$list = array(
array("GROUP"=>"A", "NAME"=>"Apple", "COUNT"=>1),
array("GROUP"=>"A", "NAME"=>"Gorilla", "COUNT"=>2),
array("GROUP"=>"B", "NAME"=> "Rapper", "COUNT"=>3),
);
$convert_list = array_reduce($list, function($result,$row){
if(!key_exists($row["GROUP", $result)){
$result[$row["GROUP"]]=array();
}
$result[$row["GROUP"]][]=$row;
return$result;
}, array());
var_dump($convert_list);
The results are as follows:
array(2){
["A"] = >
array(2){
[0]=>
array(3){
["GROUP"] =>
US>//#string(1) "A"
["NAME"] =>
US>//#string(9) "Apple"
["COUNT"] =>
int(1)
}
[1]=>
array(3){
["GROUP"] =>
US>//#string(1) "A"
["NAME"] =>
US>//#string(9) "Gorilla"
["COUNT"] =>
int(2)
}
}
["B"] = >
array(1){
[0]=>
array(3){
["GROUP"] =>
US>//#string(1) "B"
["NAME"] =>
US>//#string(9) "Rapper"
["COUNT"] =>
int(3)
}
}
}
Would it be possible to use Python's reduce()
(or otherwise)?
I will also show you how to use redundancy.
#!/usr/bin/env python
# -*-coding:utf-8-*-
import sys
if sys.version_info[0]>=3:
from functools import reduction
from print import print aspp
lst=[{"GROUP":"A", "NAME":u"Apple", "COUNT":1} ,
{"GROUP":"A", "NAME":u "Gorilla", "COUNT":2},
{"GROUP": "B", "NAME":u "Rapper", "COUNT":3},]
default func(result, row):
if not row ["GROUP"] in result:
result [row["GROUP"]] = [ ]
result [row["GROUP"]].append(row)
return result
pp(reduce(func,lst,{}))
Results:
{'A':[{'COUNT':1,'GROUP':'A', 'NAME':'Apple',
{'COUNT':2, 'GROUP': 'A', 'NAME': 'Gorilla'},
US>'B': [{'COUNT':3,'GROUP':'B','NAME':'Rapper'}]}
Python cannot write multiple lines of unknown functions, so it defines a function (func) once.
(It seems that it can be written in encapsulation without defining a function, but it will be less readable.)
The flow of processing within the function is the same as the code you asked.
In addition, we first check the version of Python and import (if it is python3 or higher).This is because in Python 3.0 and above, redundancy is moving to the functools module instead of the built-in function.The result is Python 3.4, but the above code can still be executed in 2.7
.
It's not redundancy, but I think it's better to use defaultdict.
#!/usr/bin/python
# -*-coding:utf-8-*-
from print import print aspp
from collections import defaultdict
lst=[{"GROUP":"A", "NAME":u"Apple", "COUNT":1} ,
{ "GROUP": "A", "NAME":u "Gorilla", "COUNT":2} ,
{ "GROUP": "B", "NAME":u "Rapper", "COUNT":3}]
convert_list=defaultdict(list)
for row install:
convert_list [row["GROUP"]].append(row)
pp(dict(convert_list))
Results:
{'A':[{'COUNT':1,'GROUP':'A','NAME':u'\u308a\u3093\u3054'},
{'COUNT':2,'GROUP':'A', 'NAME':u'\u30b4\u30ea\u30e9'},
'B': [{'COUNT':3, 'GROUP':'B', 'NAME':u'\u30e9\u30c3\u30d1'}]}
Using cytoolz:
from cytoolz import groupby
lst=[{"GROUP":"A", "NAME":u"Apple", "COUNT":1} ,
{"GROUP":"A", "NAME":u "Gorilla", "COUNT":2},
{"GROUP": "B", "NAME":u "Rapper", "COUNT":3},]
groupby("GROUP", lst)
The output is:
{'A':[{'COUNT':1,'GROUP':'A','NAME':u'\u308a\u3093\u3054'},
{'COUNT':2,'GROUP':'A', 'NAME':u'\u30b4\u30ea\u30e9'},
'B': [{'COUNT':3, 'GROUP':'B', 'NAME':u'\u30e9\u30c3\u30d1'}]}
© 2024 OneMinuteCode. All rights reserved.