473,385 Members | 1,341 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

list Integer indexing dies??

Hi all. Look at this snippet of code.
l = ['a','b','c','d']
l ['a', 'b', 'c', 'd'] l[0][0][0] 'a'
It prints the value 'a'. Fine so far :-)
l[0] ---> 'a' .
l[0][0]---> 'a'[0] --> 'a'.
l[0][0][0] ---> 'a'[0][0] --> 'a'[0] --> 'a'

Now why doesnt this list which holds integer seem to work??
l = [1,2,3]
l[0] 1 l[0][0]
Traceback (most recent call last):
File "<pyshell#244>", line 1, in -toplevel-
l[0][0]
TypeError: unsubscriptable object l[0] 1 1[0]
Traceback (most recent call last):
File "<pyshell#246>", line 1, in -toplevel-
1[0]
TypeError: unsubscriptable object


The compiler reports unsubscriptable object ?? confused , dazzled i am ???!!??
The same list now holds integer instead of strings and l[0][0][0]
which worked fine earlier on strings doesn't seem to work on
integers???
Any help is greatly appreciated.

--
cheers,
Ishwor Gurung
Jul 18 '05 #1
10 2021
Op 2004-12-23, Ishwor schreef <is***********@gmail.com>:
Hi all. Look at this snippet of code.
l = ['a','b','c','d']
l ['a', 'b', 'c', 'd'] l[0][0][0]

'a'
It prints the value 'a'. Fine so far :-)
l[0] ---> 'a' .
l[0][0]---> 'a'[0] --> 'a'.
l[0][0][0] ---> 'a'[0][0] --> 'a'[0] --> 'a'

Now why doesnt this list which holds integer seem to work??


Because this only works with strings.

String is the only object in python which has an implied
equivallence between an element and a squence of one.

So one character is a string and a string is a sequence
of characters.

So 'a'[0] is again 'a' which can again be indexed by
0 as many times as you want.
I think python would have been more consistent if
strings would have been tuples of chars and maybe
implied an equivalence between whatever object and
a singleton of that object.

--
Antoon Pardon
Jul 18 '05 #2
On 23 Dec 2004 14:28:37 GMT, Antoon Pardon <ap*****@forel.vub.ac.be> wrote:
Op 2004-12-23, Ishwor schreef <is***********@gmail.com>:
Hi all. Look at this snippet of code.
> l = ['a','b','c','d']
> l

['a', 'b', 'c', 'd']
> l[0][0][0]

'a'
It prints the value 'a'. Fine so far :-)
l[0] ---> 'a' .
l[0][0]---> 'a'[0] --> 'a'.
l[0][0][0] ---> 'a'[0][0] --> 'a'[0] --> 'a'

Now why doesnt this list which holds integer seem to work??


Because this only works with strings.

String is the only object in python which has an implied
equivallence between an element and a squence of one.

So one character is a string and a string is a sequence
of characters.

So 'a'[0] is again 'a' which can again be indexed by
0 as many times as you want.


;-) gotcha. But shouldn't this be valid too??
123232[0]

in which basically python can infer from the object type and print out
1 instead of coughing up those errors? My experience as a learner here
is that there should be some automagics & say like "okay you want to
do indexing on integers ( context dependent); i'll give you the index
of 0th position in that integer" ???
[snip]

Thanks Antoon.
--
cheers,
Ishwor Gurung
Jul 18 '05 #3
Op 2004-12-23, Ishwor schreef <is***********@gmail.com>:
On 23 Dec 2004 14:28:37 GMT, Antoon Pardon <ap*****@forel.vub.ac.be> wrote:
Op 2004-12-23, Ishwor schreef <is***********@gmail.com>:
> Hi all. Look at this snippet of code.
>
>>>> l = ['a','b','c','d']
>>>> l
> ['a', 'b', 'c', 'd']
>>>> l[0][0][0]
> 'a'
> It prints the value 'a'. Fine so far :-)
> l[0] ---> 'a' .
> l[0][0]---> 'a'[0] --> 'a'.
> l[0][0][0] ---> 'a'[0][0] --> 'a'[0] --> 'a'
>
> Now why doesnt this list which holds integer seem to work??
Because this only works with strings.

String is the only object in python which has an implied
equivallence between an element and a squence of one.

So one character is a string and a string is a sequence
of characters.

So 'a'[0] is again 'a' which can again be indexed by
0 as many times as you want.


;-) gotcha. But shouldn't this be valid too??
123232[0]


Well if it should become valid, it should just return 123232 IMO.
in which basically python can infer from the object type and print out
1 instead of coughing up those errors?
Why do you feel it should cough up 1?

