[CentralOH] chr()/ord() Ugliness
William McVey
wam at cisco.com
Mon Nov 11 16:55:32 CET 2013
[I've reorganized your reply to my mail for clarity in the answer]
> I understand 'hello world'.upper(), but not string.upper[i].
That was my mistake. I meant to type string.uppercase[i]:
>>> i=5
>>> chr(ord('A') + i)
'F'
>>> import string
>>> string.uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.uppercase[i]
'F'
On 11/11/2013 10:29 AM, jep200404 at columbus.rr.com wrote:
> I want to get a letter that is i greater than 'A',
> while iterating over arbitrary iterable.
What happens if your iterable is longer than the number of letters. Do
you want to wrap around back to 'a' again? Do you want to start pulling
in punctuation? Lowercase letters? The assumption seems to be that
you're dealing with ascii, but when you say "letter greater than 'A'",
do you mean by unicode codepoint? ASCII/UTF-8 encoding?
> Imagine something like:
>
> for i, foo in enumerate(bar):
> print 'Entity %s is %s' % (chr(ord('A') + i), foo)
I think I'd probably chose to use itertools rather than using enumerate
to pull offsets. For example:
>>> import string, itertools
>>> lorem_words=""""Lorem ipsum dolor sit amet, consectetur
adipisicing elit, sed do eiusmod tempor incididunt ut labore et
dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit
esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia deserunt mollit
anim id est laborum.""".split()
>>> list(itertools.izip(itertools.cycle(string.uppercase),
lorem_words))
[('A', '"Lorem'), ('B', 'ipsum'), ('C', 'dolor'), ('D', 'sit'),
('E', 'amet,'), ('F', 'consectetur'), ('G', 'adipisicing'), ('H',
'elit,'), ('I', 'sed'), ('J', 'do'), ('K', 'eiusmod'), ('L',
'tempor'), ('M', 'incididunt'), ('N', 'ut'), ('O', 'labore'), ('P',
'et'), ('Q', 'dolore'), ('R', 'magna'), ('S', 'aliqua.'), ('T',
'Ut'), ('U', 'enim'), ('V', 'ad'), ('W', 'minim'), ('X', 'veniam,'),
('Y', 'quis'), ('Z', 'nostrud'), ('A', 'exercitation'), ('B',
'ullamco'), ('C', 'laboris'), ('D', 'nisi'), ('E', 'ut'), ('F',
'aliquip'), ('G', 'ex'), ('H', 'ea'), ('I', 'commodo'), ('J',
'consequat.'), ('K', 'Duis'), ('L', 'aute'), ('M', 'irure'), ('N',
'dolor'), ('O', 'in'), ('P', 'reprehenderit'), ('Q', 'in'), ('R',
'voluptate'), ('S', 'velit'), ('T', 'esse'), ('U', 'cillum'), ('V',
'dolore'), ('W', 'eu'), ('X', 'fugiat'), ('Y', 'nulla'), ('Z',
'pariatur.'), ('A', 'Excepteur'), ('B', 'sint'), ('C', 'occaecat'),
('D', 'cupidatat'), ('E', 'non'), ('F', 'proident,'), ('G', 'sunt'),
('H', 'in'), ('I', 'culpa'), ('J', 'qui'), ('K', 'officia'), ('L',
'deserunt'), ('M', 'mollit'), ('N', 'anim'), ('O', 'id'), ('P',
'est'), ('Q', 'laborum.')]
Note that in this case, I used itertools.cycle to basically repeat the
uppercase letter string... However, you could choose to feed the izip
other well known strings (strings.letters and strings.printable are
possibly good options). The izip() returns an iterable that emits tuples
composed of one element off of each of it's set of (iterable) arguments
in turn. The conversion of the izip iterator into a list was simply for
display purposes. You wouldn't need/want this in your actual code.
-- William
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/centraloh/attachments/20131111/077d1e18/attachment.html>
More information about the CentralOH
mailing list