string->list->string 
Author Message
 string->list->string

So. I forget how to do this AGAIN. I know how to convert a string to a list:

mylist = str ("alpha")

but I don't remember the shorthand for converting it back?

On another note, is there a module somewhere that knows how to
strip an entire set of user-specified characters from a string, ala:

"a!b%c^".stripchars("!%^")

"abc"

???

C//



Sat, 11 Oct 2003 08:03:27 GMT  
 string->list->string

Quote:
>>> x = [1,2,3]
>>> str(x)
'[1, 2, 3]'
>>> eval(str(x))
[1, 2, 3]
>>> s = "a!b%c^"
>>> import string
>>> t = string.maketrans(s,s)
>>> s.translate(t, "!%^")
'abc'

-- bjorn
Quote:
-----Original Message-----

Sent: Monday, April 23, 2001 6:03 PM

Subject: string->list->string

So. I forget how to do this AGAIN. I know how to convert a string to a list:

mylist = str ("alpha")

but I don't remember the shorthand for converting it back?

On another note, is there a module somewhere that knows how to
strip an entire set of user-specified characters from a string, ala:

"a!b%c^".stripchars("!%^")

"abc"

???

C//

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



Sat, 11 Oct 2003 08:36:48 GMT  
 string->list->string

Quote:

>>>> x = [1,2,3]
>>>> str(x)
>'[1, 2, 3]'
>>>> eval(str(x))
>[1, 2, 3]

Not getting it: I want

s = "abc"
l = list(s)
<something>(l)
"abc"

Where something is some expression. The worst part here is that sometime in
the last year, I've asked this very same question before. *IRK* Is this in the FAQ?
It outta be.

Quote:
>>>> s = "a!b%c^"
>>>> import string
>>>> t = string.maketrans(s,s)
>>>> s.translate(t, "!%^")
>'abc'

Hrm. I read the documentation for string.maketrans() and string.translate() just now.
I don't get it. The documentation doesn't make this clear, and I don't understand
from your example what's going on. While I can see that it works, I don't get the
basic principle. I would think a maketrans of s,s would just translate one character
to the same character.

???

C//



Sat, 11 Oct 2003 09:35:38 GMT  
 string->list->string

Quote:

> Not getting it: I want
> s = "abc"
> l = list(s)
> <something>(l)
> "abc"

import string
string.join(l, "")

|>oug



Sat, 11 Oct 2003 09:46:56 GMT  
 string->list->string
Or, in Py2.0, the rather uncute:

"".join(l)

:)

--Stephen
(replace 'NOSPAM' with 'seraph' to respond in email)


Quote:

> > Not getting it: I want

> > s = "abc"
> > l = list(s)
> > <something>(l)
> > "abc"

> import string
> string.join(l, "")

> |>oug



Sat, 11 Oct 2003 12:48:02 GMT  
 string->list->string

Quote:

> So. I forget how to do this AGAIN. I know how to convert a string to a list:

> mylist = str ("alpha")

This is still a string.  Try:

alist = map(None, "alpha")

To convert it back:

''.join(alist)

Quote:

> but I don't remember the shorthand for converting it back?

> On another note, is there a module somewhere that knows how to
> strip an entire set of user-specified characters from a string, ala:

> "a!b%c^".stripchars("!%^")

import re
c = re.compile("[!%^]")
c.sub('', "a!b%c^")

--
Jeffery D. Collins, Ph.D.
Sr. Software Developer
Endeavors Technology, Inc.
http://www.endeavors.com



Sat, 11 Oct 2003 12:35:45 GMT  
 string->list->string

Quote:


> >>>> x = [1,2,3]
> >>>> str(x)
> >'[1, 2, 3]'
> >>>> eval(str(x))
> >[1, 2, 3]

> Not getting it: I want

> s = "abc"
> l = list(s)
> <something>(l)
> "abc"

Well, perhaps if your original enquiry hadn't said

"""
So. I forget how to do this AGAIN. I know how to convert a string to a list:

mylist = str ("alpha")

but I don't remember the shorthand for converting it back?
"""

we might have understood that you really meant mylist=list(alpha).

The collective brainpower of c.l.py is indeed impressive, and I have seen
the smarter members pull off some pretty amazing feats of psychic response
("the answer to the question you *really* meant to ask is ...").

That doesn't mean you can expect it all the time!

Quote:
> Where something is some expression. The worst part here is that sometime
in
> the last year, I've asked this very same question before. *IRK* Is this in
the FAQ?
> It outta be.

How embarrassing!  Good for you for confessing ;-) Good for the soul, doncha
know...

Quote:
> >>>> s = "a!b%c^"
> >>>> import string
> >>>> t = string.maketrans(s,s)
> >>>> s.translate(t, "!%^")
> >'abc'

> Hrm. I read the documentation for string.maketrans() and

string.translate() just now.
Quote:
> I don't get it. The documentation doesn't make this clear, and I don't
understand
> from your example what's going on. While I can see that it works, I don't
get the
> basic principle. I would think a maketrans of s,s would just translate one
character
> to the same character.

Part of your cinfusion could be that you are reading the wrong
documentation. The translate used above is the translate method of the
string type, not the translate function of the string module. Although the
reponse could have made that clearer, the clue is that the answer used
string.maketrans() but s.translate().

Given this new information, does the example make sense now you understand
that the "!%^" argument is specifying delete characters (you do understand
that now, right)?

Quote:
> ???

!!!

we're-only-human-(except-the-bots)-you-know-ly y'rs  - steve



Sat, 11 Oct 2003 13:21:40 GMT  
 string->list->string

Quote:
>we might have understood that you really meant mylist=list(alpha).

The wonderful thing about usenet is that you're all but guaranteed to
be taunted for any idiocy. :)

Quote:
>Part of your cinfusion could be that you are reading the wrong
>documentation. The translate used above is the translate method of the
>string type, not the translate function of the string module

Oh. oh. oh. Yeah. ARG.

Someone also showed me a nice trick with the re module.

C//



Sun, 12 Oct 2003 06:46:24 GMT  
 string->list->string

Quote:

> >we might have understood that you really meant mylist=list(alpha).

> The wonderful thing about usenet is that you're all but guaranteed to
> be taunted for any idiocy. :)

Yeah, well I suppose in that case I'm only passing on what I've justifiably
been the recipient of in the past...

Quote:
> >Part of your cinfusion could be that you are reading the wrong
> >documentation. The translate used above is the translate method of the
> >string type, not the translate function of the string module

> Oh. oh. oh. Yeah. ARG.

> Someone also showed me a nice trick with the re module.

Gladja-gottit-solved-ly y'rs  - stevE


Mon, 13 Oct 2003 03:32:56 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. question about symbol->string, string->list

2. MIT scheme: string->list on long strings

3. Output to strings / F>STRING

4. Proposal: S>UPPER etc /was: Standardising string primitives (was: Re: String)

5. Bugs in ST80 4.1 (Stream>>through, List>>copyFrom:to:)

6. External interfacing to varargs, and String>>sprintfWithArguments:

7. Bug on String>>replaceAll:with:

8. Is String>>sprintfWith: broken?

9. String>>sprintfWith:with:

10. String>>withNormalizedLineDelimiters

11. String>>sprintfWith: with float argument

12. String>>withNormalizedLineDelimiters

 

 
Powered by phpBB® Forum Software