2 Stimmen

Python: Iterieren über zwei Wörterbücher, die Listen als Werte haben

Ich habe zwei Wörterbücher erstellt, die Histogrammwerte von Bildern enthalten. Jedes Wörterbuch hat den Dateinamen der Bilddatei als Schlüssel und die Liste der drei eindimensionalen Vektoren, die zusammengefügt wurden, als seine Werte.

{'someFileName.jpg' : ['forecolor=2,3,5,5,6','edge=2,4,5','texture=5,4,3']}

Hier ist eine aktuelle Darstellung eines meiner Wörterbücher:

Wörterbuch1

{'/Users/images/Transcend-8GB-Class-10-SDHC-Flash-Memory-Card.jpg': ['fcolor=2,4,14,5,0,0,0,0,0,0,0,0,0,0,12,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,6,0,0,0,0,1,0,0,0,0,0,0,0,0,0,20,9,0,0,0,2,2,0,0,0,0,0,0,0,0,0,13,6,0,0,0,1,0,0,0,0,0,0,0,0,0,0,10,8,0,0,0,1,2,0,0,0,0,0,0,0,0,0,17,17,0,0,0,3,6,0,0,0,0,0,0,0,0,0,7,5,0,0,0,2,0,0,0,0,0,0,0,0,0,0,4,3,0,0,0,1,1,0,0,0,0,0,0,0,0,0,6,6,0,0,0,2,3', 'edge=1,252,1,32,124,194,63,252,67,15,240,1,7,244,66,47,0,192,63', 'texture=1,78,27,37,13,6,6,7,78']}

Wörterbuch2

{'/Users/images/kodax-camera-M531.jpg': ['fcolor=2,74,6,20,30,1,2,0,1,0,0,0,1,3,2,0,0,0,0,0,1,1,1,0,0,2,0,0,0,2,2,0,0,0,0,0,2,2,1,0,0,5,0,0,0,1,4,0,0,0,0,0,2,2,1,0,0,1,0,0,0,3,1,0,0,0,0,0,1,1,0,0,0,3,0,0,0,1,2,0,0,0,0,0,2,2,1,0,0,4,0,0,0,0,5,0,0,0,0,0,2,1,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0', 'edge=1,4,1,88,128,22,8,39,25,142,230,226,31,60,64,255,252,12,76', 'texture=1,15,32,31,28,19,16,12,98']}

Mein Endziel ist es, zwei dieser Wörterbücher an eine Methode zu übergeben und tatsächlich cosign Wert ausführen,

Beispiel: So jedes Wörterbuch hat Liste als sein Wert, so für jedes Wörterbuch Schlüssel ich tun möchten Vektor-Multiplikation zwischen dictionary1's key1, velu1 mit dictionary2 key1, value1,

Ich habe die Vektor-Multiplikation-Funktion, so was ich versuche, herauszufinden, wie man richtig iterieren, ich dachte entlang der Linie der Verwendung einer Yield-Funktion, aber es hat nicht wirklich funktionieren, wenn ich versucht. Dies ist, was ich so weit haben:

def cosignSimilarity(image1VectorDict, image2VectorDict):
    for image1Key, image2Value in image1VectorDict.iteritems():
        print image1Key
        for aValue in image1Value:
            print aValue
            for image2Key, image2Value in image2VectorDict.iteritems():
                for eValue in image2Value:
                    print aValue
                    print "\n"
                    print eValue

Zu Ihrer Information: Ich bitte nicht um Hilfe bei der Berechnung der Mitunterschrift.

