turn list of letters into an array of integers

S

Steven D'Aprano

Wasn't there a Monty Python sketch where a man carrying a parrot in a
cage comes into a shop full of stuffed animals and complains: No, I
don't admire the taxidermist for making that parrot look like it were
alive -- that beast bit me!

I don't think so. Are you thinking of the famous Monty Python "Dead
Parrot Sketch"? Here's one of many versions:

 
P

Peter Otten

Steven said:
I don't think so. Are you thinking of the famous Monty Python "Dead
Parrot Sketch"? Here's one of many versions:


My rendition was meant to be a travesty of that one, an "undead" parrot as a
follow-up to Dennis' "lich" post. I'm sorry I forgot the smiley ;)

- He didn't move, that was just the wind stirring his plumage.
- No, that parrot is alive and kicking, fresh as a daisy, full of beans...
- Aren't the glass eyes beautiful?
- Glass eyes -- he just blinked!

And so on. I'm off to work on my laden swallow branch of Python. It's going
to be a real heavy-weight...
 
M

Mark Lawrence

My rendition was meant to be a travesty of that one, an "undead" parrot as a
follow-up to Dennis' "lich" post. I'm sorry I forgot the smiley ;)

- He didn't move, that was just the wind stirring his plumage.
- No, that parrot is alive and kicking, fresh as a daisy, full of beans...
- Aren't the glass eyes beautiful?
- Glass eyes -- he just blinked!

And so on. I'm off to work on my laden swallow branch of Python. It's going
to be a real heavy-weight...

I just hope you get the technicalities correct. "That parrot wouldn't
move if you put 4 million volts through it". What rubbish. It should
either have been 4 million amps through it or 4 million volts across it.
I'm +1 for the former, although possibly biased by history.
 
P

Prasad, Ramit

David said:
Here's some example code. Theinput is a list which is a "matrix" of letters:
a b a
b b a

and I'd like to turn this into a Python array:

1 2 1
2 2 1

so 1 replaces a, and 2 replaces b. Here's the code I have so far:

L=['a b a\n','b b a\n']
s=' '.join(L)
seq1=('a','b')
seq2=('1','2')
d = dict(zip(seq1,seq2))
# Define method to replace letters according to dictionary (got this from
http://gommeitputor.wordpress.com/2...ace-multiple-words-or-characters-with-python/).
... def replace_all(text, dic):
... for i, j in dic.iteritems():
... text = text.replace(i, j)
... return text
...

seq =replace_all(s,d)
print seq
1 2 1
2 2 1

'1 2 1\n 2 2 1\n'
I'd suggest, if this is what you're referring to:

x = seq.split('\n ')
array_list = [ ]
next_3_d_array = []
range_of_seq = len(seq)
for num in range(0,range_of_seq):
if num % 3 != 0:
next_3_d_array.append(num)
if num % 3 == 0:
array_list.append(next_3_d_array)
next_3_d_array = [ ]

Wow, that looks complicated. Why hardcode to 3 instead of where ever
the newline is?

[ int(x.strip()) for subseq in seq.split('\n') for x in subseq.split() ]
[1, 2, 1, 2, 2, 1]
lst = []
# OR
for subseq in seq.split('\n'):
... for x in subseq.split():
... lst.append( int(x.strip()))
...

Ramit Prasad


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top