Verständnissfrage[Englisch]->Deutsch

Alles, was nicht direkt mit Python-Problemen zu tun hat. Dies ist auch der perfekte Platz für Jobangebote.
Antworten
Chris79
User
Beiträge: 15
Registriert: Dienstag 26. Januar 2016, 21:45

Hi,
ich habe eine Aufgabenstellung und verstehe die Aufgabe des Englischen wegens nicht wirklich. :?
Ich möchte auch keinen Code von euch, sondern nur falls mir wer helfen kann eine Erklärung.
Assume you are given two dictionaries d1 and d2, each with integer keys and integer values. You are also given a function f, that takes in two integers, performs an unknown operation on them, and returns a value.

Write a function called dict_interdiff that takes in two dictionaries (d1 and d2). The function will return a tuple of two dictionaries: a dictionary of the intersect of d1 and d2 and a dictionary of the difference of d1 and d2, calculated as follows:

intersect: The keys to the intersect dictionary are keys that are common in both d1 and d2. To get the values of the intersect dictionary, look at the common keys in d1 and d2 and apply the function f to these keys' values -- the value of the common key in d1 is the first parameter to the function and the value of the common key in d2 is the second parameter to the function. Do not implement f inside your dict_interdiff code -- assume it is defined outside.
difference: a key-value pair in the difference dictionary is (a) every key-value pair in d1 whose key appears only in d1 and not in d2 or (b) every key-value pair in d2 whose key appears only in d2 and not in d1.

Here are two examples:

If f(a, b) returns a + b
d1 = {1:30, 2:20, 3:30, 5:80}
d2 = {1:40, 2:50, 3:60, 4:70, 6:90}
then dict_interdiff(d1, d2) returns ({1: 70, 2: 70, 3: 90}, {4: 70, 5: 80, 6: 90})

If f(a, b) returns a > b
d1 = {1:30, 2:20, 3:30}
d2 = {1:40, 2:50, 3:60}
then dict_interdiff(d1, d2) returns ({1: False, 2: False, 3: False}, {})
Den roten Text kann ich nicht greifen.

Ich habe zwei dic´s. Was ist denn aber der common key?

Mein script funktioniert soweit solange ich den Wert von f(a,b) manipuliere. Also entweder vergleichen > (True, False) oder summieren
Da ich allerdings wie schon geschrieben nicht wirklich verstehe auf welchen Wert sich f(a,b) bezieht kann ich das leider nicht einbauen :-(

Vielen Dank im voraus

Chris
BlackJack

@Chris79: „common key“ ist jeder Schlüssel der in beiden Wörterbüchern vorkommt.
Chris79
User
Beiträge: 15
Registriert: Dienstag 26. Januar 2016, 21:45

Hätte ich mal in englisch besser aufgepasst *g*

Ist das also so gemeint?
d1 = {1:30, 2:20, 3:30, 5:80}
d2 = {1:40, 2:50, 3:60, 4:70, 6:90}

Code: Alles auswählen

if f("30,20,30","40,50,60")
oder

Code: Alles auswählen

f(80,150)
als Bedingung z.b.:

Code: Alles auswählen

if f(80,150) == 80+150:
BlackJack

@Chris79: Weder noch. Schau Dir mal an was „intersect“ bei Mengen bedeutet. Und das ``if`` macht in dem Zusammenhang keinen Sinn. `f` ist kein Prädikat, sondern etwas was mit den Werten von eben den gemeinsamen Schlüsseln gemacht werden soll.
Chris79
User
Beiträge: 15
Registriert: Dienstag 26. Januar 2016, 21:45

Danke für den Denkanstoß,

nun habe ich geschnallt was die Aufgabe von mir möchte :-)

Es geht wahrscheinlich auch noch eleganter, aber das hat mich zum Ziel gebracht:

Code: Alles auswählen

def dict_interdiff(d1,d2):
    d3 ={}
    d4 ={}
    for i in d1.keys():
        if i in d2.keys():
            d3[i] = f(d1.pop(i),d2.pop(i))
        else:
            d4[i] = d1.pop(i)
        for i in d2.keys():
            if i not in d1.keys():
                d4[i] = d2.pop(i) 
        res = d3,d4    
        return res
Anfänger1999
User
Beiträge: 3
Registriert: Montag 15. Februar 2016, 20:03

Hey Chris,

Wir sind wohl im selben Kurs :D
Ich hänge da auch irgendwie fest und habe nur noch drei Versuche :oops:
Vielleicht kann mir jemand meinen Denkfehler aufzeigen oder wo mein Fehler liegt oder wie ich meinen Code reparieren kann?
Ich bin aber noch Anfänger, also bitte geduldig sein..

