> >> anish singh wrote:
> >>
> >>> I need a custom comparator.
> >>>
> >>> dictionary = {a:[b,c], d:[e,f]}
> >>>
> >>> If both 'b' and 'e' belong to the same bin
> >>> then it should be compared based on 'c' and 'f'.
> >>>
> >>> However, I want to also represent the result of the
> >>> sorted operation in a ordered dictionary as order is
> >>> important.
> >>>
> >>> My custom comparator is something like this:
> >>>
> >>>
> >>> ''' x and y is a list of two elements each'''
> >>> def cmpr(x, y):
> >>>    r = 3
> >>>    if x[0]//r != y[0]//r:
> >>>        return x[0]//r < y[0]//r
> >>>    return x[1] < y[1]
> >>
> >> This looks like it should be called less() rather than compare() as it
> >> doesn't differentiate between the x < y and x == y case.
> >>
> >>> Please note it is not exactly comparing the first elements
> >>> of the value but checking if they belong to the same bin
> >>> and they do then it checks the second element as as shown
> >>> above.
> >>
> >> The effect should be the same.
> >
> > Well no, take the case of [1,100] and [2,0]
> > Both belong to same bin suppose then it should
> > be sorted based on second index and I would
> > expect [2,0] [1,100] as output.
> > This is not happening currently with the original
> > code I have sent.
>
> I think that is because you do not consider all three cases.
> Let's start with a function cmp() modeled after the Python 2 built-in
>
> def cmp(a, b):
>     if a < b:
>         return -1
>     elif a > b:
>         return 1
>     return 0
>
> Then your comparator could be fixed (I think) as follows
>
> def compare(x, y):
>     def bin(a): return a[0] // 3
>
>     result = cmp(bin(x), bin(y))
>     if result:
>         return result
>     return cmp(x[1], y[1])
>
> and that "fixed" version would be equivalent (I think) to
>
> def compare(x, y)
>     def key(a): return (a[0] // 3, a[1])
>
>     return cmp((key(x), key(y))
>
> That said, even if you use Python 2 you should use sorted() with a key
> function rather than a comparison -- as shown below. Did that work for you?
>

Yes it did. Thanks.

>
> >>> Example:
> >>> {0:[0, 8], 1:[2, 5], 2:[2, 11], 3:[16, 17], 4:[13, 14], 5:[1, 17],
> >>> {6:[17,
> >>> 17] }
> >>> output should be:
> >>> {1:[2, 5], 0:[0, 8], 2:[2, 11], 5:[1, 17], 4:[13, 14], 3:[16, 17],
> >>> {6:[17,
> >>> 17] }
> >>
> >>>>> input = {0:[0, 8], 1:[2, 5], 2:[2, 11], 3:[16, 17], 4:[13, 14], 5:[1,
> >> 17], 6:[17,
> >> ... 17] }
> >>>>> wanted = {1:[2, 5], 0:[0, 8], 2:[2, 11], 5:[1, 17], 4:[13, 14],
> 3:[16,
> >> 17], 6:[17,
> >> ... 17] }
> >>>>> output = {k: v for k, v in sorted(input.items(), key=lambda x: (x[1]
> >> [0]//3, x[1][1]))}
> >>>>> assert list(output.items()) == list(wanted.items())
> >>
> >> As written it will work with CPython 3.6. However, for compatibility
> with
> >> other versions of Python I recommend that you replace the plain dicts
> >> above with collections.OrderedDict instances. Quoting
> >>
> >> https://docs.python.org/dev/whatsnew/3.6.html#whatsnew36-pep520
> >>
> >> """
> >> The order-preserving aspect of this new [dict] implementation is
> >> considered an implementation detail and should not be relied upon [...]
> >> """
>
>
>
>
> ------------------------------
>
> Message: 3
> Date: Mon, 26 Jun 2017 11:22:21 -0600
> From: Mats Wichmann <m...@wichmann.us>
> To: tutor@python.org
> Subject: Re: [Tutor] custom comparator with ordered list
> Message-ID: <9ecb3bcb-ceaa-8e2c-d6a2-edf78689a...@wichmann.us>
> Content-Type: text/plain; charset=utf-8
>
> On 06/26/2017 10:38 AM, Anish Kumar wrote:
> >
> >> anish singh wrote:
> >>
> >>> I need a custom comparator.
> >>>
> >>> dictionary = {a:[b,c], d:[e,f]}
> >>>
> >>> If both 'b' and 'e' belong to the same bin
>
> if would help alot if your problem statement included a description of
> what "same bin" is.
>
> > Well no, take the case of [1,100] and [2,0]
> > Both belong to same bin
>
> how would we conclude that these "belong to same bin"?
>
>
>
>
> ------------------------------
>
> Message: 4
> Date: Mon, 26 Jun 2017 11:18:36 -0600
> From: Mats Wichmann <m...@wichmann.us>
> To: tutor@python.org
> Subject: Re: [Tutor] how-to generate specific lines of text from two
>         python lists
> Message-ID: <56d6e476-64aa-1a3a-b156-83b06a4f1...@wichmann.us>
> Content-Type: text/plain; charset=utf-8
>
> On 06/25/2017 12:44 PM, Tahir Hafiz wrote:
> > Thanks Alan and Peter,
> >
> > Alan you are right this could be solved via an SQL statement but I was
> > asked to finish the python script.
> > Anyways, this worked and helped to solve the problem in the end:
> >
> > # Create iterator object, dictionary which can be used to iterate
> against.
> > b_iter = iter(new_emails)
> >
> >
> > print "Creating a list of usernames and email addresses from retreived
> > database data:"
> > if __name__ == "__main__":
> >     dictionary = dict(zip(usernames, new_emails))
> >     my_list = []
> >     for username in usernames:
> >
> >       my_list.append({'username':username, 'email':next(b_iter)})
> >
> >     print my_list
> >
> > print
> > print "Creating a file called update_emails.sql with UPDATE statements
> from
> > the list."
> > # Open a file in write mode and write the UPDATE sql statements to the
> file
> > # Close the file once iterated against.
> > with open('update_emails.sql', 'w') as f:
> >    for i in my_list:
> >      mystring = "UPDATE users set email='{0}' WHERE username='{1}';"
> >      new_mystring = mystring.format(i['email'], i['username'])
> >      f.write(new_mystring + '\n')
> > f.close()
>
> I'd like to point out that with this solution you are creating a
> dictionary named "dictionary" but then never using it, and you then
> proceed to make a list consisting of dictionaries which each look like a
> single record.  Far be it from me to tell you this is wrong since you
> report the effort is working out!!! But you do seem to be redoing work.
>
> Consider this as a simplified alternative, which actually uses the
> dictionary you create up front (ignoring the very valid "don't do it
> this way" comments you have already received, let the database connector
> handle the quoting).  Also note you don't need to "close the file once
> iterated against", since that's the exact purpose of the 'with'
> statement - to handle that cleanup for you.
>
> print "Creating dictionary of usernames and email addresses from
> retreived database data:"
> if __name__ == "__main__":
>     dictionary = dict(zip(usernames, new_emails))
>     print dictionary
>
>     print
>     print "Creating a file called update_emails.sql with UPDATE
> statements from the list."
>     # Open a file in write mode and write the UPDATE sql statements to
> the file
>     with open('update_emails.sql', 'w') as f:
>        for key in dictionary.keys():
>          mystring = "UPDATE users set email='{0}' WHERE username='{1}';"
>          new_mystring = mystring.format(dictionary[key], key)
>          f.write(new_mystring + '\n')
>
>
>
>
> ------------------------------
>
> Message: 5
> Date: Mon, 26 Jun 2017 22:32:16 -0700
> From: Micheal Dale Peterson <michealdpeter...@gmail.com>
> To: tutor@python.org
> Subject: [Tutor] Using files to read data
> Message-ID: <996421dd-f654-4efb-9417-6bf2782d3...@gmail.com>
> Content-Type: text/plain; charset="utf-8"
>
> hello I?m new to python. I have been trying to teach myself without asking
> questions because i feel i learn more. But i am stuck on this for a bout a
> week now.  I am trying to write something that stores x and y data with a
> time reference and then use it later to create some type of a graph.  I
> have tried saving to text files. and the closest i have come is using the
> json save.  this is the closest output i can get(posted below).  Trying to
> read it back does me no good. i can print each thing individually. everyday
> i try i either get a bunch of added characters or it separates every
> character. Sorry i can?t  post all of the ways i have tried. I have been at
> this for a week and have changed things a thousand times and made a mess of
> most my files at this point. haha. Any help would be appreciated.  I am
> running OS X version 10.12.5 with python 3.6.1. thank you in advance
>
>
> my saved lists
>
> -------------- next part --------------
>
>
> my code to get saved list
> -------------- next part --------------
>
>
>
>
> ------------------------------
>
> Message: 6
> Date: Tue, 27 Jun 2017 10:52:07 +0200
> From: Peter Otten <__pete...@web.de>
> To: tutor@python.org
> Subject: Re: [Tutor] Using files to read data
> Message-ID: <oit6bo$bcn$1...@blaine.gmane.org>
> Content-Type: text/plain; charset="ISO-8859-1"
>
> Micheal Dale Peterson wrote:
>
> > my saved lists
>
> Hello Micheal!
>
> Have a look at
>
> <https://mail.python.org/pipermail/tutor/2017-June/111469.htm>
>
> to learn what we see of your mail.
>
> Unfortunately attachments are stripped off. Please resend as plain text,
> with your code and data inlined in the body of the mail. (Make sure that
> the
> data is small, just enough that we can get an idea of its structure.)
>
>
>
>
> ------------------------------
>
> Subject: Digest Footer
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> https://mail.python.org/mailman/listinfo/tutor
>
>
> ------------------------------
>
> End of Tutor Digest, Vol 160, Issue 34
> **************************************
>
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to