Suppose I write a number in octal notation.

What should 035[0] cough up? Be carefull it should
cough up the same as 29[0].
My experience as a learner here
is that there should be some automagics & say like "okay you want to
do indexing on integers ( context dependent); i'll give you the index
of 0th position in that integer" ???


Integers have no position. The position we think of in integers is an
artefact of our notational system. Even then if we would simply defer
to decimal notation there is still a problem. You see there are a
number of arguments that would make 123232[0] cough up 2. Because
by starting indexing from the back we get a nice correspondence between
the index of the number and the power of 10 it represents.

--
Antoon Pardon
Jul 18 '05 #4
> ;-) gotcha. But shouldn't this be valid too??
123232[0]


No, it shouldn't be valid. It makes the implicit assumption that you
want the base 10 digit, which isn't really a good assumption to make in
the world of computers. Programmers are just as likely to want
hexadecimal, and arguments could be made for octal or binary too.
Basically, there's no intuitive meaning for trying to slice an integer.

Also, it seems like it would be very rare that anybody would want to do
this. Most of those cases fall into one of two categories- (1) trying
to display digits of a number in a human readable way, and (2) trying
to do some kind of math.

(1) is better handled by explicitly converting it to a string first,
and
(2) is likely better handled using logarithms.

Jul 18 '05 #5
On 23 Dec 2004 15:05:20 GMT, Antoon Pardon <ap*****@forel.vub.ac.be> wrote:
Op 2004-12-23, Ishwor schreef <is***********@gmail.com>:
On 23 Dec 2004 14:28:37 GMT, Antoon Pardon <ap*****@forel.vub.ac.be> wrote:
Op 2004-12-23, Ishwor schreef <is***********@gmail.com>:
> Hi all. Look at this snippet of code.
>
>>>> l = ['a','b','c','d']
>>>> l
> ['a', 'b', 'c', 'd']
>>>> l[0][0][0]
> 'a'
> It prints the value 'a'. Fine so far :-)
> l[0] ---> 'a' .
> l[0][0]---> 'a'[0] --> 'a'.
> l[0][0][0] ---> 'a'[0][0] --> 'a'[0] --> 'a'
>
> Now why doesnt this list which holds integer seem to work??

Because this only works with strings.

String is the only object in python which has an implied
equivallence between an element and a squence of one.

So one character is a string and a string is a sequence
of characters.

So 'a'[0] is again 'a' which can again be indexed by
0 as many times as you want.
;-) gotcha. But shouldn't this be valid too??
> 123232[0]
Well if it should become valid, it should just return 123232 IMO.

Im not sure i understand u but what i meant was that
123 + 2 125 # nice n good

now it would be nice if integer could also be *subscripted* too 123[0] + 2 3
;-) But as i said in earlier post said, i'll stick with import this's
#2 by Tim Peters. Its better to leave these design issues with other
**senior pythoneers**.

in which basically python can infer from the object type and print out
1 instead of coughing up those errors?


Why do you feel it should cough up 1?


123232[0] #hypothetical 0th position in the integer. 1

Suppose I write a number in octal notation.

What should 035[0] cough up? Be carefull it should 035[0] 3 # my own opinion.
cough up the same as 29[0]. 29[0]

2 #again my own opinion
[snip]
by starting indexing from the back we get a nice correspondence between
the index of the number and the power of 10 it represents.

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


As in my view if python could treat object in context sensitive
manner, it would be better world but its just my own beginners
opinion.
Happy hunting with Python. ;-)

--
cheers,
Ishwor Gurung
Jul 18 '05 #6
On Fri, 24 Dec 2004 02:59:40 +1030, Ishwor <is***********@gmail.com> wrote:
123[0] + 2 3

TypeError: unsubscriptable object
in which basically python can infer from the object type and print out
1 instead of coughing up those errors?


Why do you feel it should cough up 1?

123232[0] #hypothetical 0th position in the integer. 1

TypeError: unsubscriptable object
Suppose I write a number in octal notation.

What should 035[0] cough up? Be carefull it should

035[0] 3 # my own opinion.

TypeError: unsubscriptable object
cough up the same as 29[0].

29[0]

2 #again my own opinion

TypeError: unsubscriptable object
Just-in-my-own-opinion-ly y'rs

Stephen Thorne
Jul 18 '05 #7
Antoon Pardon wrote:
Suppose I write a number in octal notation.

What should 035[0] cough up? Be carefull it should
cough up the same as 29[0].


