分配元素时跳转 [英] Jumping around when assigning elements

查看:97
本文介绍了分配元素时跳转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python新手在这里。这是我第一次学习面向对象的

编程,试图打破常用的Korn / Perl / PHP风格

的编程。难以理解一些项目。


对于列表,我理解这一点:

C = [" need",some,help ;]

打印C [1]

一些


但我似乎无法做到这一点:

C [3] =" here


我知道C.append(这里),但这让我想到了我的问题...... />

无论如何分配到

开头没有定义的元素?就像我想要分配元素5而不是元素4

而不使用"或者没有?


我目前正在将一个Perl脚本重写为Python并且使用Perl我可以免费分配数组中的任何元素必须填写

之前的元素。我似乎无法在Python中做到这一点......除非我这是错误的。
做错了。


谢谢

--Matt

Python Newbie here. This is my first time learning object-oriented
programming and trying to break out of the usual Korn/Perl/PHP style
of programming. Having some difficulty understand some items.

For lists, I understand this:
C=["need","some","help"]
print C[1]
some

But I can''t seem to do this:
C[3]="here"

I know about C.append("here") but this brings me to my question...

Is there anyway to assign to an element that wasn''t defined in the
beginning? Like if I wanted element 5 assigned but not element 4
without using "" or None?

I''m currently re-writing a Perl script into Python and with Perl I was
free to assign any element in the array without having to fill in the
previous elements. I can''t seem to do that in Python...unless I''m
doing it wrong.

Thanks
--Matt

推荐答案

Matthew Sims在留言中写道

< 1e ******** ******************@posting.google.com> ;. ..
Matthew Sims wrote in message
<1e**************************@posting.google.com>. ..
Python新手在这里。这是我第一次学习面向对象的编程并试图打破常规的Korn / Perl / PHP风格的编程。难以理解一些项目。

对于列表,我理解这一点:
C = [" need",some,help]
打印C [1]
一些

但我似乎无法做到这一点:
C [3] =" here

我知道关于C.append(这里),但这让我想到了我的问题...

无论如何分配给一个在
开头没有定义的元素?就像我想要分配元素5而不是元素4
而不使用"还是没有?


我假设你试过''insert''?
Python Newbie here. This is my first time learning object-oriented
programming and trying to break out of the usual Korn/Perl/PHP style
of programming. Having some difficulty understand some items.

For lists, I understand this:
C=["need","some","help"]
print C[1]
some

But I can''t seem to do this:
C[3]="here"

I know about C.append("here") but this brings me to my question...

Is there anyway to assign to an element that wasn''t defined in the
beginning? Like if I wanted element 5 assigned but not element 4
without using "" or None?
I assume you tried ''insert''?
C = [" ;需要,某些,帮助]
C.insert(10,''here'')
C
C=["need","some","help"]
C.insert(10, ''here'')
C


[''需要'',''某些'',''帮助'',''这里'']


啊,那不是没有工作。 Python没有(内置)方法来做你想做的事。

原因不是技术性的,但是这个列表需要*某些东西*在那个间隔空间中,并且Python并不打算对你想要的东西进行轻率推测(显式优于隐式)。在两个行为之间选择

以插入超出列表末尾的索引时,

追加被认为不那么令人惊讶,不那么暴力,并且不太可能

引入了神秘的错误而不是填写中间元素

谁知道什么。


我甚至不能想到为什么一个希望列表具有那种

的行为。在我看来,如果我们分配给任意元素和

我们希望列表随之增长,我们应该使用不同的数据

结构,就像一个带整数键的字典。列表的索引

通常与其中包含的数据没有语义关联 - 它只是

指定顺序,数据并不关心它自己的索引。如果我们想要

a语义相关,我们通过字典的

键值对来明确表示相关性。


如果你想做这种事情(在列表插入中隐式赋值给中间的

元素),写一个函数来做它,或者子类列表。

你甚至可以修改切片行为本身(如果你去子类

route),那么C [3]将默默地表现得像perl而不是Python,但这个
是*相当的* unPythonic。 (阅读你的代码的其他Python人将会被混淆。)


如果这种答案让你感到惊讶(来自perl,它可能......) ,做一个

" import this"在交互式提示下阅读。 :)

