将列表转换为字符串三元组序列 [英] Convert a list into a sequence of string triples

查看:90
本文介绍了将列表转换为字符串三元组序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想转换一个列表,例如:

I'd like to convert a list like:

["Red", "Green", "Blue"]

转换为字符串三元组的元组序列:

into a tuple sequence of string triples:

[("RED", "Red", ""), ("GREEN", "Green", ""), ("BLUE", "Blue", "")]

直到现在我一直使用这种方法:

Until now I always use this method:

def list_to_items(lst):
    items = []
    for i in lst:
        items.append((i.upper(), i, ""))
    return items

但是感觉有点难看.有更好/更多的pythonic方式做到这一点吗?

But it feels a bit ugly. Is there a nicer / more pythonic way of doing this?

推荐答案

您可以使用理解:

def list_to_items(lst):
    return [(item.upper(), item.title(), '') for item in lst]

这篇关于将列表转换为字符串三元组序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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