Stefan Ram wrote:

> Michael Torrie <torr...@gmail.com> writes:
>>On 09/15/2017 12:04 PM, Stefan Ram wrote:
>>>writes some complex queries to the table, what can be expected
>>^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>>How do you plan to code these queries?
> 
>   I did a quick prototype. I am aware that the code
>   is very quick and very dirty. But since you ask:
> 
>   The table is filled after »# fill table« below.
> 
>   An example ad-hoc query then follows after
>   »# query table« below.
> 
>   This code was not intended to be read by someone else,
>   the next thing to do is a clean up of the code.

"import builtins", lines ending on ';', both extra and missing spaces, come 
on ;) 

Do yourself a favour, and steer your style a bit more into the direction of 
PEP 8.

>   So you have been warned!
> 
>   The output should be:
> 
> |abend
> | - Beispiel  =  Montag abend
> |Abend
> | - Beispiel  =  des Abends
> |ähnlich
> | - Beispiel  =  dem Hause ähnlich

Based on the example I wonder if you really want a table, or whether 
something tree-like would be more appropriate:

from collections import OrderedDict, defaultdict

data = {
    "abend": {
        "Beispiel": {"Montag abend"},
        "Kategorie": {"Deutsches Wörterbuch", "Groß- und Kleinschreibung"}
    },
    "Abend": {
        "Beispiel": {"des Abends"},
        "Kategorie": {"Deutsches Wörterbuch",
                      "Getrennt -und Zusammenschreibung"}
    },
    "Montag abend": {
        "Kategorie":  {"Getrennt -und Zusammenschreibung"}
    },
    "ähnlich": {
        "Kategorie": {"Deutsches Wörterbuch"},
        "Beispiel": {"dem Hause ähnlich"}
    },
}

data = OrderedDict(sorted(
    ((k, defaultdict(set, v)) for k, v in data.items()),
    key=lambda kv: (kv[0].casefold(), kv[0])))

for key, value in data.items():
    if "Deutsches Wörterbuch" in value["Kategorie"]:
        print(key)
        for example in value["Beispiel"]:
            print(" -", example)


If this turns out to be too slow just throw more lookup tables at it:

by_relation = defaultdict(list)
for key, value in data.items():
    for k, v in value.items():
        for vv in v:
            by_relation[k, vv].append(key)

Then you can rewrite the print loops to avoid iterating over the whole 
table:

for key in by_relation["Kategorie", "Deutsches Wörterbuch"]:
    print(key)
    for example in data[key]["Beispiel"]:
        print(" -", example)


-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to