Dies ist, wie mein aktueller Code die Daten ausspuckt, wenn ich Schlüssel zu Schlüssel von einem Wörterbuch zu einem anderen isolieren kann, dann kann ich den Rest wie die Berechnung von Kosinuswerten tun.

   First  Dictionary
    {'/Users/test/Transcend-8GB-Class-10-SDHC-Flash-Memory-Card.jpg': ['fcolor=2,4,14,5,0,0,0,0,0,0,0,0,0,0,12,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,6,0,0,0,0,1,0,0,0,0,0,0,0,0,0,20,9,0,0,0,2,2,0,0,0,0,0,0,0,0,0,13,6,0,0,0,1,0,0,0,0,0,0,0,0,0,0,10,8,0,0,0,1,2,0,0,0,0,0,0,0,0,0,17,17,0,0,0,3,6,0,0,0,0,0,0,0,0,0,7,5,0,0,0,2,0,0,0,0,0,0,0,0,0,0,4,3,0,0,0,1,1,0,0,0,0,0,0,0,0,0,6,6,0,0,0,2,3', 'edge=1,252,1,32,124,194,63,252,67,15,240,1,7,244,66,47,0,192,63', 'texture=1,78,27,37,13,6,6,7,78']}
    ------------------
Second Dictionary
    {'/Users/test/kodax-camera-M531.jpg': ['fcolor=2,74,6,20,30,1,2,0,1,0,0,0,1,3,2,0,0,0,0,0,1,1,1,0,0,2,0,0,0,2,2,0,0,0,0,0,2,2,1,0,0,5,0,0,0,1,4,0,0,0,0,0,2,2,1,0,0,1,0,0,0,3,1,0,0,0,0,0,1,1,0,0,0,3,0,0,0,1,2,0,0,0,0,0,2,2,1,0,0,4,0,0,0,0,5,0,0,0,0,0,2,1,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0', 'edge=1,4,1,88,128,22,8,39,25,142,230,226,31,60,64,255,252,12,76', 'texture=1,15,32,31,28,19,16,12,98']}
    ++++++++++++++++++
    /Users/test/Transcend-8GB-Class-10-SDHC-Flash-Memory-Card.jpg
    fcolor=2,4,14,5,0,0,0,0,0,0,0,0,0,0,12,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,6,0,0,0,0,1,0,0,0,0,0,0,0,0,0,20,9,0,0,0,2,2,0,0,0,0,0,0,0,0,0,13,6,0,0,0,1,0,0,0,0,0,0,0,0,0,0,10,8,0,0,0,1,2,0,0,0,0,0,0,0,0,0,17,17,0,0,0,3,6,0,0,0,0,0,0,0,0,0,7,5,0,0,0,2,0,0,0,0,0,0,0,0,0,0,4,3,0,0,0,1,1,0,0,0,0,0,0,0,0,0,6,6,0,0,0,2,3
    fcolor=2,4,14,5,0,0,0,0,0,0,0,0,0,0,12,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,6,0,0,0,0,1,0,0,0,0,0,0,0,0,0,20,9,0,0,0,2,2,0,0,0,0,0,0,0,0,0,13,6,0,0,0,1,0,0,0,0,0,0,0,0,0,0,10,8,0,0,0,1,2,0,0,0,0,0,0,0,0,0,17,17,0,0,0,3,6,0,0,0,0,0,0,0,0,0,7,5,0,0,0,2,0,0,0,0,0,0,0,0,0,0,4,3,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252,1,32,124,194,63,252,67,15,240,1,7,244,66,47,0,192,63
    edge=1,252,1,32,124,194,63,252,67,15,240,1,7,244,66,47,0,192,63

    fcolor=2,74,6,20,30,1,2,0,1,0,0,0,1,3,2,0,0,0,0,0,1,1,1,0,0,2,0,0,0,2,2,0,0,0,0,0,2,2,1,0,0,5,0,0,0,1,4,0,0,0,0,0,2,2,1,0,0,1,0,0,0,3,1,0,0,0,0,0,1,1,0,0,0,3,0,0,0,1,2,0,0,0,0,0,2,2,1,0,0,4,0,0,0,0,5,0,0,0,0,0,2,1,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0
    edge=1,252,1,32,124,194,63,252,67,15,240,1,7,244,66,47,0,192,63

    edge=1,4,1,88,128,22,8,39,25,142,230,226,31,60,64,255,252,12,76
    edge=1,252,1,32,124,194,63,252,67,15,240,1,7,244,66,47,0,192,63

    texture=1,15,32,31,28,19,16,12,98
    texture=1,78,27,37,13,6,6,7,78
    texture=1,78,27,37,13,6,6,7,78

    fcolor=2,74,6,20,30,1,2,0,1,0,0,0,1,3,2,0,0,0,0,0,1,1,1,0,0,2,0,0,0,2,2,0,0,0,0,0,2,2,1,0,0,5,0,0,0,1,4,0,0,0,0,0,2,2,1,0,0,1,0,0,0,3,1,0,0,0,0,0,1,1,0,0,0,3,0,0,0,1,2,0,0,0,0,0,2,2,1,0,0,4,0,0,0,0,5,0,0,0,0,0,2,1,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0
    texture=1,78,27,37,13,6,6,7,78

    edge=1,4,1,88,128,22,8,39,25,142,230,226,31,60,64,255,252,12,76
    texture=1,78,27,37,13,6,6,7,78

    texture=1,15,32,31,28,19,16,12,98

