Convert a list into a sequence of string triples

别说谁变了你拦得住时间么 提交于 2019-12-02 07:17:11

You can use a comprehension:

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

Similar to list comprehension, but a bit different.

This is the map function.

lst = ["Red", "Green", "Blue"]
new_lst = map(lambda x: (x.upper(), x, ""), lst)

It basically changes every element of a list one-by-one according to the function you enter as the first parameter. Which is in this case:

lambda x: (x.upper(), x, "")

If you don't have an idea what lambda is, it is almost like in the high school math:

f(x) = (x.upper(), x, "") # basically defines a function in one line.

The code in the return statement is called list comprehension.

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

You can find more info http://www.secnetix.de/olli/Python/list_comprehensions.hawk

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!