Re: How to fill in a dictionary with key and value from a string?

2018-04-01 Thread Mark Lawrence
On 02/04/18 03:24, C W wrote: Thank you Steven. I am frustrated that I can't enumerate a dictionary by position index. Maybe I want to shift by 2 positions, 5 positions... I want to know/learn how to manipulate dictionary with loop and by its position location. Frankly I think you'd be much

Re: How to fill in a dictionary with key and value from a string?

2018-04-01 Thread C W
Yes, you see right through me! I was able to conquer it, there's probably better ways: self.myDict = dict(zip(string.ascii_lowercase + string.ascii_uppercase, string.ascii_lowercase[shift:26] + string.ascii_lowercase[:shift] + string.ascii_uppercase[shift:26] + string.ascii_uppercase[:shift])) Ho

Re: How to fill in a dictionary with key and value from a string?

2018-04-01 Thread Steven D'Aprano
On Sun, 01 Apr 2018 22:24:31 -0400, C W wrote: > Thank you Steven. I am frustrated that I can't enumerate a dictionary by > position index. Why do you care about position index? > Maybe I want to shift by 2 positions, 5 positions... Sounds like you are trying to program the Caesar Shift cipher,

Re: How to fill in a dictionary with key and value from a string?

2018-04-01 Thread C W
now, the > > values are shift by one position. > > > > key:abcdefghijklmnopqrstuvwxyz > > value: BCDEFGHIJKLMNOPQRSTUVWXYZA > > > > Can I fill in a key and its corresponding value simultaneously on the > > fly? > > Of course. > > myDict = dict(zip(&

Re: How to fill in a dictionary with key and value from a string?

2018-04-01 Thread Steven D'Aprano
On Sun, 01 Apr 2018 20:52:35 -0400, C W wrote: > Thank you all for the response. > > What if I have myDict = {'a': 'B', 'b': 'C',...,'z':'A' }? So now, the > values are shift by one position. > > key:abcdefghijklm

Re: How to fill in a dictionary with key and value from a string?

2018-04-01 Thread C W
I am using Python 3.6. I ran the those lines and got a sorted dictionary by keys. On Sun, Apr 1, 2018 at 9:38 PM, Chris Angelico wrote: > On Mon, Apr 2, 2018 at 11:34 AM, C W wrote: > > A different but related question: > > > > myDict = dict(zip(string.ascii_lowercase + string.ascii_uppercase,

Re: How to fill in a dictionary with key and value from a string?

2018-04-01 Thread Chris Angelico
On Mon, Apr 2, 2018 at 11:34 AM, C W wrote: > A different but related question: > > myDict = dict(zip(string.ascii_lowercase + string.ascii_uppercase, > string.ascii_lowercase + string.ascii_uppercase)) >>myDict > {'A': 'A', 'B': 'B', 'C': 'C',...,'w': 'w', 'x': 'x', 'y': 'y', 'z': 'z'} > > Why ar

Re: How to fill in a dictionary with key and value from a string?

2018-04-01 Thread C W
hat if I have myDict = {'a': 'B', 'b': 'C',...,'z':'A' }? So now, the > values are shift by one position. > > key:abcdefghijklmnopqrstuvwxyz > value: BCDEFGHIJKLMNOPQRSTUVWXYZA > > Can I fill in a key and its corresponding value simultan

Re: How to fill in a dictionary with key and value from a string?

2018-04-01 Thread C W
Thank you all for the response. What if I have myDict = {'a': 'B', 'b': 'C',...,'z':'A' }? So now, the values are shift by one position. key:abcdefghijklmnopqrstuvwxyz value: BCDEFGHIJKLMNOPQRSTUVWXYZA Can I fill in a key and its

Re: How to fill in a dictionary with key and value from a string?

2018-04-01 Thread Chris Angelico
On Mon, Apr 2, 2018 at 3:03 AM, Rustom Mody wrote: > On Saturday, March 31, 2018 at 4:30:04 PM UTC+5:30, bartc wrote: >> On 30/03/2018 21:13, C W wrote: >> > Hello all, >> > >> > I want to create a dictionary. >> > >> > The keys are 26 lowercase letters. The values are 26 uppercase letters. >> > >

Re: How to fill in a dictionary with key and value from a string?

2018-04-01 Thread Rustom Mody
On Saturday, March 31, 2018 at 4:30:04 PM UTC+5:30, bartc wrote: > On 30/03/2018 21:13, C W wrote: > > Hello all, > > > > I want to create a dictionary. > > > > The keys are 26 lowercase letters. The values are 26 uppercase letters. > > > > The output should look like: > > {'a': 'A', 'b': 'B',..

Re: How to fill in a dictionary with key and value from a string?

2018-03-31 Thread bartc
On 30/03/2018 21:13, C W wrote: Hello all, I want to create a dictionary. The keys are 26 lowercase letters. The values are 26 uppercase letters. The output should look like: {'a': 'A', 'b': 'B',...,'z':'Z' } I know I can use string.ascii_lowercase and string.ascii_uppercase, but how do I u

Re: How to fill in a dictionary with key and value from a string?

2018-03-30 Thread MRAB
On 2018-03-30 21:13, C W wrote: Hello all, I want to create a dictionary. The keys are 26 lowercase letters. The values are 26 uppercase letters. The output should look like: {'a': 'A', 'b': 'B',...,'z':'Z' } I know I can use string.ascii_lowercase and string.ascii_uppercase, but how do I use

How to fill in a dictionary with key and value from a string?

2018-03-30 Thread C W
Hello all, I want to create a dictionary. The keys are 26 lowercase letters. The values are 26 uppercase letters. The output should look like: {'a': 'A', 'b': 'B',...,'z':'Z' } I know I can use string.ascii_lowercase and string.ascii_uppercase, but how do I use it exactly? I have tried the foll

Re: key and ..

2016-11-17 Thread justin walters
On Thu, Nov 17, 2016 at 7:05 PM, Val Krem via Python-list < python-list@python.org> wrote: > > > Hi all, > Sorry for asking such a basic question butI am trying to merge two > files(file1 and file2) and do some stuff. Merge the two files by the first > column(key). Here is the description of file

key and ..

2016-11-17 Thread Val Krem via Python-list
Hi all, Sorry for asking such a basic question butI am trying to merge two files(file1 and file2) and do some stuff. Merge the two files by the first column(key). Here is the description of files and what I would like to do. file1 key c1 c2 1 759 939 2 345 154571 3 251 350711 4 3749

Re: Q: sort's key and cmp parameters

2009-10-07 Thread Raymond Hettinger
[Hrvoje Niksic] > Note that stable sort has additional memory requirements.  In situations > where you don't need stability, but do need memory-efficient in-place > sorting, an unstable sort might well be preferred.  This is why > libraries such as C++'s STL offer both. FWIW, the "additional memor

Re: Q: sort's key and cmp parameters

2009-10-07 Thread Luis Zarrabeitia
On Tuesday 06 October 2009 02:40:46 pm Paul Rubin wrote: > > The problem is that if you allow to use the cmp, lot of programmers > > will use it straight away, not even bothering to know what that > > strange 'key' argument may be useful for. And they miss the how much > > handy 'key' is. > > Given

Re: Q: sort's key and cmp parameters

2009-10-07 Thread Bearophile
Hrvoje Niksic: > Note that stable sort has additional memory requirements.  In situations > where you don't need stability, but do need memory-efficient in-place > sorting, an unstable sort might well be preferred.  This is why > libraries such as C++'s STL offer both. There are stable sorts that

Re: Q: sort's key and cmp parameters

2009-10-07 Thread Hrvoje Niksic
Bearophile writes: > What I meant is that a general sorting routine, even in D, is better > to be first of all flexible. So I think it's better for the D built-in > sort to be stable, because such extra invariant allows you to use the > sort in more situations. Note that stable sort has addition

Re: Q: sort's key and cmp parameters

2009-10-07 Thread Bearophile
Paul Rubin: > Bearophile: > > sorting, and something that's surely not bug-prone. In such situation > > having a 'key' argument is *better*. Such sort can be stable. > > Nothing stops comparison sorting from being stable.  Since the rest of > your post seems premised on the opposite, I hope that cl

Re: Q: sort's key and cmp parameters

2009-10-07 Thread Steven D'Aprano
On Tue, 06 Oct 2009 23:20:13 -0700, Paul Rubin wrote: > Bearophile writes: >> sorting, and something that's surely not bug-prone. In such situation >> having a 'key' argument is *better*. Such sort can be stable. > > Nothing stops comparison sorting from being stable. Since the rest of > your p

Re: Q: sort's key and cmp parameters

2009-10-06 Thread Paul Rubin
Bearophile writes: > sorting, and something that's surely not bug-prone. In such situation > having a 'key' argument is *better*. Such sort can be stable. Nothing stops comparison sorting from being stable. Since the rest of your post seems premised on the opposite, I hope that clears things up

Re: Q: sort's key and cmp parameters

2009-10-06 Thread Steven D'Aprano
On Tue, 06 Oct 2009 12:28:00 -0700, Raymond Hettinger wrote: > [bearophile] >> I love the 'key', it makes my code simpler and it's simpler to >> understand. I am not opposed to the idea of keeping cmp, that in some >> rare cases may be useful or more natural. >> >> The problem is that if you allow

Re: Q: sort's key and cmp parameters

2009-10-06 Thread Bearophile
ke that in the std lib, but with a weird name like >> SchwartzSort that's surely not the first standard sort they look >> for... this is bad. > "key" and the DSU (Schwartz) sorting pattern makes lots of sense in > scripting languages like Python that are primaril

Re: Q: sort's key and cmp parameters

2009-10-06 Thread Paul Rubin
Raymond Hettinger writes: > Once Kernighan and Ritchie put C's qsort() into the food supply, > we were doomed. It was already too late. Knuth vol 3 came out in 1973(?) and its sorting half is mostly about comparison sorting. > FWIW, I think the standard library's unittest module is also a > co

Re: Q: sort's key and cmp parameters

2009-10-06 Thread Raymond Hettinger
[bearophile] > I love the 'key', it makes my code simpler and it's simpler to > understand. I am not opposed to the idea of keeping cmp, that in some > rare cases may be useful or more natural. > > The problem is that if you allow to use the cmp, lot of programmers > will use it straight away, not

Re: Q: sort's key and cmp parameters

2009-10-06 Thread Paul Rubin
t; function a key function argument is often better. They in the end have > added something like that in the std lib, but with a weird name like > SchwartzSort that's surely not the first standard sort they look > for... this is bad. "key" and the DSU (Schwartz) sort

Re: Q: sort's key and cmp parameters

2009-10-06 Thread Bearophile
Raymond Hettinger: > Psychologically, the thing that I find to be interesting is > that beginners and intermediate users seem to take to key > functions more readily than old timers.  The key function seems > to be an easy thing to teach (perhaps because that's the > way Excel sorts and the way SQ

Re: Q: sort's key and cmp parameters

2009-10-05 Thread Steven D'Aprano
On Mon, 05 Oct 2009 19:34:27 -0700, Paul Rubin wrote: > Raymond Hettinger writes: >> When Guido made the final call, I'm sure he was balancing a number of >> goals including language simplification and one-way-to-do-it. I'm also >> sure that sorting lazily evaluated infinite sequences was not o

Re: Q: sort's key and cmp parameters

2009-10-05 Thread Paul Rubin
Raymond Hettinger writes: > There were two sorts in my post and only one in yours. > That's why you didn't get the same answer. Whoops, missed that. > When Guido made the final call, I'm sure he was balancing > a number of goals including language simplification and > one-way-to-do-it. I'm also

Re: Q: sort's key and cmp parameters

2009-10-05 Thread Raymond Hettinger
to being able to write one's natural thought pattern if that pattern > happens to be a comparison function?  Who wants to concoct these > ad-hoc encodings for every ordering? Your (x%2, x) idea seems like a good way to do it in a single pass. Also, it seems to be a natural translation o

Re: Q: sort's key and cmp parameters

2009-10-05 Thread Paul Rubin
Raymond Hettinger writes: > FWIW, you could also just flatten it to: [(1,3,7,5), (19,23)]. > > The point is that the tree comparisons you presented have a > predetermined order of values to compare, so they either be recast a > flattened list of comparison values or the tree itself can be cast >

Re: Q: sort's key and cmp parameters

2009-10-05 Thread Raymond Hettinger
> > So, it looks like the relevant comparison values can be stored in > > nested lists: > > >list_of_lists = \ > > [[1, [3, [], []], > > [7, [5, [], []], []]], > > [19, [23, [], []], > > []], > > ] > > Now you've copied all the data out of the original tree,

Re: Q: sort's key and cmp parameters

2009-10-05 Thread Paul Rubin
Raymond Hettinger writes: > So, it looks like the relevant comparison values can be stored in > nested lists: > >list_of_lists = \ > [[1, [3, [], []], > [7, [5, [], []], []]], > [19, [23, [], []], > []], > ] Now you've copied all the data out of the origin

Re: Q: sort's key and cmp parameters

2009-10-04 Thread Raymond Hettinger
[Paul Rubin] > Example of list of trees (nested dicts). In practice you could get > such a list from the simplejson module: > > list_of_trees = [{'value':1, 'left':{'value':3,'left':None,'right':None}, >'right':{'value':7,'left':{'value':5, ...}}}, >

Re: Q: sort's key and cmp parameters

2009-10-03 Thread Paul Rubin
kj writes: > !!! > > Maybe Haskell is much handier than I give it credit for, but it's > hard for me to imagine that it is as convenient as Python 3, even > without the cmp sort option... Heh, yeah, I was being a bit snarky/grouchy. Haskell has a very steep learning curve and will never be as

Re: Q: sort's key and cmp parameters

2009-10-03 Thread kj
In <7xtyyhikrl@ruckus.brouhaha.com> Paul Rubin writes: >Python 2.x provides two ways >and you can use whichever one fits the application better. I have >never understood why Python 3.x finds it necessary to break one of >them. Maybe I can migrate to Haskell by

Re: Q: sort's key and cmp parameters

2009-10-03 Thread Paul Rubin
Paul Rubin writes: > c = compare(tree1['left'], tree2['left']) Of course this recursive call crashes if either branch is None. Oh well, I'll stop trying to correct it since I'm sure you get the idea. -- http://mail.python.org/mailman/listinfo/python-list

Re: Q: sort's key and cmp parameters

2009-10-03 Thread Paul Rubin
Paul Rubin writes: > Example comparison function: > > def compare(tree1, tree2): > c = cmp(tree1['value'], tree2['value']) > if c != 0: return c > c = cmp(tree1['left'], tree2['left']) > if c != 0: return c > return cmp(tree1['right'], tree

Re: Q: sort's key and cmp parameters

2009-10-03 Thread Paul Rubin
Raymond Hettinger writes: > Can you give an example of a list of trees and a cmp function > that recursively compares them? Example of list of trees (nested dicts). In practice you could get such a list from the simplejson module: list_of_trees = [{'value':1, 'left':{'value':3,'left':None,'r

Re: Q: sort's key and cmp parameters

2009-10-02 Thread Raymond Hettinger
[Paul Rubin] > The idea was that you have a list of trees that you want to sort, and > an ordering relation between trees: > >    def gt(tree1, tree2): ... Are the trees user defined classes? Can the gt() function be added incorporated as __lt__ method so that you can just run a plain sort:

Re: Q: sort's key and cmp parameters

2009-10-02 Thread Raymond Hettinger
[Paul Rubin] > The idea was that you have a list of trees that you want to sort, and > an ordering relation between trees: > >    def gt(tree1, tree2): ... > > where you recursively descend both trees until you find an unequal > pair of nodes.  You're not trying to sort the nodes within a single >

Re: Q: sort's key and cmp parameters

2009-10-02 Thread Paul Rubin
Raymond Hettinger writes: > I'm not sure what you mean by this. What are the semantics of > sorting a tree? Can you outline an example of tree that > could be sorted easily with a cmp function but not a key function? The idea was that you have a list of trees that you want to sort, and an order

Re: Q: sort's key and cmp parameters

2009-10-02 Thread Raymond Hettinger
[Paul Rubin] > Yes, think of sorting tree structures where you have to recursively > compare them til you find an unequal pair of nodes.   I'm not sure what you mean by this. What are the semantics of sorting a tree? Can you outline an example of tree that could be sorted easily with a cmp funct

Re: Q: sort's key and cmp parameters

2009-10-02 Thread Paul Rubin
Scott David Daniels writes: > Most cases are moreeasily done with key, and it is > a good idea to make the most accessible way to a sort be the most > efficient one. In the rare case that you really want each comparison, > the cmp-injection function will do nicely (and can be

Re: Q: sort's key and cmp parameters

2009-10-02 Thread Scott David Daniels
it is run once per element. Most cases are moreeasily done with key, and it is a good idea to make the most accessible way to a sort be the most efficient one. In the rare case that you really want each comparison, the cmp-injection function will do nicely (and can be written as a recipe. In short,

Re: Q: sort's key and cmp parameters

2009-10-02 Thread Duncan Booth
Paul Rubin <http://phr...@nospam.invalid> wrote: > Duncan Booth writes: >> > Is there a real-life sorting task that requires (or is far more >> > efficient with) cmp and can't be easily achieved with key and reverse? >> > >> There is no sorting

Re: Q: sort's key and cmp parameters

2009-10-01 Thread Paul Rubin
Bearophile writes: > > Yes, think of sorting tree structures where you have to recursively > > compare them til you find an unequal pair of nodes. > > That's cute. In what situations do you have to perform such kind of > sort? It came up in a search engine application I've been involved with. --

Re: Q: sort's key and cmp parameters

2009-10-01 Thread kj
In alex23 writes: >kj wrote: >> This example convinces me that it was a bad idea to >> get rid of cmp in Python 3, even if situations like this one are >> rare. >It sounds like the entire point of this exercise was to get other >people to confirm your bias for you. The only problem with this

Re: Q: sort's key and cmp parameters

2009-10-01 Thread alex23
kj wrote: > This example convinces me that it was a bad idea to > get rid of cmp in Python 3, even if situations like this one are > rare. It sounds like the entire point of this exercise was to get other people to confirm your bias for you. -- http://mail.python.org/mailman/listinfo/python-list

Re: Q: sort's key and cmp parameters

2009-10-01 Thread Paul Rubin
Raymond Hettinger writes: > If you're assuming a consistent sort-order (transitivity, not > evolving over time, etc), then the cmp method and key method > are mathematically equivalent (you could always do a compare > sort first, record the order produced, and assign the position > number as a key

Re: Q: sort's key and cmp parameters

2009-10-01 Thread Paul Rubin
Duncan Booth writes: > > Is there a real-life sorting task that requires (or is far more > > efficient with) cmp and can't be easily achieved with key and reverse? > > > There is no sorting task that *requires* cmp. If all else fails you can > define a key class

Re: Q: sort's key and cmp parameters

2009-10-01 Thread kj
In <7x1vln2bzh@ruckus.brouhaha.com> Paul Rubin <http://phr...@nospam.invalid> writes: >kj writes: >> Is there a real-life sorting task that requires (or is far more >> efficient with) cmp and can't be easily achieved with key and >> reverse? >Yes, t

Re: Q: sort's key and cmp parameters

2009-10-01 Thread kj
In Raymond Hettinger writes: Thanks for an extremely helpful reply! >If you need to sort by an ascending primary key and a descending >secondary key, you may find it easiest to sort in two passes >(taking advantage of guaranteed sort stability): >sorted(s, key=seconda

Re: Q: sort's key and cmp parameters

2009-10-01 Thread Raymond Hettinger
On Oct 1, 10:08 am, kj wrote: > Challenge: to come up with a sorting task that cannot be achieved > by passing to the sort method (or sorted function) suitable values > for its key and reverse parameters, but instead *require* giving > a value to its cmp parameter. If you&#

Re: Q: sort's key and cmp parameters

2009-10-01 Thread Duncan Booth
kj wrote: > Is there a real-life sorting task that requires (or is far more > efficient with) cmp and can't be easily achieved with key and > reverse? > There is no sorting task that *requires* cmp. If all else fails you can define a key class to wrap the original wrapper su

Re: Q: sort's key and cmp parameters

2009-10-01 Thread Bearophile
Paul Rubin: > Yes, think of sorting tree structures where you have to recursively > compare them til you find an unequal pair of nodes. That's cute. In what situations do you have to perform such kind of sort? Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: Q: sort's key and cmp parameters

2009-10-01 Thread Paul Rubin
kj writes: > Is there a real-life sorting task that requires (or is far more > efficient with) cmp and can't be easily achieved with key and > reverse? Yes, think of sorting tree structures where you have to recursively compare them til you find an unequal pair of nodes. To

Re: Q: sort's key and cmp parameters

2009-10-01 Thread Ethan Furman
Laszlo Nagy wrote: can be achieved (to a very good approximation at least) with scrambled = some_list.sort(key=lambda x: random()) Is there a real-life sorting task that requires (or is far more efficient with) cmp and can't be easily achieved with key and reverse? The

Re: Q: sort's key and cmp parameters

2009-10-01 Thread Carsten Haese
kj wrote: > > Challenge: to come up with a sorting task that cannot be achieved > by passing to the sort method (or sorted function) suitable values > for its key and reverse parameters, but instead *require* giving > a value to its cmp parameter. Such a task can't exist, b

Re: Q: sort's key and cmp parameters

2009-10-01 Thread Laszlo Nagy
can be achieved (to a very good approximation at least) with scrambled = some_list.sort(key=lambda x: random()) Is there a real-life sorting task that requires (or is far more efficient with) cmp and can't be easily achieved with key and reverse? The core developers don't t

Re: Q: sort's key and cmp parameters

2009-10-01 Thread Peter Otten
kj wrote: > Challenge: to come up with a sorting task that cannot be achieved > by passing to the sort method (or sorted function) suitable values > for its key and reverse parameters, but instead *require* giving > a value to its cmp parameter. > > For example, > > f

Re: Q: sort's key and cmp parameters

2009-10-01 Thread Laszlo Nagy
Is this a homework? Challenge: to come up with a sorting task that cannot be achieved by passing to the sort method (or sorted function) suitable values for its key and reverse parameters, but instead *require* giving a value to its cmp parameter. Let me put up this question: how do you

Q: sort's key and cmp parameters

2009-10-01 Thread kj
Challenge: to come up with a sorting task that cannot be achieved by passing to the sort method (or sorted function) suitable values for its key and reverse parameters, but instead *require* giving a value to its cmp parameter. For example, from random import random scrambled = some_list.sort

Re: python function for retrieving key and encryption

2009-07-27 Thread Piet van Oostrum
> jayshree (j) wrote: >j> import M2Crypto >j> from M2Crypto import RSA,SSL >j> def encrypt(): >j> pk = open('my_key.public.pem', 'rb').read() >j> rsa = M2Crypto.RSA.load_pub_key(pk) #return a M2Crypto.RSA.RSA_pub >j> object. >j> plaintext = 4545479545655576767767686688782344 >j>

Re: python function for retrieving key and encryption

2009-07-26 Thread jayshree
On Jul 23, 5:48 pm, Piet van Oostrum wrote: > > jayshree (j) wrote: > >j> On Jul 21, 8:59 pm, Piet van Oostrum wrote: > >>> The recipient_public_key.pem file is the public key of the recipient > >>> which means the person that is going to receive the encrypted message. > >>> You should get i

Re: python function for retrieving key and encryption

2009-07-23 Thread Piet van Oostrum
> jayshree (j) wrote: >j> On Jul 21, 8:59 pm, Piet van Oostrum wrote: >>> The recipient_public_key.pem file is the public key of the recipient >>> which means the person that is going to receive the encrypted message. >>> You should get it from the recipient him/herself or from some key sto

Re: python function for retrieving key and encryption

2009-07-22 Thread jayshree
On Jul 21, 8:59 pm, Piet van Oostrum wrote: > > jayshree (j) wrote: > >j> M2Crypto package not showing the 'recipient_public_key.pem' file at > >j> linux terminal .how do i get/connect with recipient public key. > >j> exactly i need to check how can i open this file through linux > >j> comman

Re: python function for retrieving key and encryption

2009-07-21 Thread Piet van Oostrum
> jayshree (j) wrote: >j> M2Crypto package not showing the 'recipient_public_key.pem' file at >j> linux terminal .how do i get/connect with recipient public key. >j> exactly i need to check how can i open this file through linux >j> commands. >j> import M2Crypto def encrypt(): recip = M2Cry

python function for retrieving key and encryption

2009-07-20 Thread jayshree
M2Crypto package not showing the 'recipient_public_key.pem' file at linux terminal .how do i get/connect with recipient public key. exactly i need to check how can i open this file through linux commands. import M2Crypto def encrypt(): recip = M2Crypto.RSA.load_pub_key(open ('recipient_public_key