Wie Sie sehen können, spucke ich viel zu viele Wiederholungen desselben Wertes aus.

Dies sind die aktuellen Wörterbücher, mit denen ich zu tun habe:

Wörterbuch 1:

{'/Users/test/Transcend-8GB-Class-10-SDHC-Flash-Memory-Card.jpg': ['fcolor=2,4,14,5,0,0,0,0,0,0,0,0,0,0,12,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,6,0,0,0,0,1,0,0,0,0,0,0,0,0,0,20,9,0,0,0,2,2,0,0,0,0,0,0,0,0,0,13,6,0,0,0,1,0,0,0,0,0,0,0,0,0,0,10,8,0,0,0,1,2,0,0,0,0,0,0,0,0,0,17,17,0,0,0,3,6,0,0,0,0,0,0,0,0,0,7,5,0,0,0,2,0,0,0,0,0,0,0,0,0,0,4,3,0,0,0,1,1,0,0,0,0,0,0,0,0,0,6,6,0,0,0,2,3', 'edge=1,252,1,32,124,194,63,252,67,15,240,1,7,244,66,47,0,192,63', 'texture=1,78,27,37,13,6,6,7,78']}

Wörterbuch 2:

{'/Users/test/kodax-camera-M531.jpg': ['fcolor=2,74,6,20,30,1,2,0,1,0,0,0,1,3,2,0,0,0,0,0,1,1,1,0,0,2,0,0,0,2,2,0,0,0,0,0,2,2,1,0,0,5,0,0,0,1,4,0,0,0,0,0,2,2,1,0,0,1,0,0,0,3,1,0,0,0,0,0,1,1,0,0,0,3,0,0,0,1,2,0,0,0,0,0,2,2,1,0,0,4,0,0,0,0,5,0,0,0,0,0,2,1,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0', 'edge=1,4,1,88,128,22,8,39,25,142,230,226,31,60,64,255,252,12,76', 'texture=1,15,32,31,28,19,16,12,98']}

Ich habe die Funktion Lamba

cosinLamba = lambda a, b : round(NP.inner(a, b)/(LA.norm(a)*LA.norm(b)), 3)

Ich möchte über Wörterbuch 1 und Wörterbuch 2 iterieren und den fcolor-Wert von Wörterbuch 1 erhalten 'fcolor=2,4,14,5,0,0,0,0,0,0,0,0,0,0,12,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,6,0,0,0,0,1,0,0,0,0,0,0,0,0,0,20,9,0,0,0,2,2,0,0,0,0,0,0,0,0,0,13,6,0,0,0,1,0,0,0,0,0,0,0,0,0,0,10,8,0,0,0,1,2,0,0,0,0,0,0,0,0,0,17,17,0,0,0,3,6,0,0,0,0,0,0,0,0,0,7,5,0,0,0,2,0,0,0,0,0,0,0,0,0,0,4,3,0,0,0,1,1,0,0,0,0,0,0,0,0,0,6,6,0,0,0,2,3'

