By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,818 Members | 1,262 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,818 IT Pros & Developers. It's quick & easy.

Beginner question: Converting Single-Element tuples to list

P: n/a
Hello everyone,

I want to convert a tuple to a list, and I expected this behavior:

list(('abc','def')) -> ['abc','def']
list(('abc')) -> ['abc']

But Python gave me this behavior:

list(('abc','def')) -> ['abc','def']
list(('abc')) -> ['a','b','c']

How do I do get Python to work like the in former case?

Many thanks!

David

Jul 19 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
('abc') is not a tuple - this is an unfortunate result of using ()'s as
expression grouping *and* as tuple delimiters. To make ('abc') a
tuple, you must add an extra comma, as ('abc',).
list( ('abc',) )

['abc']

-- Paul

Jul 19 '05 #2

P: n/a
Hi,

Thanks for your reply! A new thing learned....

Allow me to follow that up with another question:

Let's say I have a result from a module called pyparsing:

Results1 = ['abc', 'def']
Results2 = ['abc']

They are of the ParseResults type:
type(Results1) <class 'pyparsing.ParseResults'> type(Results2)

<class 'pyparsing.ParseResults'>

I want to convert them into Python lists. list() will work fine on
Results1, but on Results2, it will return:

['a', 'b', 'c']

Because 'abc' is a string. But I want it to return ['abc'].

In short, my question is: how do I typecast an arbitrary object,
whether a list-like object or a string, into a Python list without
having Python split my strings into characters?

Thanks very much for your help!

David

p.s. I know pyparsing has an asList() method, but let's just say I am
unable to use it in my circumstances.

Jul 19 '05 #3

P: n/a
vd********@gmail.com wrote:
Hi,

Thanks for your reply! A new thing learned....

Allow me to follow that up with another question:

Let's say I have a result from a module called pyparsing:

Results1 = ['abc', 'def']
Results2 = ['abc']

They are of the ParseResults type:
type(Results1) <class 'pyparsing.ParseResults'> type(Results2)

<class 'pyparsing.ParseResults'>

I want to convert them into Python lists. list() will work fine on
Results1, but on Results2, it will return:

['a', 'b', 'c']

Because 'abc' is a string. But I want it to return ['abc'].

In short, my question is: how do I typecast an arbitrary object,
whether a list-like object or a string, into a Python list without
having Python split my strings into characters?


This seems like a glitch in pyparsing. If a ParseResults class emulates
a list, it should do so every time, not only if there is more than one
value in it.

Reinhold
Jul 19 '05 #4

P: n/a
"Reinhold Birkenfeld" <re************************@wolke7.net> wrote in
message news:3i************@individual.net...
vd********@gmail.com wrote:
Hi,

Thanks for your reply! A new thing learned....

Allow me to follow that up with another question:

Let's say I have a result from a module called pyparsing:

Results1 = ['abc', 'def']
Results2 = ['abc']

They are of the ParseResults type:
> type(Results1) <class 'pyparsing.ParseResults'>
> type(Results2)

<class 'pyparsing.ParseResults'>

I want to convert them into Python lists. list() will work fine on
Results1, but on Results2, it will return:

['a', 'b', 'c']

Because 'abc' is a string. But I want it to return ['abc'].

In short, my question is: how do I typecast an arbitrary object,
whether a list-like object or a string, into a Python list without
having Python split my strings into characters?


This seems like a glitch in pyparsing. If a ParseResults class emulates
a list, it should do so every time, not only if there is more than one
value in it.


Unfortunately, I've seen that behavior a number of times:
no output is None, one output is the object, more than one
is a list of objects. That forces you to have checks for None
and list types all over the place. Granted, sometimes your
program logic would need checks anyway, but frequently
just returning a possibly empty list would save the caller
a lot of grief.

John Roth
Reinhold


Jul 19 '05 #5

P: n/a
David -

I'm not getting the same results. Run this test program:
---------------
import pyparsing as pp
import sys

def test(s):
results = pp.OneOrMore( pp.Word(pp.alphas) ).parseString( s )
print repr(s),"->",list(results)

print "Python version:", sys.version
print "pyparsing version:", pp.__version__
test("abc def")
test("abc")
---------------
The output I get is:
Python version: 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit
(Intel)]
pyparsing version: 1.3.1
'abc def' -> ['abc', 'def']
'abc' -> ['abc']