我目前正在将一个Perl脚本重写为Python并使用Perl我可以自由地分配数组中的任何元素而无需填写
以前的元素。我似乎无法用Python做到这一点......除非我做错了。


[''need'', ''some'', ''help'', ''here'']

Ah, well that didn''t work. Python has no (builtin) way to do what you want.
The reason isn''t technical, but that the list needs *something* in that
intervening space, and Python isn''t about to make rash presumptions about
what you want there ("explicit is better than implicit"). In choosing
between two behaviors for inserting to an index beyond the end of a list,
appending was deemed less surprising, less violent, and less likely to
introduce mysterious bugs than filling in the intermediate elements with
who-knows-what.

I can''t even think of why one would want lists to have that sort of
behavior. It seems to me that if we''re assigning to arbitrary elements and
we want the list to grow with it, that we should be using a different data
structure, like a dictionary with integer keys. The index of a list
generally has no semantic correlation to the data contained therein--it just
specifies order, and the data doesn''t care about its own index. If we want
a semantic correlation, we make that correlation explicit by a dictionary''s
key-value pair.

If you want to do this sort of thing (implicit assignment to intermediate
elements in list insertions), write a function to do it, or subclass list.
You can even modify the slice behavior itself (if you go the subclass
route), so that C[3] will silently act like perl instead of Python, but this
is *quite* unPythonic. (Other Python people reading your code will be
confused.)

If this sort of answer surprises you (coming from perl, it might...), do an
"import this" at an interactive prompt and read. :)
I''m currently re-writing a Perl script into Python and with Perl I was
free to assign any element in the array without having to fill in the
previous elements. I can''t seem to do that in Python...unless I''m
doing it wrong.




同样,一般来说,Python已经* no *含义

作业/声明/修改/等。如果你没有*明确地*询问

列表来填写其中间元素,Python就不会这样做。这是一个有意识的设计决策,也是Python Python的重要组成部分。

对于至少两种伟大的感觉,IMO。


我永远无法学习perl(尽管经过多次尝试),所以我问,perl

对中间元素有什么作用? Perl数组更像是一个字典

有隐式整数键(所以中间键根本就不存在),或者

它是否用0填充中间元素或类似的东西?在

这个行为有用的perl习语是什么? (我仍然打算学习perl一个

这些天的shell脚本。)

-

Francis Avila



Again, in general, Python has *no* implicit
assignments/declarations/modifications/etc. If you don''t *explicitly* ask a
list to fill in its intermediate elements, Python won''t do it. This is a
conscious design decision, and a great part of what makes Python Python.
For at least two senses of "great", IMO.

I could never learn perl (despite many attempts), so I ask, what does perl
do to the intermediate elements? Is a perl array more like a dictionary
with implicit integer keys (so intermediate keys simply don''t exist), or
does it fill in the intermediate elements with 0 or something like that? In
what perl idioms is this behavior useful? (I still mean to learn perl one
of these days, for shell scripting.)
--
Francis Avila


Francis Avila写道:
Francis Avila wrote:
Matthew Sims在编辑的消息
Matthew Sims wrote in message
中写道。理解某些项目时遇到一些困难。

我目前正在将一个Perl脚本重写为Python并使用Perl I
可以自由地分配数组中的任何元素而无需填写
在以前的元素中。我似乎无法用Python做到这一点......除非
我做错了。
同样,一般来说,Python有*没有*隐含的
赋值/声明/修改/ etc中。如果你没有明确地*要求列表填写其中间元素,Python将不会这样做。这是一个有意识的设计决策,也是Python Python的重要组成部分。对于至少两种伟大的感觉,IMO。
of programming. Having some difficulty understand some items.

