list整数索引模具? [英] list Integer indexing dies??

查看:52
本文介绍了list整数索引模具?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。看看这段代码。

l = ['''',''b'', ''c'','d'']
l
['''',''b'',''c'',''d''] l [0] [0] [0]
''''

打印值a。到目前为止很好:-)

l [0] ---> ''a''。

l [0] [0] ---> ''''[0] - > ''a''。

l [0] [0] [0] ---> ''a''[0] [0] - > ''''[0] - > ''a''


现在为什么这个包含整数的列表似乎不起作用?

l = [1,2,3]
l [0]
1 l [0] [0]


回溯(最近一次调用最后一次):

文件"< pyshell #244>",第1行,在-toplevel-

l [0] [0]

TypeError:unsubscriptable对象l [0]
1 1 [ 0]


回溯(最近一次调用最后一次):

文件"< pyshell#246>",第1行,在-toplevel-

1 [0]

TypeError:unsubscriptable对象




编译器报告unsubscriptable对象??困惑,眼花缭乱我是??? !! ??

同样的列表现在保存整数而不是字符串和l [0] [0] [0]

哪个有效早期字符串上的罚款似乎无法工作

整数???

非常感谢任何帮助。


-

欢呼,

Ishwor Gurung

解决方案

Op 2004-12-23, Ishwor schreef<是*********** @ gmail.com>:

大家好。看看这段代码。

l = ['''',''b'',''c'','' d'']
l ['''',''b'',''c'',''d''] l [0] [0] [0]


''a''
它打印值''a''。到目前为止很好:-)
l [0] ---> ''a''。
l [0] [0] ---> ''''[0] - > ''a''。
l [0] [0] [0] ---> ''a''[0] [0] - > ''''[0] - > ''a''

现在为什么这个包含整数的列表似乎不起作用?




因为这只适用于字符串。


字符串是python中唯一一个元素和一个序列之间隐含

等价的对象。


所以一个字符是一个字符串,一个字符串是一个序列

个字符。


所以''a''[0]再次'' a'可以再次被

0索引多次。

我认为python会更加一致如果

字符串可能是字符的元组,也许

暗示任何对象和

a单个对象之间的等价。


- -

Antoon Pardon


2004年12月23日14:28:37 GMT,Antoon Pardon< ap ***** @ forel。 vub.ac.be>写道:

Op 2004-12-23,Ishwor schreef< is *********** @ gmail.com>:

大家好。看看这段代码。

> l = ['''',''b'',''c'','d'']
> l


['''',''b'',''c'',''d'']

> l [0] [0] [0]


''a''
它打印值''a''。到目前为止很好:-)
l [0] ---> ''a''。
l [0] [0] ---> ''''[0] - > ''a''。
l [0] [0] [0] ---> ''a''[0] [0] - > ''''[0] - > ''a''

现在为什么这个包含整数的列表似乎不起作用?



因为这只适用于字符串。
<字符串是python中唯一的对象,它在元素和一个元素的序列之间具有隐含的等效性。

因此,一个字符是字符串,字符串是序列
所以''''[0]再次''a''可以再次被
0索引多次。




;-) gotcha。但是这也不应该有效吗?

123232 [0]



基本上python可以从对象类型推断并打印出来b / b $ 1而不是咳嗽那些错误?我作为学习者的经历

是应该有一些自动化&比如好吧你想要

对整数进行索引(取决于上下文);我会给你指数

在该整数中的第0位置 ????

[snip]


谢谢Antoon。

-

欢呼,

Ishwor Gurung


Op 2004-12-23,Ishwor schreef<是*********** @ gmail。 com>:

2004年12月23日14:28:37 GMT,Antoon Pardon< ap ***** @ forel.vub.ac.be>写道:

Op 2004-12-23,Ishwor schreef< is *********** @ gmail.com>:

>大家好。看看这段代码。
>
>>>> l = ['''',''b'',''c'',''d'']
>>>> l
> ['''',''b'',''c'',''d'']
>>>> l [0] [0] [0]
> ''a''
>它打印值''a''。到目前为止很好:-)
> l [0] ---> ''a''。
> L [0] [0] ---> ''''[0] - > ''a''。
> l [0] [0] [0] ---> ''a''[0] [0] - > ''''[0] - > ''a''
>
>现在为什么这个包含整数的列表似乎不起作用?
因为这只适用于字符串。

字符串是python中唯一具有隐含的
等价的对象。元素和一个元素的序列。

所以一个字符是一个字符串,一个字符串是一个字符序列。

所以''a''[0 ]再次''a''可以再次被
0索引多次。



;-) gotcha。但是这也不应该有效吗?

123232 [0]



如果它应该变为有效,它应该只返回123232 IMO。

其中python基本上可以从对象类型推断并打印出来而不是咳嗽那些错误?


为什么你觉得它应该咳嗽1?


假设我用八进制表示法写一个数字。

035 [0]应该怎么办?小心它应该

咳嗽与29 [0]相同。

我作为学习者的经验
是应该有一些自动化&比如说好吧你想对整数进行索引(取决于上下文);我会给你那个整数中第0位的指数
???




整数没有位置。我们在整数中想到的位置是我们的符号系统的人工制品。即便如此,如果我们只是将
推迟到十进制表示法,那么仍然存在问题。你看到有一个

的参数可以使123232 [0]咳嗽2.因为

从后面开始索引我们得到了很好的对应关系/>
数字的索引和它代表的10的幂。


-

Antoon Pardon


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

解决方案

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


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


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


这篇关于list整数索引模具?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