Re: How to sort over dictionaries
On Wednesday, August 29, 2018 at 11:20:26 AM UTC+5:30, John Ladasky wrote:
> The top-level object you are showing is a list [], not a dictionary {}. It
> has dictionaries inside of it though. Do you want to sort the list?
>
> Python's sorted() function returns a sorted copy of a sequence. Sorted() has
> an optional argument called "key". Key accepts a second function which can
> be used to rank each element in the event that you don't want to compare them
> directly.
>
> The datetime module has functions which can convert the time strings you are
> showing into objects which are ordered by time and are suitable as keys for
> sorting. Look at datetime.datetime.strptime(). It takes two arguments, the
> date/time string, and a second string describing the format of the first
> string. There are many ways to format date and time information as strings
> and none are standard. This function call seems to work for your data:
>
> >>> datetime.strptime("04-08-2018 19:12", "%d-%m-%Y %H:%M")
> datetime.datetime(2018, 8, 4, 19, 12)
>
> Hope that gets you started.
i have tried but it was showing error like this
TypeError: '<' not supported between instances of 'operator.itemgetter' and
'operator.itemgetter'
Thanks John
--
https://mail.python.org/mailman/listinfo/python-list
How to sort over dictionaries
[, https://getbootstrap.com/', 'author': 'Hari', 'date': '15-08-2018 15:15', 'headline': 'latest news'}>, , https://www.deps.co/blog/google-cloud-platform-good-bad-ugly/', 'author': 'Harish', 'headline': 'Google Cloud Platform – The Good, Bad, and Ugly', 'date': '16-08-2018 08:15'}>, , , http://www.dailymail.co.uk/tvshowbiz/harry_styles/index.html', 'author': 'Harry', 'headline': 'Reunion', 'date': '17-08-2018 20:40', 'votes': 3}>, https://www.telegraph.co.uk/news/2017/07/15/us-vogue-apologises-missing-mark-zayn-malik-gigi-hadid-gender/', 'author': 'zayn', 'date': '17-08-2018 15:39', 'headline': 'Memories'}>] Can someone help me out how to sort this dictionary data based on time, how to arrange the same dictionary in descending order based on time. Thanks -- https://mail.python.org/mailman/listinfo/python-list
Re: How to sort over dictionaries
>
> > On Wednesday, August 29, 2018 at 11:20:26 AM UTC+5:30, John Ladasky wrote:
> >> The top-level object you are showing is a list [], not a dictionary {}.
> >> It has dictionaries inside of it though. Do you want to sort the list?
> >>
> >> Python's sorted() function returns a sorted copy of a sequence. Sorted()
> >> has an optional argument called "key". Key accepts a second function
> >> which can be used to rank each element in the event that you don't want
> >> to compare them directly.
> >>
> >> The datetime module has functions which can convert the time strings you
> >> are showing into objects which are ordered by time and are suitable as
> >> keys for sorting. Look at datetime.datetime.strptime(). It takes two
> >> arguments, the date/time string, and a second string describing the
> >> format of the first string. There are many ways to format date and time
> >> information as strings and none are standard. This function call seems
> >> to work for your data:
> >>
> >> >>> datetime.strptime("04-08-2018 19:12", "%d-%m-%Y %H:%M")
> >> datetime.datetime(2018, 8, 4, 19, 12)
> >>
> >> Hope that gets you started.
> >
> >
> >
> >
> > i have tried but it was showing error like this
> > TypeError: '<' not supported between instances of 'operator.itemgetter'
> > and 'operator.itemgetter'
>
> Please remember to always provide the code you tried. That makes it easier
> to point out the error.
>
> That said, here's a typical example of sorted and operator.itemgetter:
>
> >>> import operator
> >>> data = [dict(foo=1, bar="second"), dict(foo=2, bar="first")]
> >>> sorted(data, key=operator.itemgetter("foo"))
> [{'bar': 'second', 'foo': 1}, {'bar': 'first', 'foo': 2}]
> >>> sorted(data, key=operator.itemgetter("bar"))
> [{'bar': 'first', 'foo': 2}, {'bar': 'second', 'foo': 1}]
sort = sorted(results, key=lambda res:itemgetter('date'))
print(sort)
I have tried the above code peter but it was showing error like
TypeError: '<' not supported between instances of 'operator.itemgetter' and
'operator.itemgetter'
Thanks
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to sort over dictionaries
> > sort = sorted(results, key=lambda res:itemgetter('date'))
> > print(sort)
> >
> >
> > I have tried the above code peter but it was showing error like
> > TypeError: '<' not supported between instances of 'operator.itemgetter'
> > and 'operator.itemgetter'
>
> lambda res: itemgetter('date')
>
> is short for
>
> def keyfunc(res):
> return itemgetter('date')
>
> i. e. it indeed returns the itemgetter instance. But you want to return
> res["date"]. For that you can either use a custom function or the
> itemgetter, but not both.
>
> (1) With regular function:
>
> def keyfunc(res):
> return res["date"]
> sorted_results = sorted(results, key=keyfunc)
>
> (1a) With lambda:
>
> keyfunc = lambda res: res["date"]
> sorted_results = sorted(results, key=keyfunc)
>
> (2) With itemgetter:
>
> keyfunc = itemgetter("date")
> sorted_results = sorted(results, key=keyfunc)
>
> Variants 1a and 2 can also be written as one-liners.
Thanks PeterNo 2 has worked.
--
https://mail.python.org/mailman/listinfo/python-list
Check file is
Hi Friends Is there any utility in python which will help me to read any pdf files? Regards Harish -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem when applying Patch from issue1424152 to get https over authenticating proxies working with urllib2 in Python 2.5
On Monday, July 20, 2009 11:28:53 PM UTC+5:30, tvashtar wrote:
> On Jul 20, 4:42 pm, Nike wrote:
> > hi!
> > It's looks like a ssl error . Under the following step to help u :
> > 1. takes a simple code to confirm your pupose without ssl protocol.
> > 2. to confirm python version and extended libs work well
> > 3. to confirm ssl work well.
> >
> > goog luck!
> >
> > nikekoo
>
> I've reduced my code to the following:
>
> import urllib2
>
> p = "https://user:pass@myproxy:port";
> proxy_handler = urllib2.ProxyHandler({"https": p})
> urllib2.install_opener(urllib2.build_opener(proxy_handler))
> request = urllib2.Request( "https://groups.google.com";)
> response = urllib2.urlopen(request)
>
> and it is now failing with:
>
> Traceback (most recent call last):
> File "D:\p4\depot\Development\HEAD\Build\ReleaseSystem\DownloadSystem
> \test.py", line 12, in
> response = urllib2.urlopen(request)
> File "C:\Python25\lib\urllib2.py", line 121, in urlopen
> return _opener.open(url, data)
> File "C:\Python25\lib\urllib2.py", line 379, in open
> response = self._open(req, data)
> File "C:\Python25\lib\urllib2.py", line 397, in _open
> '_open', req)
> File "C:\Python25\lib\urllib2.py", line 358, in _call_chain
> result = func(*args)
> File "C:\Python25\lib\urllib2.py", line 1115, in https_open
> return self.do_open(httplib.HTTPSConnection, req)
> File "C:\Python25\lib\urllib2.py", line 1082, in do_open
> raise URLError(err)
> urllib2.URLError: Authentication Required>
>
> I thought the proxy_handler should take care of the authentication?
>
> Thanks for your help
Is this issue fixed. I am also facing the same issue of tunneling in https
request. Please suggest how to proceed further
--
https://mail.python.org/mailman/listinfo/python-list
Detecting if a library has native dependency
Hello Is there a reliable way to detect if a python library has native dependency or native code? For ex. can I programmatically determine that "lxml" package has native code and depends on the presence of libxml on the system? Regards, Harish -- https://mail.python.org/mailman/listinfo/python-list
how to fix python logging to not log to stderr
I am doing this logging.basiConfig(logleve=Logging.Info) then i create a file log handler and attach to it. i also have propagate as True. My logs are going to the stderr as well. How do i fix so that logs don't go to stdout? -- https://mail.python.org/mailman/listinfo/python-list
List insert at index that is well out of range - behaves like append that too SILENTLY
Hi , Let me demonstrate the problem I encountered : I had a list a = [1, 2, 3] when I did a.insert(100, 100) [1, 2, 3, 100] as list was originally of size 4 and I was trying to insert value at index 100 , it behaved like append instead of throwing any errors as I was trying to insert in an index that did not even existed . Should it not throw IndexError: list assignment index out of range exception as it throws when I attempt doing a[100] = 100 Personal Opinion : Lets see how other languages behave in such a situation : 1. Ruby : > a = [1, 2] > a[100] = 100 > a => [1, 2, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, 100] The way ruby handles this is pretty clear and sounds meaningful (and this is how I expected to behave and it behaved as per my expectation) at least to me . Here also it was silently handled but the way it fills non existing indexes in between with nil sounded meaningful . 2. Java : When I do such an action in java by using .add(index.value) on may be arraylist or linkedlist I get java.lang.IndexOutOfBoundException Here instead of handling it silently it throws an error . But the python way of handling such a problem by appending to the end sounds more unexpected to me . This in fact flummoxed me in the beginning making me think it could be a bug . Then when I raised it in stackoverflow I got chance to look at source and found that's the way code is written . Question : 1. Any idea Why it has been designed to silently handle this instead of at least informing the user with an exception(as in java) or attaching null values in empty places (as in ruby) ? Thanks Harish -- https://mail.python.org/mailman/listinfo/python-list
How to get back a list object from its string representation?
Hello, Consider : >>> li = [1,2,3] >>> repr(li) '[1, 2, 3]' Is there a standard way to get back li, from repr(li) ? Regards, Harish -- http://mail.python.org/mailman/listinfo/python-list
What is ''r'' in python?
Hello, I accidentally did this in the shell. >>> ''r'' '' >>> ''r'' == '' True >>> ''r'' == "" True That is . However if I try -> >>> ''c'' File "", line 1 ''c'' ^ SyntaxError: invalid syntax >>> ''z'' File "", line 1 ''z'' ^ SyntaxError: invalid syntax Any other character that way is Invalid Syntax. What is so special about character r enclose within a pair of single quotes? Regards, Harish -- http://mail.python.org/mailman/listinfo/python-list
tkinter import problem
Hi, I have Mandriva 2010.0 in my laptop. I installed python3.1 from the repository. But i am unable to import tkinter in python console. When I try to import tkinter I get the following error, `ImportError : No module named _tkinter` Am I doing something wrong? Thanks, -- http://mail.python.org/mailman/listinfo/python-list