I''m currently re-writing a Perl script into Python and with Perl I
was free to assign any element in the array without having to fill
in the previous elements. I can''t seem to do that in Python...unless
I''m doing it wrong.
Again, in general, Python has *no* implicit
assignments/declarations/modifications/etc. If you don''t
*explicitly* ask a list to fill in its intermediate elements, Python
won''t do it. This is a conscious design decision, and a great part
of what makes Python Python. For at least two senses of "great", IMO.




正如这里多次说明的那样,机械师并不是一个好主意

将perl翻译为python,尽管语法和功能相似,但它们在方法上有很大不同。

更好地理解初始脚本逻辑并从头开始编写python。

在你的情况下它可能是列表,字典甚至都不是,所以

它高度依赖于手头的任务。

我永远不会学习perl(尽管有很多尝试),所以我想,perl对中间元素做了什么? perl数组更像是带有隐式整数键的字典(所以中间键根本就不存在),或者它是否用0或
类似的东西填充中间元素?这个行为在perl成语中有用吗?
(我仍然想要学习perl中的一天,用于shell脚本。)



As were stated here many times it''s not a good idea to mechanicaly
translate perl to python, they are quite different in approaches
despite similar syntax and capabilities.
Better understand initial script logic and write python from scratch.
In your case it might be list, dictionary or even none of them, so
it''s highly dependent on the task at hand.
I could never learn perl (despite many attempts), so I ask, what does
perl do to the intermediate elements? Is a perl array more like a
dictionary with implicit integer keys (so intermediate keys simply
don''t exist), or does it fill in the intermediate elements with 0 or
something like that? In what perl idioms is this behavior useful?
(I still mean to learn perl one of these days, for shell scripting.)




perl doesn''分配它们,它们是''undef'',所以它更像是python'的字典


Mike




perl doesn''t assigns them, they are ''undef'', so it''s more like python''s dict

Mike



Matthew Sims写道:
Matthew Sims wrote:
无论如何分配给
开头没有定义的元素?就像我想要分配元素5而不是元素4
而不使用"还是没有?


你可以写自己的课程:


class GrowingList(list):

def __init __(self, seq,default = None):

list .__ init __(self,seq)

self.default = default

def __setitem __(self,index ,值):

如果索引> = len(自我):

self.extend([self.default] *(index - len(self)))

self.append(value)

else:

list .__ setitem __(self,index,value)

g = GrowingList([" alpha,beta,gamma])

g [7] =" omega"

print g


这是一个部分实现,只有g [index] = value才有效。

我目前正在将一个Perl脚本重写为Python并且使用Perl我是
可以自由分配数组中的任何元素,而无需填写
以前的元素。我似乎无法用Python做到这一点......除非我做错了。
Is there anyway to assign to an element that wasn''t defined in the
beginning? Like if I wanted element 5 assigned but not element 4
without using "" or None?
You can write your own class:

class GrowingList(list):
def __init__(self, seq, default=None):
list.__init__(self, seq)
self.default = default
def __setitem__(self, index, value):
if index >= len(self):
self.extend([self.default]*(index - len(self)))
self.append(value)
else:
list.__setitem__(self, index, value)
g = GrowingList(["alpha", "beta", "gamma"])
g[7] = "omega"
print g

This is a partial implementation, only g[index] = value will work.
I''m currently re-writing a Perl script into Python and with Perl I was
free to assign any element in the array without having to fill in the
previous elements. I can''t seem to do that in Python...unless I''m
doing it wrong.




正如已经指出的那样,机械翻译将产生不合格的
结果。因为你没有用

autogrowing来描述你正在解决的问题。列表,我们几乎没有机会想出更好的

或至少更惯用的方法。


Peter


PS:欢迎使用最糟糕的编程语言 - 除了所有其他语言:-)




As already pointed out, a mechanical translation will yield substandard
results. As you did not describe the problem you are solving with the
"autogrowing" list, there is little chance for us to come up with a better
or at least more idiomatic approach.

Peter

PS: Welcome to the worst programming language - except all others :-)



这篇关于分配元素时跳转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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