Sort dictionary after class attributes and today's dateHow do I sort a list of dictionaries by a value of the dictionary?How to sort a list of objects based on an attribute of the objects?How do I sort a dictionary by value?Sort a list by multiple attributes?In python. How do I have a user change a dictionary value, when that dictionary is in a class?How can I sort a dictionary by key?Getting today's date in YYYY-MM-DD in Python?Python trainticket machineHow to detect EOF when input is not from a file?Adding multiple events to a PDF report
GPLv3 forces us to make code available, but to who?
Why is there such a singular place for bird watching?
Did the Soviet army intentionally send troops (e.g. penal battalions) running over minefields?
What is the point of impeaching Trump?
Generating numbers with cubes
Is there a pattern for handling conflicting function parameters?
Is "Ram married his daughter" ambiguous?
What powers or limits devil promotion?
Using RECURSIVE in Virtual Layer
Why not add cuspidal curves in the moduli space of stable curves?
Search for something difficult to count/estimate
What makes a character irredeemable?
Does Bank Manager's discretion still exist in Mortgage Lending
Disable all sound permanently
Canteen Cutlery Issue
How can I find places to store/land a private airplane?
Does it require less energy to reach the Sun from Pluto's orbit than from Earth's orbit?
How to protect bash function from being overridden?
What is the difference between increasing volume and increasing gain?
Could Boris Johnson face criminal charges for illegally proroguing Parliament?
How is this situation not a checkmate?
Why do popular TCP-using services have UDP as well as TCP entries in /etc/services?
Knights and Knaves: What does C say?
As a team leader is it appropriate to bring in fundraiser candy?
Sort dictionary after class attributes and today's date
How do I sort a list of dictionaries by a value of the dictionary?How to sort a list of objects based on an attribute of the objects?How do I sort a dictionary by value?Sort a list by multiple attributes?In python. How do I have a user change a dictionary value, when that dictionary is in a class?How can I sort a dictionary by key?Getting today's date in YYYY-MM-DD in Python?Python trainticket machineHow to detect EOF when input is not from a file?Adding multiple events to a PDF report
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;
I have a class, Reminder, and dictionary, Events, and would like to sort the dictionary based on today's date.
The program should ask the user for an int input of today's date, and then print all keys in the dictionary that are relevant for this day and further.
Eg. it should not print events that have already passed.
class Reminder:
self.name = name
self.date = date
self.reminder = reminder
event1 = Reminder('trip to vegas', 20130312, 'buy a suitcase')
event2 = Reminder('to do next month', 20190401, 'get a job')
event3 = Reminder('apply for college', 20190603, 'send in application'
event_dict =
event_dict[event1.name] = event1
event_dict[event2.name] = event2
event_dict[event3.name] = event3
todays date = int(input("Input today's date!"))
So now when the user puts in for example the date 20190328, the program should not print event1 since it has already passed.
And as said I want event 2 and 3 to be sorted before printed, so that the next upcoming date is printed first.
It might seem unnecessary to sort 2 events but in the whole code it will be more events.
python python-3.x
add a comment
|
I have a class, Reminder, and dictionary, Events, and would like to sort the dictionary based on today's date.
The program should ask the user for an int input of today's date, and then print all keys in the dictionary that are relevant for this day and further.
Eg. it should not print events that have already passed.
class Reminder:
self.name = name
self.date = date
self.reminder = reminder
event1 = Reminder('trip to vegas', 20130312, 'buy a suitcase')
event2 = Reminder('to do next month', 20190401, 'get a job')
event3 = Reminder('apply for college', 20190603, 'send in application'
event_dict =
event_dict[event1.name] = event1
event_dict[event2.name] = event2
event_dict[event3.name] = event3
todays date = int(input("Input today's date!"))
So now when the user puts in for example the date 20190328, the program should not print event1 since it has already passed.
And as said I want event 2 and 3 to be sorted before printed, so that the next upcoming date is printed first.
It might seem unnecessary to sort 2 events but in the whole code it will be more events.
python python-3.x
add a comment
|
I have a class, Reminder, and dictionary, Events, and would like to sort the dictionary based on today's date.
The program should ask the user for an int input of today's date, and then print all keys in the dictionary that are relevant for this day and further.
Eg. it should not print events that have already passed.
class Reminder:
self.name = name
self.date = date
self.reminder = reminder
event1 = Reminder('trip to vegas', 20130312, 'buy a suitcase')
event2 = Reminder('to do next month', 20190401, 'get a job')
event3 = Reminder('apply for college', 20190603, 'send in application'
event_dict =
event_dict[event1.name] = event1
event_dict[event2.name] = event2
event_dict[event3.name] = event3
todays date = int(input("Input today's date!"))
So now when the user puts in for example the date 20190328, the program should not print event1 since it has already passed.
And as said I want event 2 and 3 to be sorted before printed, so that the next upcoming date is printed first.
It might seem unnecessary to sort 2 events but in the whole code it will be more events.
python python-3.x
I have a class, Reminder, and dictionary, Events, and would like to sort the dictionary based on today's date.
The program should ask the user for an int input of today's date, and then print all keys in the dictionary that are relevant for this day and further.
Eg. it should not print events that have already passed.
class Reminder:
self.name = name
self.date = date
self.reminder = reminder
event1 = Reminder('trip to vegas', 20130312, 'buy a suitcase')
event2 = Reminder('to do next month', 20190401, 'get a job')
event3 = Reminder('apply for college', 20190603, 'send in application'
event_dict =
event_dict[event1.name] = event1
event_dict[event2.name] = event2
event_dict[event3.name] = event3
todays date = int(input("Input today's date!"))
So now when the user puts in for example the date 20190328, the program should not print event1 since it has already passed.
And as said I want event 2 and 3 to be sorted before printed, so that the next upcoming date is printed first.
It might seem unnecessary to sort 2 events but in the whole code it will be more events.
python python-3.x
python python-3.x
edited Mar 28 at 20:50
Jonas Graffman
asked Mar 28 at 20:44
Jonas GraffmanJonas Graffman
525 bronze badges
525 bronze badges
add a comment
|
add a comment
|
2 Answers
2
active
oldest
votes
Simple implementation:
class Reminder:
def __init__(self, name, date, reminder):
self.name = name
self.date = date
self.reminder = reminder
def __str__(self):
return "Name: '' - Date: '' - Reminder: ''".format(self.name,
self.date,
self.reminder)
event1 = Reminder('trip to vegas', 20130312, 'buy a suitcase')
event2 = Reminder('to do next month', 20190401, 'get a job')
event3 = Reminder('apply for college', 20190603, 'send in application')
event_dict =
event_dict[event1.name] = event1
event_dict[event2.name] = event2
event_dict[event3.name] = event3
todays_date = 20180101
# implementation
events_sorted = sorted(event_dict.items(), key=lambda t: t[1].date)
events = [e[1] for e in events_sorted if e[1].date >= todays_date]
for e in events:
print(e)
Output:
Name: 'to do next month' - Date: '20190401' - Reminder: 'get a job'
Name: 'apply for college' - Date: '20190603' - Reminder: 'send in application'
You are amazing!! Thank you so much
– Jonas Graffman
Mar 28 at 21:50
add a comment
|
class Reminder(object):
def __init__(self, name, date, reminder):
self.name = name
self.date = date
self.reminder = reminder
event1 = Reminder('trip to vegas', 20130312, 'buy a suitcase')
event2 = Reminder('to do next month', 20190401, 'get a job')
event3 = Reminder('apply for college', 20190603, 'send in application')
event_dict =
event_dict[event1.date] = event1
event_dict[event2.date] = event2
event_dict[event3.date] = event3
todays_date = int(input("Input today's date!"))
import operator
#filter passed dates
event_dict = k: v for k, v in event_dict.items() if k > todays_date
#sort dictionary
sorted_events = sorted(event_dict.items(), key=operator.itemgetter(0))
#print results
for item in sorted_events:
print("trip name: " , item[1].name, "| trip date: ", item[1].date, "| trip reminder: ", item[1].reminder,' n')
Output:
trip name: to do next month | trip date: 20190401 | trip reminder: get a job
trip name: apply for college | trip date: 20190603 | trip reminder: send in application
add a comment
|
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55406566%2fsort-dictionary-after-class-attributes-and-todays-date%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Simple implementation:
class Reminder:
def __init__(self, name, date, reminder):
self.name = name
self.date = date
self.reminder = reminder
def __str__(self):
return "Name: '' - Date: '' - Reminder: ''".format(self.name,
self.date,
self.reminder)
event1 = Reminder('trip to vegas', 20130312, 'buy a suitcase')
event2 = Reminder('to do next month', 20190401, 'get a job')
event3 = Reminder('apply for college', 20190603, 'send in application')
event_dict =
event_dict[event1.name] = event1
event_dict[event2.name] = event2
event_dict[event3.name] = event3
todays_date = 20180101
# implementation
events_sorted = sorted(event_dict.items(), key=lambda t: t[1].date)
events = [e[1] for e in events_sorted if e[1].date >= todays_date]
for e in events:
print(e)
Output:
Name: 'to do next month' - Date: '20190401' - Reminder: 'get a job'
Name: 'apply for college' - Date: '20190603' - Reminder: 'send in application'
You are amazing!! Thank you so much
– Jonas Graffman
Mar 28 at 21:50
add a comment
|
Simple implementation:
class Reminder:
def __init__(self, name, date, reminder):
self.name = name
self.date = date
self.reminder = reminder
def __str__(self):
return "Name: '' - Date: '' - Reminder: ''".format(self.name,
self.date,
self.reminder)
event1 = Reminder('trip to vegas', 20130312, 'buy a suitcase')
event2 = Reminder('to do next month', 20190401, 'get a job')
event3 = Reminder('apply for college', 20190603, 'send in application')
event_dict =
event_dict[event1.name] = event1
event_dict[event2.name] = event2
event_dict[event3.name] = event3
todays_date = 20180101
# implementation
events_sorted = sorted(event_dict.items(), key=lambda t: t[1].date)
events = [e[1] for e in events_sorted if e[1].date >= todays_date]
for e in events:
print(e)
Output:
Name: 'to do next month' - Date: '20190401' - Reminder: 'get a job'
Name: 'apply for college' - Date: '20190603' - Reminder: 'send in application'
You are amazing!! Thank you so much
– Jonas Graffman
Mar 28 at 21:50
add a comment
|
Simple implementation:
class Reminder:
def __init__(self, name, date, reminder):
self.name = name
self.date = date
self.reminder = reminder
def __str__(self):
return "Name: '' - Date: '' - Reminder: ''".format(self.name,
self.date,
self.reminder)
event1 = Reminder('trip to vegas', 20130312, 'buy a suitcase')
event2 = Reminder('to do next month', 20190401, 'get a job')
event3 = Reminder('apply for college', 20190603, 'send in application')
event_dict =
event_dict[event1.name] = event1
event_dict[event2.name] = event2
event_dict[event3.name] = event3
todays_date = 20180101
# implementation
events_sorted = sorted(event_dict.items(), key=lambda t: t[1].date)
events = [e[1] for e in events_sorted if e[1].date >= todays_date]
for e in events:
print(e)
Output:
Name: 'to do next month' - Date: '20190401' - Reminder: 'get a job'
Name: 'apply for college' - Date: '20190603' - Reminder: 'send in application'
Simple implementation:
class Reminder:
def __init__(self, name, date, reminder):
self.name = name
self.date = date
self.reminder = reminder
def __str__(self):
return "Name: '' - Date: '' - Reminder: ''".format(self.name,
self.date,
self.reminder)
event1 = Reminder('trip to vegas', 20130312, 'buy a suitcase')
event2 = Reminder('to do next month', 20190401, 'get a job')
event3 = Reminder('apply for college', 20190603, 'send in application')
event_dict =
event_dict[event1.name] = event1
event_dict[event2.name] = event2
event_dict[event3.name] = event3
todays_date = 20180101
# implementation
events_sorted = sorted(event_dict.items(), key=lambda t: t[1].date)
events = [e[1] for e in events_sorted if e[1].date >= todays_date]
for e in events:
print(e)
Output:
Name: 'to do next month' - Date: '20190401' - Reminder: 'get a job'
Name: 'apply for college' - Date: '20190603' - Reminder: 'send in application'
edited Mar 28 at 21:15
answered Mar 28 at 21:08
dzangdzang
6093 silver badges10 bronze badges
6093 silver badges10 bronze badges
You are amazing!! Thank you so much
– Jonas Graffman
Mar 28 at 21:50
add a comment
|
You are amazing!! Thank you so much
– Jonas Graffman
Mar 28 at 21:50
You are amazing!! Thank you so much
– Jonas Graffman
Mar 28 at 21:50
You are amazing!! Thank you so much
– Jonas Graffman
Mar 28 at 21:50
add a comment
|
class Reminder(object):
def __init__(self, name, date, reminder):
self.name = name
self.date = date
self.reminder = reminder
event1 = Reminder('trip to vegas', 20130312, 'buy a suitcase')
event2 = Reminder('to do next month', 20190401, 'get a job')
event3 = Reminder('apply for college', 20190603, 'send in application')
event_dict =
event_dict[event1.date] = event1
event_dict[event2.date] = event2
event_dict[event3.date] = event3
todays_date = int(input("Input today's date!"))
import operator
#filter passed dates
event_dict = k: v for k, v in event_dict.items() if k > todays_date
#sort dictionary
sorted_events = sorted(event_dict.items(), key=operator.itemgetter(0))
#print results
for item in sorted_events:
print("trip name: " , item[1].name, "| trip date: ", item[1].date, "| trip reminder: ", item[1].reminder,' n')
Output:
trip name: to do next month | trip date: 20190401 | trip reminder: get a job
trip name: apply for college | trip date: 20190603 | trip reminder: send in application
add a comment
|
class Reminder(object):
def __init__(self, name, date, reminder):
self.name = name
self.date = date
self.reminder = reminder
event1 = Reminder('trip to vegas', 20130312, 'buy a suitcase')
event2 = Reminder('to do next month', 20190401, 'get a job')
event3 = Reminder('apply for college', 20190603, 'send in application')
event_dict =
event_dict[event1.date] = event1
event_dict[event2.date] = event2
event_dict[event3.date] = event3
todays_date = int(input("Input today's date!"))
import operator
#filter passed dates
event_dict = k: v for k, v in event_dict.items() if k > todays_date
#sort dictionary
sorted_events = sorted(event_dict.items(), key=operator.itemgetter(0))
#print results
for item in sorted_events:
print("trip name: " , item[1].name, "| trip date: ", item[1].date, "| trip reminder: ", item[1].reminder,' n')
Output:
trip name: to do next month | trip date: 20190401 | trip reminder: get a job
trip name: apply for college | trip date: 20190603 | trip reminder: send in application
add a comment
|
class Reminder(object):
def __init__(self, name, date, reminder):
self.name = name
self.date = date
self.reminder = reminder
event1 = Reminder('trip to vegas', 20130312, 'buy a suitcase')
event2 = Reminder('to do next month', 20190401, 'get a job')
event3 = Reminder('apply for college', 20190603, 'send in application')
event_dict =
event_dict[event1.date] = event1
event_dict[event2.date] = event2
event_dict[event3.date] = event3
todays_date = int(input("Input today's date!"))
import operator
#filter passed dates
event_dict = k: v for k, v in event_dict.items() if k > todays_date
#sort dictionary
sorted_events = sorted(event_dict.items(), key=operator.itemgetter(0))
#print results
for item in sorted_events:
print("trip name: " , item[1].name, "| trip date: ", item[1].date, "| trip reminder: ", item[1].reminder,' n')
Output:
trip name: to do next month | trip date: 20190401 | trip reminder: get a job
trip name: apply for college | trip date: 20190603 | trip reminder: send in application
class Reminder(object):
def __init__(self, name, date, reminder):
self.name = name
self.date = date
self.reminder = reminder
event1 = Reminder('trip to vegas', 20130312, 'buy a suitcase')
event2 = Reminder('to do next month', 20190401, 'get a job')
event3 = Reminder('apply for college', 20190603, 'send in application')
event_dict =
event_dict[event1.date] = event1
event_dict[event2.date] = event2
event_dict[event3.date] = event3
todays_date = int(input("Input today's date!"))
import operator
#filter passed dates
event_dict = k: v for k, v in event_dict.items() if k > todays_date
#sort dictionary
sorted_events = sorted(event_dict.items(), key=operator.itemgetter(0))
#print results
for item in sorted_events:
print("trip name: " , item[1].name, "| trip date: ", item[1].date, "| trip reminder: ", item[1].reminder,' n')
Output:
trip name: to do next month | trip date: 20190401 | trip reminder: get a job
trip name: apply for college | trip date: 20190603 | trip reminder: send in application
answered Mar 28 at 21:13
Maria NazariMaria Nazari
3301 silver badge14 bronze badges
3301 silver badge14 bronze badges
add a comment
|
add a comment
|
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55406566%2fsort-dictionary-after-class-attributes-and-todays-date%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown