寻址列表的最后一个元素 [英] Addressing the last element of a list

查看:85
本文介绍了寻址列表的最后一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不是比
更好的方式吗?
lst [len(lst) - 1] = ...




解决方案

或者:


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

作为简写:


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

的想法


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

a dict):


ref [" foo"] = 42

ref [" bar"] =" Ni!"





/ David


Aloha,

pi************@gmail.com 写道:

不是比
更简单的方法[len(lst) - 1] = ...



lst [ -1] = ...


祝你节日快乐

LOBI

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


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

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


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?


这篇关于寻址列表的最后一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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