Re: Python 3 how to convert a list of bytes objects to a list of strings?

2020-08-30 Thread Chris Green
Cameron Simpson wrote: > On 29Aug2020 16:50, Chris Green wrote: > >However the problem appears to be that internally in Python 3 mailbox > >class there is an assumption that it's being given 'ascii'. Here's > >the error (and I'm doing no processing of the message at all):- > > > >Traceback (

Re: Python 3 how to convert a list of bytes objects to a list of strings?

2020-08-30 Thread Chris Green
Karsten Hilbert wrote: > > However the problem appears to be that internally in Python 3 mailbox > > class there is an assumption that it's being given 'ascii'. > > Do you really _need_ the mailbox class ? From what you've > written so far my understanding was that you receive data > (bytes) and

Re: Python 3 how to convert a list of bytes objects to a list of strings?

2020-08-29 Thread Cameron Simpson
On 29Aug2020 16:50, Chris Green wrote: >However the problem appears to be that internally in Python 3 mailbox >class there is an assumption that it's being given 'ascii'. Here's >the error (and I'm doing no processing of the message at all):- > >Traceback (most recent call last): > File

Aw: Re: Re: Python 3 how to convert a list of bytes objects to a list of strings?

2020-08-29 Thread Karsten Hilbert
> Just appending a message as a raw file to a mailbox, doesn't properly > add it as a new message. You need to add a From: line to the front, and > then go through the message and alter any line that begins as "From:" > (and possibly any line that begins with something like ">From:" or > ">>From:"

Re: Aw: Re: Python 3 how to convert a list of bytes objects to a list of strings?

2020-08-29 Thread Richard Damon
On 8/29/20 3:31 PM, Karsten Hilbert wrote: >> However the problem appears to be that internally in Python 3 mailbox >> class there is an assumption that it's being given 'ascii'. > Do you really _need_ the mailbox class ? From what you've > written so far my understanding was that you receive data

Aw: Re: Python 3 how to convert a list of bytes objects to a list of strings?

2020-08-29 Thread Karsten Hilbert
> However the problem appears to be that internally in Python 3 mailbox > class there is an assumption that it's being given 'ascii'. Do you really _need_ the mailbox class ? From what you've written so far my understanding was that you receive data (bytes) and want to append that to a file (which

Re: Python 3 how to convert a list of bytes objects to a list of strings?

2020-08-29 Thread Richard Damon
On 8/29/20 11:50 AM, Chris Green wrote: > Chris Green wrote: >> Dennis Lee Bieber wrote: >>> On Fri, 28 Aug 2020 12:26:07 +0100, Chris Green declaimed >>> the >>> following: >>> >>> >>> Maybe I shouldn't but Python 2 has been managing to do so for several years without any issues. I

Re: Python 3 how to convert a list of bytes objects to a list of strings?

2020-08-29 Thread Chris Green
Chris Green wrote: > Dennis Lee Bieber wrote: > > On Fri, 28 Aug 2020 12:26:07 +0100, Chris Green declaimed > > the > > following: > > > > > > > > >Maybe I shouldn't but Python 2 has been managing to do so for several > > >years without any issues. I know I *could* put the exceptions in a >

Re: Python 3 how to convert a list of bytes objects to a list of strings?

2020-08-29 Thread Chris Green
Dennis Lee Bieber wrote: > On Fri, 28 Aug 2020 12:26:07 +0100, Chris Green declaimed the > following: > > > > >Maybe I shouldn't but Python 2 has been managing to do so for several > >years without any issues. I know I *could* put the exceptions in a > >bucket somewhere and deal with them sep

Re: Python 3 how to convert a list of bytes objects to a list of strings?

2020-08-29 Thread Grant Edwards
On 2020-08-28, Chris Green wrote: > Maybe I shouldn't but Python 2 has been managing to do so for several > years without any issues. I know I *could* put the exceptions in a > bucket somewhere and deal with them separately but I'd really rather > not. Then just leave it as bytes and do whateve

Re: Python 3 how to convert a list of bytes objects to a list of strings?

2020-08-29 Thread Grant Edwards
On 2020-08-27, Chris Green wrote: > bbb = [b'aaa', b'bbb', b'ccc'] > sss = [] > for i in range(0, 2): > sss.append(str(bbb[i]) > > but that does seem a bit clumsy. Is there a better way? sss = [str(s) for s in bbb] -- Grant -- https://mail.python.org/mailman/listinfo/pyth

Re: Python 3 how to convert a list of bytes objects to a list of strings?

2020-08-29 Thread Grant Edwards
On 2020-08-27, Marco Sulla wrote: > Are you sure you want `str()`? > str(b'aaa') > "b'aaa'" > > Probably you want: > > map(lambda x: x.decode(), bbb) If you're an old Scheme or Lisp programmer. :) This is probably the more usual way to spell it: sss = [x.decode() for x in bbb] -- ht

Re: Python 3 how to convert a list of bytes objects to a list of strings?

2020-08-28 Thread Cameron Simpson
On 28Aug2020 12:26, Chris Green wrote: >Cameron Simpson wrote: >> POP3 is presumably handing you bytes containing a message. If the >> Python >> email.BytesParser doesn't handle it, stash the raw bytes _elsewhere_ in >> a distinct file in some directory. >> >> with open('evil_msg_bytes', 'wb

Re: Python 3 how to convert a list of bytes objects to a list of strings?

2020-08-28 Thread Richard Damon
On 8/28/20 8:44 AM, Chris Green wrote: > Stefan Ram wrote: >> Chris Green writes: >>> Therein lies the problem, the incoming byte stream *isn't* ASCII, it's >>> an E-Mail message which may, for example, have UTF-8 or other encoded >>> characters in it. Hopefully it will have an encoding given in

Re: Python 3 how to convert a list of bytes objects to a list of strings?

2020-08-28 Thread Chris Angelico
On Fri, Aug 28, 2020 at 11:24 PM Chris Green wrote: > > Chris Angelico wrote: > > > > Also, if you're parsing an email message, you can and should be doing > > so with respect to the encoding(s) stipulated in the headers, after > > which you will have valid Unicode text. > > > But not all E-Mail

Re: Aw: Re: Python 3 how to convert a list of bytes objects to a list of strings?

2020-08-28 Thread D'Arcy Cain
On 2020-08-28 08:30, Richard Damon wrote: > This might be one of the cases where Python 2's lack handling of string > vs bytes was an advantage. For English speaking Americans. > Python2 handled that sort of case quite easily. Python 3 on the other > hand, will have issue converting the byte mess

Re: Python 3 how to convert a list of bytes objects to a list of strings?

2020-08-28 Thread Chris Green
Chris Angelico wrote: > > Also, if you're parsing an email message, you can and should be doing > so with respect to the encoding(s) stipulated in the headers, after > which you will have valid Unicode text. > But not all E-Mail messages are 'well behaved', the above works fine if the headers sp

Aw: Re: Python 3 how to convert a list of bytes objects to a list of strings?

2020-08-28 Thread Karsten Hilbert
> I want to transport the message into my mbox and Python 3 won't do it > without knowing how it's encoded whereas Python 2 just stuffed it in > there 'as is'. > > I want Python 3's mailbox class to juyst put what I tell it (even if > mis-formatted or mis-encoded) into the mbox. I guess using the

Re: Python 3 how to convert a list of bytes objects to a list of strings?

2020-08-28 Thread Richard Damon
On 8/28/20 8:39 AM, Chris Green wrote: > Richard Damon wrote: >> On 8/28/20 7:50 AM, Karsten Hilbert wrote: > No interpreation requires, since parsing failed. Then you can start > dealing with these exceptions. _Do not_ write unparsable messages into > an mbox! > Maybe I shoul

Re: Python 3 how to convert a list of bytes objects to a list of strings?

2020-08-28 Thread Chris Angelico
On Fri, Aug 28, 2020 at 10:51 PM Chris Green wrote: > > > One possible solution in Python3 is to decode the byte string using an > > encoding that allows all 256 byte values, so it won't raise any encoding > > errors, just give your possibly non-sense characters for non-ASCII text. > > > But this

Re: Python 3 how to convert a list of bytes objects to a list of strings?

2020-08-28 Thread Chris Green
Stefan Ram wrote: > Chris Green writes: > >Therein lies the problem, the incoming byte stream *isn't* ASCII, it's > >an E-Mail message which may, for example, have UTF-8 or other encoded > >characters in it. Hopefully it will have an encoding given in the > >header but that's only if the sender

Re: Python 3 how to convert a list of bytes objects to a list of strings?

2020-08-28 Thread Chris Green
Richard Damon wrote: > On 8/28/20 7:50 AM, Karsten Hilbert wrote: > >>> No interpreation requires, since parsing failed. Then you can start > >>> dealing with these exceptions. _Do not_ write unparsable messages into > >>> an mbox! > >>> > >> Maybe I shouldn't but Python 2 has been managing to do

Re: Aw: Re: Python 3 how to convert a list of bytes objects to a list of strings?

2020-08-28 Thread Chris Angelico
On Fri, Aug 28, 2020 at 10:32 PM Richard Damon wrote: > > This might be one of the cases where Python 2's lack handling of string > vs bytes was an advantage. > > If he was just scanning the message for specific ASCII strings, then not > getting the full message decoded write is unlikely to have b

Re: Aw: Re: Python 3 how to convert a list of bytes objects to a list of strings?

2020-08-28 Thread Richard Damon
On 8/28/20 7:50 AM, Karsten Hilbert wrote: >>> No interpreation requires, since parsing failed. Then you can start >>> dealing with these exceptions. _Do not_ write unparsable messages into >>> an mbox! >>> >> Maybe I shouldn't but Python 2 has been managing to do so for several >> years without an

Re: Aw: Re: Python 3 how to convert a list of bytes objects to a list of strings?

2020-08-28 Thread Richard Damon
On 8/28/20 7:50 AM, Karsten Hilbert wrote: >>> No interpreation requires, since parsing failed. Then you can start >>> dealing with these exceptions. _Do not_ write unparsable messages into >>> an mbox! >>> >> Maybe I shouldn't but Python 2 has been managing to do so for several >> years without an

Aw: Re: Python 3 how to convert a list of bytes objects to a list of strings?

2020-08-28 Thread Karsten Hilbert
> > No interpreation requires, since parsing failed. Then you can start > > dealing with these exceptions. _Do not_ write unparsable messages into > > an mbox! > > > Maybe I shouldn't but Python 2 has been managing to do so for several > years without any issues. I am inclined to congratulate you

Re: Python 3 how to convert a list of bytes objects to a list of strings?

2020-08-28 Thread Chris Green
Cameron Simpson wrote: > On 28Aug2020 08:56, Chris Green wrote: > >Stefan Ram wrote: > >> Chris Angelico writes: > >> >But this is a really good job for a list comprehension: > >> >sss = [str(word) for word in bbb] > >> > >> Are you all sure that "str" is really what you all want? > >> > >Not

Re: Python 3 how to convert a list of bytes objects to a list of strings?

2020-08-28 Thread Cameron Simpson
On 28Aug2020 08:56, Chris Green wrote: >Stefan Ram wrote: >> Chris Angelico writes: >> >But this is a really good job for a list comprehension: >> >sss = [str(word) for word in bbb] >> >> Are you all sure that "str" is really what you all want? >> >Not absolutely, you no doubt have been follow

Re: Python 3 how to convert a list of bytes objects to a list of strings?

2020-08-28 Thread Chris Green
Cameron Simpson wrote: > On 27Aug2020 23:54, Marco Sulla wrote: > >Are you sure you want `str()`? > > > str(b'aaa') > >"b'aaa'" > > > >Probably you want: > > > >map(lambda x: x.decode(), bbb) > > _And_ you need to know the encoding of the text in the bytes. The above > _assumes_ UTF-8 beca

Re: Python 3 how to convert a list of bytes objects to a list of strings?

2020-08-28 Thread Chris Green
Chris Angelico wrote: > On Fri, Aug 28, 2020 at 6:36 AM Chris Green wrote: > > > > This sounds quite an easy thing to do but I can't find how to do it > > elegantly. > > > > I have a list of bytes class objects (i.e. a list containing sequences > > of bytes, which are basically text) and I want t

Re: Python 3 how to convert a list of bytes objects to a list of strings?

2020-08-28 Thread Chris Green
Stefan Ram wrote: > Chris Angelico writes: > >But this is a really good job for a list comprehension: > >sss = [str(word) for word in bbb] > > Are you all sure that "str" is really what you all want? > Not absolutely, you no doubt have been following other threads related to this one. :-)

Aw: Re: Python 3 how to convert a list of bytes objects to a list of strings?

2020-08-28 Thread Karsten Hilbert
> >Are you sure you want `str()`? > > > str(b'aaa') > >"b'aaa'" > > > >Probably you want: > > > >map(lambda x: x.decode(), bbb) > > _And_ you need to know the encoding of the text in the bytes. The above > _assumes_ UTF-8 because that is the default for bytes.decode, and if > that is _not_ wha

Re: Python 3 how to convert a list of bytes objects to a list of strings?

2020-08-27 Thread Cameron Simpson
On 27Aug2020 23:54, Marco Sulla wrote: >Are you sure you want `str()`? > str(b'aaa') >"b'aaa'" > >Probably you want: > >map(lambda x: x.decode(), bbb) _And_ you need to know the encoding of the text in the bytes. The above _assumes_ UTF-8 because that is the default for bytes.decode, and if

Re: Python 3 how to convert a list of bytes objects to a list of strings?

2020-08-27 Thread Marco Sulla
Are you sure you want `str()`? >>> str(b'aaa') "b'aaa'" Probably you want: map(lambda x: x.decode(), bbb) -- https://mail.python.org/mailman/listinfo/python-list

Re: Python 3 how to convert a list of bytes objects to a list of strings?

2020-08-27 Thread Chris Angelico
On Fri, Aug 28, 2020 at 6:36 AM Chris Green wrote: > > This sounds quite an easy thing to do but I can't find how to do it > elegantly. > > I have a list of bytes class objects (i.e. a list containing sequences > of bytes, which are basically text) and I want to convert it to a list > of string ob

Python 3 how to convert a list of bytes objects to a list of strings?

2020-08-27 Thread Chris Green
This sounds quite an easy thing to do but I can't find how to do it elegantly. I have a list of bytes class objects (i.e. a list containing sequences of bytes, which are basically text) and I want to convert it to a list of string objects. One of the difficulties of finding out how to do this is

Re: filter a list of strings

2015-12-09 Thread Sivan Greenberg
That might also work: new_list = [i for i in the_list if i not in targets] # given you have no special requirements for the selection # out of 'targets' -Sivan On Wed, Dec 9, 2015 at 12:58 AM, Thomas 'PointedEars' Lahn < pointede...@web.de> wrote: > Mark Lawrence wrote: > > > On 03/12/2015 01:1

Shadowing built-ins [was Re: filter a list of strings]

2015-12-08 Thread Steven D'Aprano
On Wednesday 09 December 2015 09:58, Thomas 'PointedEars' Lahn wrote: > Mark Lawrence wrote: > >> On 03/12/2015 01:15, c.bu...@posteo.jp wrote: >>> I would like to know how this could be done more elegant/pythonic. >>> >>> I have a big list (over 10.000 items) with strings (each 100 to 300 >>> ch

Re: filter a list of strings

2015-12-08 Thread Thomas 'PointedEars' Lahn
Mark Lawrence wrote: > On 03/12/2015 01:15, c.bu...@posteo.jp wrote: >> I would like to know how this could be done more elegant/pythonic. >> >> I have a big list (over 10.000 items) with strings (each 100 to 300 >> chars long) and want to filter them. >> >> list = . >> […] > > targets = ['Ba

Re: filter a list of strings

2015-12-05 Thread Peter Pearson
On Thu, 3 Dec 2015 10:27:19 +0100, wrote: [snip] > I often saw constructions like this > x for x in y if ... > But I don't understand that combination of the Python keywords (for, > in, if) I allready know. It is to complex to imagine what there really > happen. Don't give up! List comprehensi

Re: filter a list of strings

2015-12-03 Thread Terry Reedy
On 12/3/2015 7:28 AM, Mark Lawrence wrote: On 03/12/2015 01:15, c.bu...@posteo.jp wrote: I would like to know how this could be done more elegant/pythonic. I have a big list (over 10.000 items) with strings (each 100 to 300 chars long) and want to filter them. list = . for item in list[:]

Re: filter a list of strings

2015-12-03 Thread Jussi Piitulainen
Denis McMahon writes: > On Thu, 03 Dec 2015 08:32:49 +0200, Jussi Piitulainen wrote: > >> def isbad(item): >> return ( 'Banana' in item or >> 'Car' in item ) >> >> def isgood(item) >> return not isbad(item) > > badthings = [ 'Banana', 'Car', ] > > def isgood(item) >

Re: filter a list of strings

2015-12-03 Thread Denis McMahon
On Thu, 03 Dec 2015 08:32:49 +0200, Jussi Piitulainen wrote: > def isbad(item): > return ( 'Banana' in item or > 'Car' in item ) > > def isgood(item) > return not isbad(item) badthings = [ 'Banana', 'Car', ] def isgood(item) for thing in badthings: if thi

Re: filter a list of strings

2015-12-03 Thread Mark Lawrence
On 03/12/2015 01:15, c.bu...@posteo.jp wrote: I would like to know how this could be done more elegant/pythonic. I have a big list (over 10.000 items) with strings (each 100 to 300 chars long) and want to filter them. list = . for item in list[:]: if 'Banana' in item: list.remove(

Re: filter a list of strings

2015-12-03 Thread Grobu
On 03/12/15 02:15, c.bu...@posteo.jp wrote: I would like to know how this could be done more elegant/pythonic. I have a big list (over 10.000 items) with strings (each 100 to 300 chars long) and want to filter them. list = . for item in list[:]: if 'Banana' in item: list.remove(it

Re: filter a list of strings

2015-12-03 Thread Jussi Piitulainen
writes: > Thank you for your suggestion. This will help a lot. > > On 2015-12-03 08:32 Jussi Piitulainen wrote: >> list = [ item for item in list >> if ( 'Banana' not in item and >> 'Car' not in item ) ] > > I often saw constructions like this > x for x in y if ... > But

Re: filter a list of strings

2015-12-03 Thread Peter Otten
Laura Creighton wrote: > In a message of Thu, 03 Dec 2015 10:27:19 +0100, c.bu...@posteo.jp writes: >>Thank you for your suggestion. This will help a lot. >> >>On 2015-12-03 08:32 Jussi Piitulainen wrote: >>> list = [ item for item in list >>> if ( 'Banana' not in item and >>>

Re: filter a list of strings

2015-12-03 Thread jmp
On 12/03/2015 10:27 AM, c.bu...@posteo.jp wrote: I often saw constructions like this x for x in y if ... But I don't understand that combination of the Python keywords (for, in, if) I allready know. It is to complex to imagine what there really happen. I understand this for x in y: if

Re: filter a list of strings

2015-12-03 Thread Laura Creighton
In a message of Thu, 03 Dec 2015 10:27:19 +0100, c.bu...@posteo.jp writes: >Thank you for your suggestion. This will help a lot. > >On 2015-12-03 08:32 Jussi Piitulainen wrote: >> list = [ item for item in list >> if ( 'Banana' not in item and >> 'Car' not in item ) ] > >I o

Re: filter a list of strings

2015-12-03 Thread Wolfgang Maier
On 03.12.2015 10:27, c.bu...@posteo.jp wrote: > > I often saw constructions like this >x for x in y if ... > But I don't understand that combination of the Python keywords (for, > in, if) I allready know. It is to complex to imagine what there really > happen. > > I understand this >for x

Re: filter a list of strings

2015-12-03 Thread Chris Angelico
On Thu, Dec 3, 2015 at 8:27 PM, wrote: > Thank you for your suggestion. This will help a lot. > > On 2015-12-03 08:32 Jussi Piitulainen wrote: >> list = [ item for item in list >> if ( 'Banana' not in item and >> 'Car' not in item ) ] > > I often saw constructions like thi

Re: filter a list of strings

2015-12-03 Thread c.buhtz
Thank you for your suggestion. This will help a lot. On 2015-12-03 08:32 Jussi Piitulainen wrote: > list = [ item for item in list > if ( 'Banana' not in item and > 'Car' not in item ) ] I often saw constructions like this x for x in y if ... But I don't understand that

Re: filter a list of strings

2015-12-02 Thread Jussi Piitulainen
writes: > I would like to know how this could be done more elegant/pythonic. > > I have a big list (over 10.000 items) with strings (each 100 to 300 > chars long) and want to filter them. > > list = . > > for item in list[:]: > if 'Banana' in item: > list.remove(item) > if 'Car' in i

filter a list of strings

2015-12-02 Thread c.buhtz
I would like to know how this could be done more elegant/pythonic. I have a big list (over 10.000 items) with strings (each 100 to 300 chars long) and want to filter them. list = . for item in list[:]: if 'Banana' in item: list.remove(item) if 'Car' in item: list.remove(item)

Re: Searching for a list of strings in a file with Python

2013-10-14 Thread Denis McMahon
On Sun, 13 Oct 2013 22:34:43 -0700, Starriol wrote: > I'm trying to search for several strings, which I have in a .txt file > line by line, on another file. > So the idea is, take input.txt and search for each line in that file in > another file, let's call it rules.txt. > > So far, I've been abl

Re: Searching for a list of strings in a file with Python

2013-10-14 Thread Dave Angel
On 14/10/2013 01:34, Starriol wrote: > Hi guys, > > I'm trying to search for several strings, which I have in a .txt file line by > line, on another file. > So the idea is, take input.txt and search for each line in that file in > another file, let's call it rules.txt. > > So far, I've been able

Re: Searching for a list of strings in a file with Python

2013-10-13 Thread Peter Otten
Starriol wrote: > Hi guys, > > I'm trying to search for several strings, which I have in a .txt file line > by line, on another file. So the idea is, take input.txt and search for > each line in that file in another file, let's call it rules.txt. > > So far, I've been able to do this, to search

Searching for a list of strings in a file with Python

2013-10-13 Thread Starriol
Hi guys, I'm trying to search for several strings, which I have in a .txt file line by line, on another file. So the idea is, take input.txt and search for each line in that file in another file, let's call it rules.txt. So far, I've been able to do this, to search for individual strings: [cod

Re: Converting a list of strings into a list of integers?

2012-07-23 Thread rusi
On Jul 23, 7:27 pm, Grant Edwards wrote: > That said, "map" seems to be frowned upon by the Python community for > reasons I've never really understood,... Maybe the analogy: comprehension : map:: relational calculus : relational algebra In particular map, filter correspond to project and

Re: Converting a list of strings into a list of integers?

2012-07-23 Thread Grant Edwards
On 2012-07-22, Jan Riechers wrote: > I am not sure why everyone is using the for-iterator option over a > "map", but I would do it like that: > > MODUS_LIST= map(int, options.modus_list) > > "map" works on a list and does commandX (here "int" conversion, use > "str" for string.. et cetera) on s

Re: Converting a list of strings into a list of integers?

2012-07-22 Thread Dave Angel
On 07/22/2012 11:29 AM, Tony the Tiger wrote: > Hi, > Is there such a thing in the language, or do I have to invent it myself? > > I came up with the following: > > # options.modus_list contains, e.g., "[2,3,4]" > # (a string from the command line) > > &

Re: Converting a list of strings into a list of integers?

2012-07-22 Thread Jan Riechers
On 22.07.2012 20:01, Steven D'Aprano wrote: [SNIP] map is faster than an ordinary for-loop if the function you are applying is a builtin like int, str, etc. But if you have to write your own pure- Python function, the overhead of calling a function negates the advantage of map, which is no faster

Re: Converting a list of strings into a list of integers?

2012-07-22 Thread Ian Kelly
On Sun, Jul 22, 2012 at 10:20 AM, Jan Riechers wrote: > Hi, > > I am not sure why everyone is using the for-iterator option over a "map", > but I would do it like that: > > MODUS_LIST= map(int, options.modus_list) > > "map" works on a list and does commandX (here "int" conversion, use "str" > for

Re: Converting a list of strings into a list of integers?

2012-07-22 Thread Jan Riechers
On 22.07.2012 20:03, David Robinow wrote: On Sun, Jul 22, 2012 at 12:20 PM, Jan Riechers wrote: On 22.07.2012 18:39, Alister wrote: looks like a classic list comprehension to me and can be achieved in a single line MODUS_LIST=[int(x) for x in options.modus_list] Hi, I am not sure why everyon

Re: Converting a list of strings into a list of integers?

2012-07-22 Thread Steven D'Aprano
On Sun, 22 Jul 2012 19:20:18 +0300, Jan Riechers wrote: > "map" works on a list and does commandX (here "int" conversion, use > "str" for string.. et cetera) on sequenceY, returning a sequence. More > in the help file. > > And if I'm not completely mistaken, it's also the quicker way to do > perf

Re: Converting a list of strings into a list of integers?

2012-07-22 Thread Paul Rubin
Tony the Tiger writes: > # options.modus_list contains, e.g., "[2,3,4]" Try this: import ast MODUS_LIST = ast.literal_eval(options.modus_list) literal_eval is like eval except it can only evaluate literals rather than calling functions and the like. The idea is you can use it on untrus

Re: Converting a list of strings into a list of integers?

2012-07-22 Thread David Robinow
On Sun, Jul 22, 2012 at 12:20 PM, Jan Riechers wrote: > On 22.07.2012 18:39, Alister wrote: >> looks like a classic list comprehension to me and can be achieved in a >> single line >> MODUS_LIST=[int(x) for x in options.modus_list] > Hi, > > I am not sure why everyone is using the for-iterator opt

Re: Converting a list of strings into a list of integers?

2012-07-22 Thread Peter Otten
Tony the Tiger wrote: > On Sun, 22 Jul 2012 11:39:30 -0400, Roy Smith wrote: > >> To answer the question you asked, to convert a list of strings to a list >> of ints, you want to do something like: >> >> MODUS_LIST = [int(i) for i in options.modus_list] > >

Re: Converting a list of strings into a list of integers?

2012-07-22 Thread Jan Riechers
On 22.07.2012 18:39, Alister wrote: On Sun, 22 Jul 2012 10:29:44 -0500, Tony the Tiger wrote: I came up with the following: # options.modus_list contains, e.g., "[2,3,4]" # (a string from the command line) # MODUS_LIST contains, e.g., [2,4,8,16] # (i.e., a list of integers) if

Re: Converting a list of strings into a list of integers?

2012-07-22 Thread Alister
On Sun, 22 Jul 2012 10:29:44 -0500, Tony the Tiger wrote: > Hi, > Is there such a thing in the language, or do I have to invent it myself? > > I came up with the following: > > # options.modus_list contains, e.g., "[2,3,4]" > # (a string from the command line) > # MODUS_LIST contains, e.g.,

Re: Converting a list of strings into a list of integers?

2012-07-22 Thread Roy Smith
m)) > MODUS_LIST = intTmp To answer the question you asked, to convert a list of strings to a list of ints, you want to do something like: MODUS_LIST = [int(i) for i in options.modus_list] But, to answer the question you didn't ask, if you're trying to parse command-l

Re: How to convert a list of strings into a list of variables

2011-08-19 Thread noydb
Thanks to all for your responses! Good lessons. I implemented something like what Jerry Hill suggested (dictionary), which works well for my purposes. The list of strings that is being passed into this code is also provided by something I wrote so I do trust what is being sent. Might use what

Re: How to convert a list of strings into a list of variables

2011-08-19 Thread Roy Smith
In article <2ab25f69-6017-42a6-a7ef-c71bc2ee8...@l2g2000vbn.googlegroups.com>, noydb wrote: > How would you convert a list of strings into a list of variables using > the same name of the strings? > > So, ["red", "one", "maple"] into [red, one

Re: How to convert a list of strings into a list of variables

2011-08-19 Thread Kingsley Adio
noydb wrote: > How would you convert a list of strings into a list of variables using > the same name of the strings? > > So, ["red", "one", "maple"] into [red, one, maple] > > Thanks for any help! red="a string" one="another stri

Re: How to convert a list of strings into a list of variables

2011-08-18 Thread Steven D'Aprano
Chris Angelico wrote: > On Thu, Aug 18, 2011 at 5:09 PM, John Gordon wrote: >> for x in list_of_strings: >> list_of_variables.append(eval(x)) >> > > If this really is what you need, you can simplify it by using the > globals() dictionary - it's a regular dictionary whose contents are > all the g

Re: How to convert a list of strings into a list of variables

2011-08-18 Thread AB
Hi, If the «variables» are named attributes you can use getattr. # class colors: red=1 green=2 blue=3 c=colors() a=['red','green','blue'] for v in a: print v,getattr(c,v) #--- AB -- http://mail.python.org/mailman/listinfo/pytho

Re: How to convert a list of strings into a list of variables

2011-08-18 Thread Nobody
On Thu, 18 Aug 2011 16:09:43 +, John Gordon wrote: >> How would you convert a list of strings into a list of variables using >> the same name of the strings? > >> So, ["red", "one", "maple"] into [red, one, maple] > > If the strings

Re: How to convert a list of strings into a list of variables

2011-08-18 Thread Chris Angelico
On Thu, Aug 18, 2011 at 5:09 PM, John Gordon wrote: > for x in list_of_strings: >    list_of_variables.append(eval(x)) > If this really is what you need, you can simplify it by using the globals() dictionary - it's a regular dictionary whose contents are all the global variables in your current m

Re: How to convert a list of strings into a list of variables

2011-08-18 Thread John Gordon
In <2ab25f69-6017-42a6-a7ef-c71bc2ee8...@l2g2000vbn.googlegroups.com> noydb writes: > How would you convert a list of strings into a list of variables using > the same name of the strings? > So, ["red", "one", "maple"] into [red, one, maple] &

Re: How to convert a list of strings into a list of variables

2011-08-18 Thread noydb
On Aug 18, 11:29 am, Jerry Hill wrote: > On Thu, Aug 18, 2011 at 11:19 AM, noydb wrote: > > I am being passed the list of strings.  I have variables set up > > already pointing to files.  I need to loop through each variable in > > the list and do things to the files.  The

Re: How to convert a list of strings into a list of variables

2011-08-18 Thread Jerry Hill
On Thu, Aug 18, 2011 at 11:19 AM, noydb wrote: > I am being passed the list of strings.  I have variables set up > already pointing to files.  I need to loop through each variable in > the list and do things to the files.  The list of strings will change > each time, include up to 22

Re: How to convert a list of strings into a list of variables

2011-08-18 Thread noydb
On Aug 18, 11:12 am, David Robinow wrote: > On Thu, Aug 18, 2011 at 10:57 AM, noydb wrote: > > How would you convert a list of strings into a list of variables using > > the same name of the strings? > > > So, ["red", "one", "maple"] int

Re: How to convert a list of strings into a list of variables

2011-08-18 Thread David Robinow
On Thu, Aug 18, 2011 at 10:57 AM, noydb wrote: > How would you convert a list of strings into a list of variables using > the same name of the strings? > > So, ["red", "one", "maple"] into [red, one, maple] Why would you want to? -- http://mail.python.org/mailman/listinfo/python-list

How to convert a list of strings into a list of variables

2011-08-18 Thread noydb
How would you convert a list of strings into a list of variables using the same name of the strings? So, ["red", "one", "maple"] into [red, one, maple] Thanks for any help! -- http://mail.python.org/mailman/listinfo/python-list

Re: Memory issues when storing as List of Strings vs List of List

2010-11-30 Thread Ben Finney
OW Ghim Siong writes: > I have a big file 1.5GB in size, with about 6 million lines of > tab-delimited data. I have to perform some filtration on the data and > keep the good data. After filtration, I have about 5.5 million data > left remaining. As you might already guessed, I have to read them

Re: Memory issues when storing as List of Strings vs List of List

2010-11-30 Thread Antoine Pitrou
On Tue, 30 Nov 2010 18:29:35 +0800 OW Ghim Siong wrote: > > Does anyone know why is there such a big difference memory usage when > storing the matrix as a list of list, and when storing it as a list of > string? That's because any object has a fixed overhead (related to metadata and allocatio

Re: Memory issues when storing as List of Strings vs List of List

2010-11-30 Thread Tim Chase
On 11/30/2010 04:29 AM, OW Ghim Siong wrote: a=open("bigfile") matrix=[] while True: lines = a.readlines(1) for line in lines: data=line.split("\t") if several_conditions_are_satisfied: matrix.append(data) print "Number of lines read:", len(li

Re: Memory issues when storing as List of Strings vs List of List

2010-11-30 Thread Peter Otten
OW Ghim Siong wrote: > Hi all, > > I have a big file 1.5GB in size, with about 6 million lines of > tab-delimited data. I have to perform some filtration on the data and > keep the good data. After filtration, I have about 5.5 million data left > remaining. As you might already guessed, I have to

Re: Memory issues when storing as List of Strings vs List of List

2010-11-30 Thread Ulrich Eckhardt
OW Ghim Siong wrote: > I have a big file 1.5GB in size, with about 6 million lines of > tab-delimited data. How many fields are there an each line? > I have to perform some filtration on the data and > keep the good data. After filtration, I have about 5.5 million data left > remaining. As you m

Memory issues when storing as List of Strings vs List of List

2010-11-30 Thread OW Ghim Siong
Hi all, I have a big file 1.5GB in size, with about 6 million lines of tab-delimited data. I have to perform some filtration on the data and keep the good data. After filtration, I have about 5.5 million data left remaining. As you might already guessed, I have to read them in batches and I d

Re: Fastest way to detect a non-ASCII character in a list of strings.

2010-10-29 Thread Stefan Behnel
Dun Peal, 28.10.2010 09:10: I find myself surprised at the relatively little use that Cython is seeing. I don't think it's being used that little. It just doesn't show that easily. We get a lot of feedback on the mailing list that suggests that it's actually used by all sorts of people in all

Re: Fastest way to detect a non-ASCII character in a list of strings.

2010-10-28 Thread Dun Peal
On Wed, Oct 20, 2010 at 6:52 AM, Stefan Behnel wrote: > Well, the estimate is about one man-month, so it would be doable in about > three months time if we had the money to work on it. So far, no one has made > a serious offer to support that project, though. I find myself surprised at the relati

Re: Fastest way to detect a non-ASCII character in a list of strings.

2010-10-19 Thread Stefan Behnel
Dun Peal, 20.10.2010 02:07: On Mon, Oct 18, 2010 at 1:41 AM, Stefan Behnel wrote: Or, a bit shorter, using Cython 0.13: def only_allowed_characters(list strings): cdef unicode s return any((c< 31 or c> 127) for s in strings for c in s) Very cool, this

Re: Fastest way to detect a non-ASCII character in a list of strings.

2010-10-19 Thread Dun Peal
On Mon, Oct 18, 2010 at 1:41 AM, Stefan Behnel wrote: > Or, a bit shorter, using Cython 0.13: > >    def only_allowed_characters(list strings): >        cdef unicode s >        return any((c < 31 or c > 127) >                   for s in strings for c in s) Very cool, this caused me to look up the

Re: Fastest way to detect a non-ASCII character in a list of strings.

2010-10-18 Thread Felipe Bastos Nunes
Printable in the screen, all of them are, except for blank spaces ehhehehe 2010/10/18, Tim Chase : > On 10/18/10 09:28, Grant Edwards wrote: >> There's no easy way to even define what "printable" means. Ask three >> different people, and you'll get at least four different answers >> answers. > >

Re: Fastest way to detect a non-ASCII character in a list of strings.

2010-10-18 Thread Tim Chase
On 10/18/10 09:28, Grant Edwards wrote: There's no easy way to even define what "printable" means. Ask three different people, and you'll get at least four different answers answers. I don't have a printer...that makes *all* characters unprintable, right? Now I can convert the algorithm to O

Re: Fastest way to detect a non-ASCII character in a list of strings.

2010-10-18 Thread Grant Edwards
On 2010-10-18, Steven D'Aprano wrote: > Neither is accurate. all_ascii would be: > > all(ord(c) <= 127 for c in string for string in L) Definitely. > all_printable would be considerably harder. As far as I can tell, there's > no simple way to tell if a character is printable. There's no easy

Re: Fastest way to detect a non-ASCII character in a list of strings.

2010-10-17 Thread Stefan Behnel
Dun Peal, 17.10.2010 21:59: `all_ascii(L)` is a function that accepts a list of strings L, and returns True if all of those strings contain only ASCII chars, False otherwise. What's the fastest way to implement `all_ascii(L)`? My ideas so far are: 1. Match against a regexp with a char

Re: Fastest way to detect a non-ASCII character in a list of strings.

2010-10-17 Thread Albert Hopkins
On Sun, 2010-10-17 at 14:59 -0500, Dun Peal wrote: > `all_ascii(L)` is a function that accepts a list of strings L, and > returns True if all of those strings contain only ASCII chars, False > otherwise. > > What's the fastest way to implement `all_ascii(L)`? > > M

Re: Fastest way to detect a non-ASCII character in a list of strings.

2010-10-17 Thread Steven D'Aprano
On Mon, 18 Oct 2010 01:04:09 +0100, Rhodri James wrote: > On Sun, 17 Oct 2010 20:59:22 +0100, Dun Peal > wrote: > >> `all_ascii(L)` is a function that accepts a list of strings L, and >> returns True if all of those strings contain only ASCII chars, False >> otherwis

  1   2   3   >