und fcolor-Wert von dictionary2

'fcolor=2,74,6,20,30,1,2,0,1,0,0,0,1,3,2,0,0,0,0,0,1,1,1,0,0,2,0,0,0,2,2,0,0,0,0,0,2,2,1,0,0,5,0,0,0,1,4,0,0,0,0,0,2,2,1,0,0,1,0,0,0,3,1,0,0,0,0,0,1,1,0,0,0,3,0,0,0,1,2,0,0,0,0,0,2,2,1,0,0,4,0,0,0,0,5,0,0,0,0,0,2,1,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0'

Schicken Sie sie an meine Lamba-Funktion cosinLamba(valu1, value2) Wert1 und Wert 2 sind Zeichenketten und deshalb habe ich sie in meinem Wörterbuch als Werte gespeichert. Und ich möchte für fcolor, texture, edge alle Vektoren, die ich für ein bestimmtes Bild in jedem Wörterbuch gespeichert habe, verwenden.

2voto

jfs Punkte 370717

Könnten Sie damit beginnen, Ihre Darstellung zu ändern:

{'someFileName.jpg' : {'forecolor': [2,3,5,5,6],'edge': [2,4,5],'texture':[5,4,3]}}

Oder

{('someFileName.jpg', 'forecolor'): [2,3,5,5,6],
 ('someFileName.jpg', 'edge'): [2,4,5],
 ('someFileName.jpg', 'texture'):[5,4,3]}

Zum Beispiel, um entsprechende Listen für den 1:

from itertools import product

# pair info for each image with info of every image from another dictionary
for (fn1, d1), (fn2,d2) in product(dict1.iteritems(), dict2.iteritems()):
    for property_, list_value in d1.iteritems():
        compute_cosine_similarity(list_value, d2[property_])

Wenn Sie Ihre Darstellung mit einer Liste von Zeichenfolgen verwenden, sieht es so aus:

from itertools import product

# pair info for each image with info of every image from another dictionary
for (fn1,lst1), (fn2,lst2) in product(dict1.iteritems(), dict2.iteritems()):
    # assume all lists has the same order of elements
    for string_value1, string_value2 in zip(lst1, lst2):
        compute(string_value1, string_value2)

Sie sollten Zahlen nicht als Liste von Ascii-Strings speichern. Wenn Sie Speicherplatz sparen müssen, können Sie Numpy-Arrays verwenden. cosinLamba nimmt sie bereits an.

from collections import namedtuple
import numpy as np

Info = namedtuple('Info', 'forecolor edge texture')

dict1 = {'someFileName.jpg': Info(np.array([...], dtype=np.uint8),
                                  np.array([...], dtype=np.uint8),
                                  np.array([...], dtype=np.uint8))}

Der Code zum Aufruf von cosine_similarity() ist genau derselbe wie bei Ihrer Darstellung.

0voto

inspectorG4dget Punkte 103745

Ich bin nicht vertraut mit der Mitzeichnungsberechnung, die Sie durchführen möchten, aber abgesehen davon, wenn ich Ihre Frage richtig verstehe, sollte der folgende Code funktionieren:

for key1, vals1 in dict1.iteritems():
    vals2 = dict2[key1]
    for val1, val2 in zip(vals1, vals2):
        # you now have the corresponding values for each image file
        compute_cosign(val1, val2)

CodeJaeger.com

CodeJaeger ist eine Gemeinschaft für Programmierer, die täglich Hilfe erhalten..
Wir haben viele Inhalte, und Sie können auch Ihre eigenen Fragen stellen oder die Fragen anderer Leute lösen.

Powered by:

X