1

I have 3 dictionaries data['d1'], data['d2'] and data['d3'].

What i want is the table to look like this, getting the keys from each dictionary and presenting them like:

Team 1        |   Team 2       |   Team 3
key 1 from d1 |  key 1 from d2 |  key 1 from d3
key 2 from d1 |  key 2 from d2 |  key 2 from d3
key 3 from d1 |  key 3 from d2 |  key 3 from d3
key 4 from d1 |  key 4 from d2 |  key 4 from d3

What i have tryed

<thead>
    <tr>
        <th>Team 1</th>
        <th>Team 2</th>
        <th>Team 3</th>
    </tr>
</thead>
<tbody>
    <tr>
    {% for key, value in d1.items %}
        <td>{{ key }}</td>
    {% endfor %}
    </tr>
    <tr>
    {% for key, value in d2.items %}
        <td>{{ key }}</td>
    {% endfor %}
    </tr>
    <tr>
    {% for key, value in d3.items %}
        <td>{{ key }}</td>
    {% endfor %}
    </tr>
</tbody>

or

<thead>
    <tr>
        <th>Team 1</th>
        <th>Team 2</th>
        <th>Team 3</th>
    </tr>
</thead>
<tbody>
    {% for key, value in d1.items %}
    <tr>
        <td>{{ key }}</td>
    </tr>
    {% endfor %}
    {% for key, value in d2.items %}
    <tr>
        <td>{{ key }}</td>
    </tr>
    {% endfor %}
    {% for key, value in d3.items %}
    <tr>
        <td>{{ key }}</td>
    </tr>
    {% endfor %}
</tbody>

but it's showing them differently. How can i achieve the above table?

Current table is displayed like this:

Stalone Sobont  arda    AAA
Sardina Jhonny  Lala    Vali
LilJon  Scuipy  SSS     Mazare

views.py

stats = data['scoruri'].copy()
    data['d1'] = {}
    data['d2'] = {}
    data['d3'] = {}

for key, value in stats.copy().items():
    if not data['d1'] or sum(data['d1'].values()) <= sum(data['d2'].values()) and \
                         sum(data['d1'].values()) <= sum(data['d3'].values()):
        name, score = max(stats.items(), key=lambda p: p[1])
        data['d1'][name] = score
        stats.pop(name, score)
        data['total_1'] = sum(data['d1'].values())
    elif not data['d2'] or sum(data['d2'].values()) <= sum(data['d1'].values()) and \
                           sum(data['d2'].values()) <= sum(data['d3'].values()):
        name, score = max(stats.items(), key=lambda p: p[1])
        data['d2'][name] = score
        stats.pop(name, score)
        data['total_2'] = sum(data['d2'].values())
    elif not data['d3'] or sum(data['d3'].values()) <= sum(data['d1'].values()) and \
                           sum(data['d3'].values()) <= sum(data['d2'].values()):
        name, score = max(stats.items(), key=lambda p: p[1])
        data['d3'][name] = score
        stats.pop(name, score)
        data['total_3'] = sum(data['d3'].values())
alex
  • 2,381
  • 4
  • 23
  • 49

2 Answers2

0

You want all keys of a dict to be printed in a column whereas you are printing all keys of a dict in a row

You could combine the dicts into a list (in the view) and then iterate over this list in the template.

In the view:

data_dicts = [d1,d2,d3] 

And in the template:

<tr>
{% for d in data_dicts %}
    {% for key, value in d.items %}
        <td> {{ key }} </td>
    {% endfor %}
{% endfor %}
</tr>

Also, this long answer may also help.

Community
  • 1
  • 1
Anupam
  • 14,950
  • 19
  • 67
  • 94
  • did you try zipping the dicts? – Anupam Apr 24 '17 at 13:03
  • uhm.. no i haven't – alex Apr 24 '17 at 13:03
  • Zip is not applicable for dicts. – Abijith Mg Apr 24 '17 at 13:07
  • @AbijithMg dict.items returns a list, so that can help? Something like: `for (k1, v1), (k2, v2), (k3,v3) in zip(d1.items(), d2.items(), d3.items()): {{k1}}{{k2}}.......` (something like that I think) – Anupam Apr 24 '17 at 13:10
  • zip is not a valid builtin template tag! So the processing has to be done in the view only. – Abijith Mg Apr 24 '17 at 13:19
  • 1
    @AbhijitMg Ah yes, you are right. @NitaAlexandru another solution is to combine the dicts in a list `d = [d1,d2,d3]` in the view and then iterate over d in the template. I saw a good detailed example [here](http://stackoverflow.com/a/16404875/1526703). Updating the answer – Anupam Apr 24 '17 at 13:32
  • i tryed it, but it's not giving me the keys from each dictionary – alex Apr 24 '17 at 13:54
  • @NitaAlexandru Updated the answer now to print the key instead of value – Anupam Apr 24 '17 at 14:00
  • @NitaAlexandru Sorry, as it stands right now, my solution is wrong, it will still not do what you want. I may come back later to think about it, but dont follow the solution above – Anupam Apr 24 '17 at 14:12
  • hehe, i've noticed, what's now in the answer does what i've tryed before :) – alex Apr 24 '17 at 14:15
  • the list with dictionaries isn't a bad ideea, but how do you get the keys – alex Apr 24 '17 at 14:17
  • Also, if you need the keys in a particular order, need to use an ordered dict, see its not ordered by default – Anupam Apr 24 '17 at 14:23
  • no, i just want the keys of each dictionary on a column – alex Apr 24 '17 at 14:32
  • @NitaAlexandru If you dont need the dict values at all, how about just sending a list of keys from the view down to the template? `dict.keys()` – Anupam Apr 24 '17 at 14:55
0

So, this is how you can achieve that table:

from collections import OrderedDict


d1 = {1: 'Alex', 2: 'Andrei'}
d2 = {1: 'Cristi', 2: 'Ion'}
d3 = {1: 'Sandu', 2: 'Marica'}

d = dict()

d1 = OrderedDict(sorted(d1.items()))
d2 = OrderedDict(sorted(d2.items()))
d3 = OrderedDict(sorted(d3.items()))

d1 = list(d1.items())
d2 = list(d2.items())
d3 = list(d3.items())

d['team_1'] = d1
d['team_2'] = d2
d['team_3'] = d3

d = OrderedDict(sorted(d.items()))


{% for i in d.items %}
    <tr>
        <td>{{ i.1.0.1 }}</td>
    </tr>
{% endfor %}
alex
  • 2,381
  • 4
  • 23
  • 49