修改Python列表中的所有元组 [英] Modifying all the tuples in a Python list

查看:316
本文介绍了修改Python列表中的所有元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含标准格式元组的列表:

I have a list containing tuples with a standard format:

bar_list = [(bar1, bar2, bar3, bar4), (bar1, bar2, bar3, bar4), (bar1, bar2, bar3, bar4)...] 

尽管我想遍历列表中的每个元组,并对每个元组进行特定的修改,例如:

Though I want to iterate through each tuple in the list and for each make specific modifications such as:

foo0 = bar1
foo1 = get_foo(foo0) #get_foo(var) being a function
foo2 = bar2
foo3 = bar3/2

然后将重估后的元组重新打包到另一个列表中:

And then repackage the revalued tuples in another list:

foo_list = [(foo1, foo2, foo3), (foo1, foo2, foo3), (foo1, foo2, foo3)...]

我该怎么做?

推荐答案

您可以使用列表理解:

foo_list = [(get_foo(bar1), bar2, bar3/2) 
            for bar1, bar2, bar3, bar4 in bar_list]


注意,我假设您的意思是foo1等于get_foo(bar1)而不是NameError提升和自我指称


Note, I'm assuming you meant for foo1 to equal get_foo(bar1) rather than the NameError-raising and self-referential

foo1 = get_foo(foo1)

这篇关于修改Python列表中的所有元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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