I have a list y y
['20001201', 'ARRO', '04276410', '18.500', '19.500', '18.500',
'19.500', '224']
from which I want to extract only the 2nd and 4th item by partially
unpacking the list. So I trieda,b = y[2,4]
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: list indices must be integers
Out of curiosity, I trieda,b = y[2:4] a
'04276410' b
'18.500'
Why does this work (to a point - it gives me items 2 and 3, not 2 and
4 as I require) and not my first attempt? What is the right syntax to
use when partially upacking a sequence?
Thanks in advance
Thomas Philips 9 1401
<tk****@hotmail.com> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.com... I have a list yy ['20001201', 'ARRO', '04276410', '18.500', '19.500', '18.500', '19.500', '224']
from which I want to extract only the 2nd and 4th item by partially unpacking the list. So I trieda,b = y[2,4] Traceback (most recent call last): File "<interactive input>", line 1, in ? TypeError: list indices must be integers
Out of curiosity, I trieda,b = y[2:4] a '04276410' b
'18.500'
Why does this work (to a point - it gives me items 2 and 3, not 2 and 4 as I require) and not my first attempt? What is the right syntax to use when partially upacking a sequence?
Thanks in advance
Thomas Philips
a,b = y[2],y[4]
or
a,b = y[2:5:2]
or
a,b = ( y[i] for i in (2,4) )
-- Paul tk****@hotmail.com wrote: I have a list yy ['20001201', 'ARRO', '04276410', '18.500', '19.500', '18.500', '19.500', '224']
from which I want to extract only the 2nd and 4th item by partially unpacking the list. So I trieda,b = y[2,4] Traceback (most recent call last): File "<interactive input>", line 1, in ? TypeError: list indices must be integers
Out of curiosity, I trieda,b = y[2:4] a '04276410' b
'18.500'
Why does this work (to a point - it gives me items 2 and 3, not 2 and 4 as I require) and not my first attempt? What is the right syntax to use when partially upacking a sequence?
if you want two items, fetch two items:
a = y[2]
b = y[4]
y[2:4] is a 2-item slice starting at index 2 and ending *before* index 4.
see the documentation for more on slicing.
</F>
"Paul McGuire" <pt***@austin.rr._bogus_.com> wrote in message
news:Fk*****************@tornado.texas.rr.com... <tk****@hotmail.com> wrote in message news:11**********************@z34g2000cwc.googlegr oups.com... I have a list y>>y ['20001201', 'ARRO', '04276410', '18.500', '19.500', '18.500', '19.500', '224']
from which I want to extract only the 2nd and 4th item by partially unpacking the list. So I tried>>a,b = y[2,4] Traceback (most recent call last): File "<interactive input>", line 1, in ? TypeError: list indices must be integers
Out of curiosity, I tried>>a,b = y[2:4] >>a '04276410'>> b '18.500'
Why does this work (to a point - it gives me items 2 and 3, not 2 and 4 as I require) and not my first attempt? What is the right syntax to use when partially upacking a sequence?
Thanks in advance
Thomas Philips
a,b = y[2],y[4]
or
a,b = y[2:5:2]
or
a,b = ( y[i] for i in (2,4) )
-- Paul
Forgot one:
_,_,a,_,b,_,_,_ = y
There actually is some merit to this form. If the structure of y changes
sometime in the future (specifically if a field is added or removed), this
statement will fail noisily, calling your attention to this change. But if
a new field is added, say at the front of the list, the previous methods
will all silently succeed, but now giving you the values formerly known as
y[1] and y[3].
-- Paul
Thank you, everyone, for resolving my question. At one point, while
trying to solve the problem, I typed y[1,3]
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: list indices must be integers
The error message gave me no clue as to what I was doing wrong (in my
mind, I was just writing out the elements of a range), and I thought
perhaps that my inclusion of a comma was the problem. Perhaps a more
explicit error message would have helped.
Another solution, I suppose, is to use a list comprehension. Store the
indices in a list x. For example, let x = [1,5,4]. Then
a,b,c = [y[i] for i in x]
Thanks
Thomas Philips
Paul McGuire wrote: Forgot one:
_,_,a,_,b,_,_,_ = y
There actually is some merit to this form. If the structure of y changes sometime in the future (specifically if a field is added or removed), this statement will fail noisily, calling your attention to this change. But if a new field is added, say at the front of the list, the previous methods will all silently succeed, but now giving you the values formerly known as y[1] and y[3].
if this is likely to happen, an straightforward assert is a lot easier to parse than
a one-two-a-three-oops-one-two-a-four-five-etc number of underscores:
assert len(y) == 8
a = y[2]
b = y[4]
</F> tk****@hotmail.com enlightened us with: y[1,3] Traceback (most recent call last): File "<interactive input>", line 1, in ? TypeError: list indices must be integers
The error message gave me no clue as to what I was doing wrong (in my mind, I was just writing out the elements of a range), and I thought perhaps that my inclusion of a comma was the problem.
You thought correct. List indices must be integers, not tuples.
Perhaps a more explicit error message would have helped.
Why? You thought the right thing.
Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
>> Thank you, everyone, for resolving my question. At one point, while trying to solve the problem, I typed
> y[1,3]
Traceback (most recent call last): File "<interactive input>", line 1, in ? TypeError: list indices must be integers
The error message gave me no clue as to what I was doing wrong (in my mind, I was just writing out the elements of a range), and I thought perhaps that my inclusion of a comma was the problem. Perhaps a more explicit error message would have helped.
The error message is correct because in y[1, 3] "1, 3" is recognized by
the interpreter
as tuple. Python goodie or snakebite that is... tk****@hotmail.com wrote: Thank you, everyone, for resolving my question. At one point, while trying to solve the problem, I typed
y[1,3]
Traceback (most recent call last): File "<interactive input>", line 1, in ? TypeError: list indices must be integers
The error message gave me no clue as to what I was doing wrong (in my mind, I was just writing out the elements of a range), and I thought perhaps that my inclusion of a comma was the problem. Perhaps a more explicit error message would have helped.
the problem is that the *compiler* doesn't know what "y" is, and y[1,3]
is a perfectly valid way to access e.g. a dictionary or a multidimensional
array. so it's the list implementation that has to do the complaining, and
all it knows is that it wants an integer index, and got something else.
maybe something like
TypeError: list indices must be integers (got tuple)
would have been less confusing; feel free to add a suggestion to the bug
tracker: http://sourceforge.net/tracker/?grou...70&atid=105470
</F> tk****@hotmail.com wrote: I have a list y
y ['20001201', 'ARRO', '04276410', '18.500', '19.500', '18.500', '19.500', '224']
from which I want to extract only the 2nd and 4th item
by partially unpacking the list. So I tried a,b = y[2,4]
Mmm, so lovely and meaningful names !-)
FWIW, and since nobody seemed to mention it, list indexes are
zero-based, so the second element of a list is at index 1 and the fourth
at index 3.
Also, a GoodPractice(tm) is to use named constants instead of magic
numbers. Here we don't have a clue about why these 2 elements are so
specials. Looking at the example list (which - semantically - should be
a tuple, not a list) I could wild-guess that the 2nd item is a reference
and the fourth a price, so:
REF_INDEX = 1 # lists are zero-based
PRICE_INDEX = 3
ref, price = y[REF_INDEX], y[PRICE_INDEX]
And finally, since this list is clearly structured data, wrapping it
into an object to hide away implementation details *may* help -
depending on the context, of course !-)
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])" This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Dave Benjamin |
last post by:
Hey all,
I just realized you can very easily implement a sequence grouping function
using Python 2.3's fancy slicing support:
def group(values, size):
return map(None, * for i in...
|
by: george young |
last post by:
I came across an cool python feature that I've not seen discussed.
This may be *implied* by the language reference manual
http://docs.python.org/ref/assignment.html], but it was never
obvious to...
|
by: Paul McGuire |
last post by:
I'm trying to manage some configuration data in a list of tuples, and I
unpack the values with something like this:
configList =
for data in configList:
name,a,b,c = data
... do something...
|
by: Chris |
last post by:
Hi
I'm attempting to write a client for an existing p2p network.
The protocol defines that ints are packed into 4 bytes for transfer.
// Creating the byte vector using the following is fine:...
|
by: harold |
last post by:
Dear all,
Maybe I stared on the monitor for too long, because I cannot find the
bug ...
My script "transition_filter.py" starts with the following lines:
import sys
for line in sys.stdin :...
|
by: John Salerno |
last post by:
I'm a little confused, but I'm sure this is something trivial. I'm
confused about why this works:
('more', 'less'),
('something', 'nothing'),
('good', 'bad'))
(('hello', 'goodbye'), ('more',...
|
by: ram |
last post by:
Stupid question #983098403:
I can't seem to pass an unpacked sequence and keyword arguments to a
function at the same time. What am I doing wrong?
def f(*args, **kw):
for a in args:
print...
|
by: McA |
last post by:
Hi all,
probably a dumb question, but I didn't find something elegant for my
problem so far.
In perl you can unpack the element of a list to variables similar as
in python
(a, b, c = ), but...
|
by: mjahabarsadiq |
last post by:
Hi
I have created a web application. I am using ant to build the war and deploy in tomcat.
The war file is deployed under "TOMCATE_HOME/work/standalone/localhost/onlineres.war".
I have my...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |