且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

寻址列表的最后一个元素

更新时间:2023-11-10 09:10:16

或者:


有没有办法参考最后一个列表的元素,使用

作为简写:


ref:=& lst [len(lst) - 1]#我知道语法是错了,但你得到了

的想法


然后使用列表的最后一个元素(假设元素是

a dict):


ref [" foo"] = 42

ref [" bar"] =" Ni!"





/ David


Aloha,

pi************@gmail.com 写道:
不是比
更简单的方法[len(lst) - 1] = ...



lst [ -1] = ...


祝你节日快乐

LOBI
uote>

我知道有一个简单的方法:)


只是为了满足我的好奇心:有没有办法做一些像

我在上面建议的参考解决方案?


Isn''t there an easier way than

lst[len(lst) - 1] = ...

?

Or alternatively:

Is there a way to make reference to the last element of a list, to use
as a shorthand:

ref := &lst[len(lst) - 1] # I know syntax is wrong, but you get the
idea

and then using the last element of the list by (assuming the element is
a dict):

ref["foo"] = 42
ref["bar"] = "Ni!"

etc.

/David


Aloha,

pi************@gmail.com wrote:
Isn''t there an easier way than
lst[len(lst) - 1] = ...


lst[-1] = ...

Wishing a happy day
LOBI


I knew there was an easy way :)

Just to satisfy my curiousity: Is there a way to do something like the
reference solution I suggest above?