解压列表到变量 [英] Unpack list to variables

查看:67
本文介绍了解压列表到变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表:

row = ["Title", "url", 33, "title2", "keyword"]

有没有更多的pythonic方式可以解压缩这些值,如:

Is there a more pythonic way to unpack this values like:

title, url, price, title2, keyword = row[0], row[1], row[2], row[3], row[4]

推荐答案

像这样的事情?

>>> row = ["Title", "url", 33, "title2", "keyword"]
>>> title, url, price, title2, keyword = row

同样,为了记录在案,请注意您的示例将因IndexError而失败(Python的列表基于零).

Also and for the record, note that your example will fail with an IndexError (Python's lists are zero-based).

上面的注释是在修正OP示例之前写的...

这篇关于解压列表到变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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