Newbie: How to convert a tuple of strings into a tuple of ints

2015-12-30 Thread otaksoftspamtrap
How do I get from here

t = ('1024', '1280')

to 

t = (1024, 1280)


Thanks for all help!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Newbie: How to convert a tuple of strings into a tuple of ints

2015-12-30 Thread otaksoftspamtrap
Thanks much - both solutions work well for me




On Wednesday, December 30, 2015 at 2:57:50 PM UTC-8, Ben Finney wrote:
> kierkega...@gmail.com writes:
> 
> > How do I get from here
> >
> > t = ('1024', '1280')
> >
> > to 
> >
> > t = (1024, 1280)
> 
> Both of those are assignment statements, so I'm not sure what you mean
> by "get from ... to". To translate one assignment statement to a different
> assignment statement, re-write the statement.
> 
> 
> But I think you want to produce a new sequence from an existing sequence.
> 
> The 'map' built-in function is useful for that::
> 
> sequence_of_numbers_as_text = ['1024', '1280']
> sequence_of_integers = map(int, sequence_of_numbers_as_text)
> 
> That sequence can then be iterated.
> 
> Another (more broadly useful) way is to use a generator expression::
> 
> sequence_of_integers = (int(item) for item in sequence_of_numbers_as_text)
> 
> 
> If you really want a tuple, just pass that sequence to the 'tuple'
> callable::
> 
> tuple_of_integers = tuple(
> int(item) for item in sequence_of_numbers_as_text)
> 
> or::
> 
> tuple_of_integers = tuple(map(int, sequence_of_numbers_as_text))
> 
> -- 
>  \  "Nothing is more sacred than the facts." --Sam Harris, _The End |
>   `\   of Faith_, 2004 |
> _o__)  |
> Ben Finney

-- 
https://mail.python.org/mailman/listinfo/python-list


Newbie: Check first two non-whitespace characters

2015-12-31 Thread otaksoftspamtrap
I need to check a string over which I have no control for the first 2 non-white 
space characters (which should be '[{').

The string would ideally be: '[{...' but could also be something like 
'  [  {  '.

Best to use re and how? Something else?
-- 
https://mail.python.org/mailman/listinfo/python-list


Newbie looking for elegant solution

2015-03-24 Thread otaksoftspamtrap
I have a list containing 9600 integer elements - each integer is either 0 or 1.

Starting at the front of the list, I need to combine 8 list elements into 1 by 
treating them as if they were bits of one byte with 1 and 0 denoting bit on/off 
(the 8th element would be the rightmost bit of the first byte).

The end result should be a new list that is 8 x shorter than the original list 
containing integers between 0 and 255.

Speed is not of utmost importance - an elegant solution is. Any suggestions?

Thanks for all input,
Kai
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Newbie looking for elegant solution

2015-03-24 Thread otaksoftspamtrap
On Tuesday, March 24, 2015 at 8:29:24 PM UTC-7, Chris Angelico wrote:
> On Wed, Mar 25, 2015 at 2:13 PM,   wrote:
> > I have a list containing 9600 integer elements - each integer is either 0 
> > or 1.
> >
> > Starting at the front of the list, I need to combine 8 list elements into 1 
> > by treating them as if they were bits of one byte with 1 and 0 denoting bit 
> > on/off (the 8th element would be the rightmost bit of the first byte).
> >
> > Speed is not of utmost importance - an elegant solution is. Any suggestions?
> 
> Oooh fun!
> 

> >>> l = [1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 
> >>> 1]
> >>> list(int(''.join(str(i) for i in l),2).to_bytes(len(l)//8,'big'))
> [177, 105, 117]
> 
> Convert it into a string, convert the string to an integer
> (interpreting it as binary), then convert the integer into a series of
> bytes, and interpret those bytes as a list of integers.
> 
> Example works in Python 3. For Python 2, you'll need ord() to get the
> integers at the end.
> 
> I'm not sure how elegant this is, but it's a fun trick to play with :)
> 
> Next idea please! I love these kinds of threads.
> 
> ChrisA


Impressive - thanks!
-- 
https://mail.python.org/mailman/listinfo/python-list