Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
283 views
in Technique[技术] by (71.8m points)

python - 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_disease in id_disease:
    qres = acacia.query( 
        """
        PREFIX tst: <http://www.semanticweb.org/aalviian/ontologies/2017/1/untitled-ontology-10#>
        SELECT ?disease ?patogen
        WHERE { 
            ?disease tst:caused_by ?patogen . 
            FILTER regex(str(?disease), "%s") .
        } """ % id_disease )

    for row in qres:
        for r in row:
            print(r.replace('http://www.semanticweb.org/aalviian/ontologies/2017/1/untitled-ontology-10#',''))
        print("
")

Output:

penyakit_tepung
spaerotheca_sp

penyakit_tepung
oidium_sp


penyakit_tepung
erysiphe_sp


hawar_daun
cylindrocladium_sp


hawar_daun
kirramyces_sp


hawar_daun
phaeophleopspora_sp

Expected Array :

[['spaeerotheca_sp','oidium_sp','erysiphe_sp'].['cylindrocladium_sp','kirramyces_sp','phaeophleopspora_sp']]

Please, help me if you know how step to get it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...