One of the solutions I read for the problem of finding the latest and the oldest tweet from a list of tweet was to compare each tweet's date with every other tweet's date. I found this very inefficient and wrote a better way to do the same in the below example.
list1 = [{
"tweet":"tweet1", "date":2018
},{
"tweet":"tweet2", "date":2016
},{
"tweet":"tweet3", "date":2017
},{
"tweet":"tweet4", "date":2019
},{
"tweet":"tweet5", "date":2015
},{
"tweet":"tweet6", "date":2014
}]
date1 = 2020
date2 = 2000
firsttweet = ""
lasttweet = ""
for a in list1:
print(date1)
print(date2)
if a["date"]<date1:
date1=a["date"]
firsttweet=a["tweet"]
if a["date"]>date2:
date2=a["date"]
lasttweet=a["tweet"]
print(firsttweet)
print(lasttweet)
Comments
Post a Comment