Sorting images by dates into a list from a dictionary

2024/11/8 0:43:35

I know I asked this before, but I'm still not sure why I just get an empty list when I test this

def sorted_images(image_dict):
'''(dict) -> list of strGiven an image dictionary return a list of the filenames
sorted by date. >>> d = {'image1.jpg': ['UTSC', '2017-11-03','Happy Friday'], \
'image2.jpg': ['UTSC', '2017-11-04', 'Happy Sat.']}
>>> sorted_images(d)    
['image1.jpg', 'image2.jpg']
'''
new_list = []
for filename, (location, date, caption) in image_dict.items():if filename not in image_dict:new_list.append(filename)
return new_list
Answer

Your list is empty because this condition is always false

if filename not in image_dict:

filename is a key in image_dict.

To get the output specified in the docstring, you can do something like this:

def sorted_images(image_dict):'''(dict) -> list of strGiven an image dictionary return a list of the filenamessorted by date. >>> d = {'image1.jpg': ['UTSC', '2017-11-03','Happy Friday'], \'image2.jpg': ['UTSC', '2017-11-04', 'Happy Sat.']}>>> sorted_images(d)    ['image1.jpg', 'image2.jpg']'''return [k for k, v in sorted(image_dict.items(), key=lambda x: x[1][1])] 
https://en.xdnf.cn/q/120676.html

Related Q&A

multiple search and replace in python

I need to search in a parent folder all files that are config.xml and in those files replace one string in another. (from this-is to where-as)

how do I convert the first letter of every word in a list from upper case to lower case? [duplicate]

This question already has answers here:How to downcase the first character of a string?(9 answers)Closed 6 years ago.how do I convert the first letter of each of the below from upper case to lowere ca…

Ubuntu 11.04: Installing PIL into a virtualenv with PIP [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

What does this error mean ? Expected an indented block Python [duplicate]

This question already has answers here:Im getting an IndentationError (or a TabError). How do I fix it?(6 answers)Closed 7 months ago.My code is the following:def value(one,two): if one < two: retu…

Why is len(file.read()) giving me a value of zero?

Why are the values of print len() different for both functions? Are they not the same?The file this script is opening was a text file with three lines of text. i named it test.txt and inside it was J…

Re-formatting user input with spaces

Im using an input function where I want to convert any spaces in the input to +s. So for example, if the user inputs iphone 7 black, I want to convert this to iphone+7+black.

How to set a pdf background color? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 4 years ago.Improve…

Row Average CSV Python

Im looking for a piece of code that will print the average for each users score from a csv.It needs to read all scores and then work out an average across the row for each users.It also needs to calcul…

To input a file and get a formatted string output using .format()

Write a function named calculate_expenses that receives a filename as argument. The file contains the information about a persons expenses on items. Your function should return a list of tuples sorted …

How to use python regex to extract IP address from server log files?

I am currently getting started with python. I have a server log file for the pages I visited over a period of time. How do I write a python program to find out which IP address was visited most? Will …