Code: Alles auswählen

# -*- coding: cp1252 -*-
def dict_interdiff(d1,d2):
    intersect = {}
    diff={}
    intersect={ k:v for k,v in d1.iteritems() if ((k in d2)and(d1[k]==d2[k])) }

    
    diff = dict(set(d1.items()) - set(d2.items()))
    tup1=(diff,intersect)
    return tup1

#Also ich weiß, man muss eine Funktion haben, die zwei Dictonaries als Parameter hat.
#Dann muss man die Differenz der beiden in ein Dictonary speichern und erstmal finden.
#Und man muss noch die intersection/Überschneidung finden und das dann auch in ein dictonary packen
#Am Ende soll man die beiden dict in ein tuple zusammenfassen und return tuple

#Aber irgendwas ist wohl falsch an intersect und difference.
Die Aufgabenstellung ist ja schon bekannt;)
Danke im Voraus
BlackJack

@Anfänger1999: Beides ist falsch weil Du in beiden Fällen auch die Werte für die Vergleiche hinzu ziehst. Davon steht nichts in der Aufgabe. Und wo Du `f` verwendest sehe ich jetzt auch gerade nicht‽

Schau Dir mal Mengen und Mengenoperationen an. Die bieten eigentlich schon was man braucht um die Schlüsselmengen für beide Ergebniswörterbücher zu berechnen.
python_learner_600
User
Beiträge: 1
Registriert: Dienstag 16. Februar 2016, 00:40

Hello

We are in the same course and facing the same headache on problem 7.

Just few minutes I got the correct answer submitted and that was my second last chance, that is my 9th submission.

Thank you you rang my bell after reading your code on f(). I missed that the function is applied to the values of the same key.

So for difference, just only find out the difference of 2 dictionaries and combined them together.

For the intersect, you need to find the common keys that both appeared in 2 dictionaries.

I used "set (d1).intersection(d2)" to find the common keys and make up another dict then apply the function such as myDict[key] = f (d1[key], d2[key].

The f function is nothing more than (a + b), (a < b), (a - b) etc. Sorry I can't show you more than above!

I reply to your post because I read your posts and use google to translate to English and in one of them gave me hints that I missed, I really appreciated.

Good luck.
BlackJack

Code: Alles auswählen

from operator import add, gt


def intersect(mapping_a, mapping_b, merge_values):
    keys = set(mapping_a).intersection(mapping_b)
    return dict((k, merge_values(mapping_a[k], mapping_b[k])) for k in keys)


def difference(mapping_a, mapping_b):
    result = dict()
    keys_a = set(mapping_a)
    keys_b = set(mapping_b)
    for mapping, keys in [
        (mapping_a, keys_a - keys_b), (mapping_b, keys_b - keys_a)
    ]:
        result.update((k, mapping[k]) for k in keys)
    return result


def inter_diff(mapping_a, mapping_b, merge_values):
    return (
        intersect(mapping_a, mapping_b, merge_values),
        difference(mapping_a, mapping_b)
    )


def main():
    for mapping_a, mapping_b, merge_values in [
        ({1:30, 2:20, 3:30, 5:80}, {1:40, 2:50, 3:60, 4:70, 6:90}, add),
        ({1:30, 2:20, 3:30}, {1:40, 2:50, 3:60}, gt),
    ]:
        print inter_diff(mapping_a, mapping_b, merge_values)


if __name__ == '__main__':
    main()
DasIch
User
Beiträge: 2718
Registriert: Montag 19. Mai 2008, 04:21
Wohnort: Berlin

Es gibt .viewkeys() in Python 2.7 oder einfach .keys() in Python 3, die Dictionary Views zurückgeben. Man kann darauf direkt Mengenoperationen ausführen.
Anfänger1999
User
Beiträge: 3
Registriert: Montag 15. Februar 2016, 20:03

Hallo,

Eine Frage an Blackjack: Worauf bezieht sich nun dein Code. Ist das die Antwort :?:
Danke im Voraus,
BlackJack

@Anfänger1999: Es ist nicht ganz das was die Aufgabe vorgibt, weil dort die Funktion `f` heisst und auf magische Weise in der Umgebung existiert, was blöd zum Testen ist, weshalb ich die Funktion als Argument übergebe. Unter einem besseren Namen als `f`.
Anfänger1999
User
Beiträge: 3
Registriert: Montag 15. Februar 2016, 20:03

Alos muss ich das definierte main einfach löschen oder durch f ersetzen?
BlackJack

@Anfänger1999: Weder noch. :-)
Antworten