What versions of pyparsing and Python are you using?

-- Paul

Jul 19 '05 #6

P: n/a
John -

I just modified my test program BNF to use ZeroOrMore instead of
OneOrMore, and parsed an empty string. Calling list() on the returned
results gives an empty list. What version of pyparsing are you seeing
this None/object/list behavior?

-- Paul

Jul 19 '05 #7

P: n/a
On Mon, Jun 27, 2005 at 08:21:41AM -0600, John Roth wrote:
Unfortunately, I've seen that behavior a number of times:
no output is None, one output is the object, more than one
is a list of objects. That forces you to have checks for None
and list types all over the place.


maybe you can at least push this into a single convenience function...

def destupid(x, constructor=tuple, sequencetypes=(tuple, list)):
if x is None: return constructor()
if isinstance(x, sequencetypes): return x
return constructor((x,))

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFCwCU1Jd01MZaTXX0RAivoAKCKcSf5YQaOSp3HWDYnct qzYzxKHwCePRb2
j564UN61oP7i8Cyoy1VHxL0=
=rasL
-----END PGP SIGNATURE-----

Jul 19 '05 #8

P: n/a
Modified version of test program, to handle empty strings -> empty
lists.

import pyparsing as pp
import sys

def test(s):
results = pp.ZeroOrMore( pp.Word(pp.alphas) ).parseString( s )
print repr(s),"->",list(results)

print "Python version:", sys.version
print "pyparsing version:", pp.__version__
test("abc def")
test("abc")
test("")

Prints:
Python version: 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit
(Intel)]
pyparsing version: 1.3.1
'abc def' -> ['abc', 'def']
'abc' -> ['abc']
'' -> []

Jul 19 '05 #9

P: n/a
Hi Paul and everyone else,

I ran the script and here's what I got:

Python version: 2.4.1 (#2, Mar 31 2005, 00:05:10)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1666)]
pyparsing version: 1.3
'abc def' -> ['abc', 'def']
'abc' -> ['abc']

It seems to work fine.

I figured out what my problem was: my earlier description was
erroneous. The type for Results2 was actually 'str', not
'pyparsing.ParseResults' as I had mentioned. (I was typing from memory
-- I didn't actually run my code.... sorry, my bad).

It seems that the way I was parsing it, I sometimes got
pyparsing.parseResults lists, and sometimes got *strings* (not
actually single-element lists as I thought).

I had expected pyparsing results to always return lists, I guess with
my token description below, it doesn't. Here's the bit of code I've
wrote:

from pyparsing import *
unit = Word(alphanums)
section = unit + Literal(':').suppress()
line = ~section + Combine(unit + SkipTo(Literal(';'), include=True))
block = ZeroOrMore(line)
file = Dict(ZeroOrMore(Group(section + block)))
Results = dict(file.parseString(MyFileString)

Where MyFileStrings (the string I want to parse) is something like
this:

var:
x, y, z;
constraints:
x <= 1;
y <= 2;
z <= 3;

And the results I'd get would be something like this:
Results

{'var': 'x, y, z;', 'constraints': (['x <= 1', 'y <= 2', 'z <= 3'],
{})}

So going along Jeff Epler's line of thinking, I wrote a convenience
function:

def convertToList(input):
if type(input) == str:
output = [input]
else:
output = input
return output

So when I iterate through the keys of the Results dictionary, I just
have to convertToList(value) and I will always get a list.

Thanks for your feedback, everyone!

(and embarassingly, I just realized I was talking to the author of
pyparsing. Thanks for pyparsing, Paul! I'm using it to write a
mini-language for defining process control models used in chemical
engineering, and it has really given me a lot of leverage in terms of
churning actual working code out in minimal time. I was thinking of
doing it the lex/yacc way at first, but pyparsing is so much easier.)

Jul 19 '05 #10

P: n/a
vd********@gmail.com wrote:
if type(input) == str:


You might consider writing this as:

if isinstance(input, basestring):

I don't know if pyparsing ever produces unicode objects, but in case it
does (or it starts to in the future), isinstance is a better call here.

STeVe
Jul 19 '05 #11

P: n/a
Steve -

Good catch - in v1.3, I added some Unicode support for pyparsing,
although I have not gotten much feedback that anyone is using it, or
how well it works. So it is preferable to test against basestring
instead of str.

-- Paul

Jul 19 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.