0

I have 2 lists:

year_month_day_list = ['2017-02-12', '2017-02-11', '2017-02-10', '2017-02-09']

filename_prefix_list = ['2017-02-12_11-45-59', '2017-02-12_11-35-05', '2017-02-10_11-00-52', '2017-02-11_10-59-23', '2017-02-09_09-12-09']

If I can grab all of the filenames that match their associated ymd:

def filter_files(file_list, filter_param):
    return [f for f in file_list
            if f.startswith(filter_param)]

for unique_day in year_month_day_list:
        files_for_day = sorted(filter_files(
            filename_prefix_list, unique_day), reverse=True)

How can I create a dictionary that uses the unique_day as the key and the associated filenames for that day as the values?

Output should be something like:

dict = {'2017-02-12': ['2017-02-12_11-45-59', '2017-02-12_11-35-05'], '2017-02-10': ['2017-02-10_11-00-52']}

Then, how can I use those in my template? Something like:

<div class="thumbnail-row" id="thumbnail_row">
    <div class="row-date">
        <span>{{ unique_day }}</span>
    </div>
    <div class="">
        <form action="{{ filename_prefix }}/video/" method="post">
            {% csrf_token %}
            <input type="image" name="filename_prefix" value="{{ filename_prefix }}" src="{{ MEDIA_URL}}thumbnails/2017/02/{{ filename_prefix }}.jpg">
        </form>
    </div>
</div>
brewcrazy
  • 623
  • 13
  • 30

2 Answers2

2

You can use itertools.groupby to group the list items by their dates. The date can be sliced from the datetime strings and used as the grouping key. You can drop sorted if the datetime strings are already clustered by dates:

from itertools import groupby
from pprint import pprint

filename_prefix_list = ['2017-02-12_11-45-59', '2017-02-12_11-35-05', '2017-02-10_11-00-52', '2017-02-11_10-59-23', '2017-02-09_09-12-09']

dct = {k: list(g) for k, g in groupby(sorted(filename_prefix_list), lambda x: x[:10])}
pprint(dct)
# {'2017-02-09': ['2017-02-09_09-12-09'],
#  '2017-02-10': ['2017-02-10_11-00-52'],
#  '2017-02-11': ['2017-02-11_10-59-23'],
#  '2017-02-12': ['2017-02-12_11-35-05', '2017-02-12_11-45-59']}

To use the dictionary in your template simply pass it to the current context via your view function, and get its key-value pairs by calling its items method using {% for k, v in dct.items %}.

More generally:

{% for k, v in dct.items %}
     {% for dt in v %}
     <!- your html -->
     {% endfor %}
{% endfor %}
Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
  • It looks like this is working great. However, I want the results returned in descending order for both the key and list of values. Basically I want the most recent day, then its files with the most recent file first. Can you suggest how I can accomplish that? Sort() on dct removes the values. – brewcrazy Feb 12 '17 at 21:28
  • Pass `reverse=True` to `sorted` and save the result into `collections.OrderedDict` – Moses Koledoye Feb 12 '17 at 21:29
  • Which result am I saving into `collections.OrderedDict`? This is what I have now: `dct = {k: list(g) for k, g in groupby( sorted(sorted_filename_list, reverse=True), lambda x: x[:10])}` – brewcrazy Feb 12 '17 at 21:42
  • This: `dct = OrderedDict((k, list(g)) for k, g in groupby(sorted(sorted_filename_list, reverse=True), lambda x: x[:10]))` – Moses Koledoye Feb 12 '17 at 21:59
  • Perfect. Thank you!! – brewcrazy Feb 13 '17 at 02:52
0

For converting the lists into the desired format dict, you may use collections.defaultdict as:

from collections import defaultdict

year_month_day_list = ['2017-02-12', '2017-02-11', '2017-02-10', '2017-02-09']
filename_prefix_list = ['2017-02-12_11-45-59', '2017-02-12_11-35-05', '2017-02-10_11-00-52', '2017-02-11_10-59-23', '2017-02-09_09-12-09']

my_dict = defaultdict(list)
for year_month_day in year_month_day_list:
    for filename_prefix in filename_prefix_list:
        if filename_prefix.startswith(year_month_day):
            my_dict[year_month_day].append(filename_prefix)

where value hold by my_dict will be:

{
    '2017-02-09': ['2017-02-09_09-12-09'], 
    '2017-02-10': ['2017-02-10_11-00-52'], 
    '2017-02-12': ['2017-02-12_11-45-59', '2017-02-12_11-35-05'],        
    '2017-02-11': ['2017-02-11_10-59-23']
}

For accessing dict objects in Django's template, you will have to do something like:

{% for key, values in my_dict.items %} 
    {{key}}
    {% for value in values %}
        {{value}}
    {% endfor %} 
{% endfor %}

For more details, refer: How to access dictionary element in django template?


Note: Do not use dict as variable name since dict is an in-built type in Python.

Community
  • 1
  • 1
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126