given that 035==000000000000000000000000000000000000000000000 035 I say they
should both cough up 0 for any positive index of course.
Jul 18 '05 #8
Ishwor <is***********@gmail.com> writes:
On 23 Dec 2004 14:28:37 GMT, Antoon Pardon <ap*****@forel.vub.ac.be> wrote:
My experience as a learner here is that there should be some
automagics & say like "okay you want to do indexing on integers (
context dependent); i'll give you the index of 0th position in that
integer" ???


Python doesn't do things automagically. The rules for pythonic
behavior include "Explicit is better than implicit" and "In the face
of ambiguity, refuse the tempation to guess." Both of those are
violated by automatic conversions and the like.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 18 '05 #9
On Fri, 24 Dec 2004 05:44:50 -0600, Mike Meyer <mw*@mired.org> wrote:
Ishwor <is***********@gmail.com> writes:
On 23 Dec 2004 14:28:37 GMT, Antoon Pardon <ap*****@forel.vub.ac.be> wrote:
My experience as a learner here is that there should be some
automagics & say like "okay you want to do indexing on integers (
context dependent); i'll give you the index of 0th position in that
integer" ???


Python doesn't do things automagically. The rules for pythonic
behavior include "Explicit is better than implicit" and "In the face
of ambiguity, refuse the tempation to guess." Both of those are
violated by automatic conversions and the like.


Well *shrugs* that was just an opionion though a bad one from a
beginner :-)... No hard feeling pythoner friends. ;-) and Merry
Christmas to you all ;-)
Then gingerbread is ready at
http://www.mediatinker.com/blog/archives/008798.html
;-)

[snip]

--
cheers,
Ishwor Gurung
Jul 18 '05 #10
Ishwor <is***********@gmail.com> writes:
On Fri, 24 Dec 2004 05:44:50 -0600, Mike Meyer <mw*@mired.org> wrote:
Ishwor <is***********@gmail.com> writes:
> On 23 Dec 2004 14:28:37 GMT, Antoon Pardon <ap*****@forel.vub.ac.be> wrote:
> My experience as a learner here is that there should be some
> automagics & say like "okay you want to do indexing on integers (
> context dependent); i'll give you the index of 0th position in that
> integer" ???


Python doesn't do things automagically. The rules for pythonic
behavior include "Explicit is better than implicit" and "In the face
of ambiguity, refuse the tempation to guess." Both of those are
violated by automatic conversions and the like.


Well *shrugs* that was just an opionion though a bad one from a
beginner :-)... No hard feeling pythoner friends. ;-) and Merry
Christmas to you all ;-)
Then gingerbread is ready at
http://www.mediatinker.com/blog/archives/008798.html


I'm aware that it was an opinion from a beginner. I thought it was
worth pointing out *why* this was a bad opinion in Python. I certainly
didn't intend to offend or insult anyone by doing so.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 18 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Hilde Roth | last post by:
This may have been asked before but I can't find it. If I have a rectangular list of lists, say, l = ,,], is there a handy syntax for retrieving the ith item of every sublist? I know about for i...
12
by: Steven Bethard | last post by:
So I need to do something like: for i in range(len(l)): for j in range(i+1, len(l)): # do something with (l, l) where I get all pairs of items in a list (where I'm thinking of pairs as sets,...
1
by: Hal Styli | last post by:
hello, can someone please help... Im interested in the use of an auxiliary array to act like pointers for another array, allowing the sort order to be traversed. See code below. I dont have an...
0
by: darrel | last post by:
I've asked this in the past with a few responses, but thought I'd as one more time ;o) As we polish up our in-house CMS tool, there's two components that we're missing yet that we can either...
11
by: Zorpiedoman | last post by:
The problem is this: I have a list box. I set an array list as the datasource. I remove an item from the array list. I set the listbox datasource to nothing. I set the listbox datasource to...
3
by: Gary Wessle | last post by:
Hi can type conversion work to convert an int to a list? I am trying to solve an problem in one tutorial. **************************************************************** a = , ] As an...
8
by: jerry.levan | last post by:
Hi, I have a file that contains a "tcl" list stored as a string. The list members are sql commands ex: { begin { select * from foo where baz='whatever'} {select * from gooble } end { insert...
4
by: Emin | last post by:
Dear Experts, How much slower is dict indexing vs. list indexing (or indexing into a numpy array)? I realize that looking up a value in a dict should be constant time, but does anyone have a...
6
by: Monty | last post by:
Hello, I have a singleton settings class (.Net 2.0 framework) that I serialize/deserialize to XML. On my settings class is a shared list of integers. If I have two numbers in my list and I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
0
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...
0
BarryA
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...
1
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.