Re: Odd list behavior

2009-05-14 Thread norseman

Rhodri James wrote:

On Wed, 13 May 2009 23:08:26 +0100, norseman norse...@hughes.net wrote:


Evan Kroske wrote:
I'm working on a simple file processing utility, and I encountered a 
weird error. If I try to get the first element of a list I'm 
splitting from a string, I get an error:

 key = string.split()[0]
Error!
 However, I can slice the list like normal, but that gives me a 
one-element-long list:

 key = string.split()[:1]
Success!
 Finally, the operation works perfectly if I initialize the list 
beforehand:

 list = string.split()
key = list[0]
Success!
 Why does this happen?




==
Take a look at the  split() command.

I think you will find you need one var on the left side for each piece 
on the right.


Given that he's immediately indexing the split results, that's irrelevant.
There's no point in even guessing with out the traceback.



...the first element of a list...
key = string.split()[0]
if the list is a list of tuples the error message is correct.
same for a list of key:value pairs
he will need one var on left for each piece of [0]
 k,v=  list.split(key:value)??!!


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


Re: Odd list behavior

2009-05-14 Thread Rhodri James

On Thu, 14 May 2009 17:49:33 +0100, norseman norse...@hughes.net wrote:


Rhodri James wrote:
On Wed, 13 May 2009 23:08:26 +0100, norseman norse...@hughes.net  
wrote:



Evan Kroske wrote:
I'm working on a simple file processing utility, and I encountered a  
weird error. If I try to get the first element of a list I'm  
splitting from a string, I get an error:

 key = string.split()[0]
Error!
 However, I can slice the list like normal, but that gives me a  
one-element-long list:

 key = string.split()[:1]
Success!
 Finally, the operation works perfectly if I initialize the list  
beforehand:

 list = string.split()
key = list[0]
Success!
 Why does this happen?




==
Take a look at the  split() command.

I think you will find you need one var on the left side for each piece  
on the right.
 Given that he's immediately indexing the split results, that's  
irrelevant.

There's no point in even guessing with out the traceback.




...the first element of a list...
key = string.split()[0]
if the list is a list of tuples the error message is correct.


What error message?  The OP still hasn't told us what error he got.

In any case, you aren't correct.  First, the str.split() method
returns a list of strings, so there are no tuples in sight.  Second,
even if it did, all that means is that key is a tuple, which it's
perfectly well allowed to be.

--
Rhodri James *-* Wildebeeste Herder to the Masses
--
http://mail.python.org/mailman/listinfo/python-list


Odd list behavior

2009-05-13 Thread Evan Kroske
I'm working on a simple file processing utility, and I encountered a 
weird error. If I try to get the first element of a list I'm splitting 
from a string, I get an error:


key = string.split()[0]
Error!

However, I can slice the list like normal, but that gives me a 
one-element-long list:


key = string.split()[:1]
Success!

Finally, the operation works perfectly if I initialize the list beforehand:

list = string.split()
key = list[0]
Success!

Why does this happen?
-- |
Evan Kroske
Welcome2Obscurity.Blogspot.com http://welcome2obscurity.blogspot.com
Glory is fleeting, but obscurity is forever. — some French guy |
--
http://mail.python.org/mailman/listinfo/python-list


Re: Odd list behavior

2009-05-13 Thread Chris Rebert
On Wed, May 13, 2009 at 2:51 PM, Evan Kroske e.kro...@gmail.com wrote:
 I'm working on a simple file processing utility, and I encountered a weird
 error. If I try to get the first element of a list I'm splitting from a
 string, I get an error:

 key = string.split()[0]
 Error!

 However, I can slice the list like normal, but that gives me a
 one-element-long list:

 key = string.split()[:1]
 Success!

 Finally, the operation works perfectly if I initialize the list beforehand:

 list = string.split()
 key = list[0]
 Success!

 Why does this happen?

Please include the actual error messages and Tracebacks in the future.
Error! and Success! aren't much to go on; and answering your
question without further information is just a guessing game at best.

Cheers,
Chris
-- 
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Odd list behavior

2009-05-13 Thread norseman

Evan Kroske wrote:
I'm working on a simple file processing utility, and I encountered a 
weird error. If I try to get the first element of a list I'm splitting 
from a string, I get an error:


key = string.split()[0]
Error!

However, I can slice the list like normal, but that gives me a 
one-element-long list:


key = string.split()[:1]
Success!

Finally, the operation works perfectly if I initialize the list beforehand:

list = string.split()
key = list[0]
Success!

Why does this happen?
-- |
Evan Kroske
Welcome2Obscurity.Blogspot.com http://welcome2obscurity.blogspot.com
Glory is fleeting, but obscurity is forever. — some French guy |


==
Take a look at the  split() command.

I think you will find you need one var on the left side for each piece 
on the right.


a=x y
b,c= a.split()
b
x
c
y


Maybe???


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


Re: Odd list behavior

2009-05-13 Thread Rhodri James

On Wed, 13 May 2009 23:08:26 +0100, norseman norse...@hughes.net wrote:


Evan Kroske wrote:
I'm working on a simple file processing utility, and I encountered a  
weird error. If I try to get the first element of a list I'm splitting  
from a string, I get an error:

 key = string.split()[0]
Error!
 However, I can slice the list like normal, but that gives me a  
one-element-long list:

 key = string.split()[:1]
Success!
 Finally, the operation works perfectly if I initialize the list  
beforehand:

 list = string.split()
key = list[0]
Success!
 Why does this happen?




==
Take a look at the  split() command.

I think you will find you need one var on the left side for each piece  
on the right.


Given that he's immediately indexing the split results, that's irrelevant.
There's no point in even guessing with out the traceback.

--
Rhodri James *-* Wildebeeste Herder to the Masses
--
http://mail.python.org/mailman/listinfo/python-list


Re: Odd list behavior

2009-05-13 Thread Emile van Sebille

On 5/13/2009 2:51 PM Evan Kroske said...
I'm working on a simple file processing utility, and I encountered a 
weird error. If I try to get the first element of a list I'm splitting 
from a string, 



What value of string gives these results?

Emile



I get an error:

key = string.split()[0]
Error!

However, I can slice the list like normal, but that gives me a 
one-element-long list:


key = string.split()[:1]
Success!

Finally, the operation works perfectly if I initialize the list beforehand:

list = string.split()
key = list[0]
Success!

Why does this happen?
-- |
Evan Kroske
Welcome2Obscurity.Blogspot.com http://welcome2obscurity.blogspot.com
Glory is fleeting, but obscurity is forever. — some French guy |


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


Re: Odd list behavior

2009-05-13 Thread Peter Otten
Evan Kroske wrote:

 I'm working on a simple file processing utility, and I encountered a
 weird error. If I try to get the first element of a list I'm splitting
 from a string, I get an error:
 
 key = string.split()[0]
 Error!

When string contains only whitespace string.split() returns an empty list an 
then

 [][0]
Traceback (most recent call last):
  File stdin, line 1, in module
IndexError: list index out of range

 However, I can slice the list like normal, but that gives me a
 one-element-long list:
 
 key = string.split()[:1]
 Success!

The slice length may be shorter than specified:

 [][:1]
[]


 Finally, the operation works perfectly if I initialize the list
 beforehand:
 
 list = string.split()
 key = list[0]
 Success!

Not with the same string as in your previous examples.

Peter

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