Skip to content Skip to sidebar Skip to footer

How To Group Array Based On The Same Values

Please, confused with array in python. I want to group array based on the same values. Code: enter image description here id_disease = ['penyakit_tepung','hawar_daun'] for id_disea

Solution 1:

It should be

listoflists = []

for row in qres:
    a_list = []

        for r in row:
           data = r.replace('http://www.semanticweb.org/aalviian/ontologies/2017/1/untitled-ontology-10#','')
           a_list.append(data)

    listoflists.append(a_list)

print listoflists

Solution 2:

The general approach I take is to create a dictionary mapping each key to a list.

Say your input list looks like this:

[
 ['a', 'A'], ['a', 'B'], 
 ['b', 'D'], ['b', 'E'],
 ['a', 'C']
]

What I would do is:

map = {}
for item ininput:
    key, value = item  # destructuring assignmentifnot key inmap: map[key] = []  # initialize a spot for the key if not seen beforemap[key].append(value)

Now we should have a map that looks like this:

{'a': ['A', 'B', 'C'], 'b': ['D', 'E']}

Post a Comment for "How To Group Array Based On The Same Values"