简体   繁体   English

理解Python List Comprehension等价物

[英]Understanding Python List Comprehension equivalent

I have the following code: 我有以下代码:

listOfStrings = ['i_am_exercising', 'python_functional', 'lists_comprehension']
[ "".join([elem.title() for elem in splited]) for splited in [el.split("_")for el in listOfStrings]]

the result of this is: 结果是:

['IAmExercising', 'PythonFunctional', 'ListsComprehension']

reading the documentation , i got the equivalent extended version, which puts the first expression in a variable to be appended and the second expression in a list to be iterated with the for statement: 阅读文档 ,我得到了等效的扩展版本,它将第一个表达式放在一个要附加的变量中,第二个表达式放在一个列表中,用for语句进行迭代:

returned = []
for splited in [el.split("_")for el in listOfStrings]: 
            returned.append("".join([elem.title() for elem in splited]))

but if i want write the same code without any list comprehension, how is the best way to do this? 但如果我想在没有任何列表理解的情况下编写相同的代码,那么最好的方法是什么? i tried with the following code, which works well: 我尝试使用以下代码,效果很好:

returned = []
temp = []

for el in listOfStrings:                 
    temp = []                          
    for splited in el.split("_"):       
        temp.append(splited.title())  
    returned.append("".join(temp))

but i didn't completely understood how to do this (transform the list comprehension to an equivalent full extended form) 但我没有完全理解如何做到这一点(将列表理解转换为等效的完整扩展形式)

You have both a nested list comprehensions, one inside the other, plus another one to create a list of split elements first. 你有一个嵌套的列表推导,一个在另一个里面,另外一个用于创建一个拆分元素列表。 You can reduce this to just two loops instead: 您可以将此减少为仅两个循环:

returned = []
for el in listOfStrings:
    tmp = []
    for splited in el.split("_"):
        tmp.append(splited.title())
    returned.append("".join(tmp))

This simplifies back down to a list comprehension of the form: 这简化了对表单的列表理解:

["".join([splited.title() for splited in el.split("_")]) for el in listOfStrings]

You can easily convert from outwards to inwards: 您可以轻松地从外部转换为内部:

listOfStrings = ['i_am_exercising', 'python_functional', 'lists_comprehension']

result = [ "".join([elem.title() for elem in split]) for split in [el.split("_")for el in listOfStrings]]
print result

result = []
for split in [el.split("_") for el in listOfStrings]:
    result.append("".join([elem.title() for elem in split]))
print result

result = []
temp1 = []
for el in listOfStrings:
    temp1.append(el.split("_"))
for split in temp1:
    result.append("".join([elem.title() for elem in split]))
print result

result = []
temp1 = []
for el in listOfStrings:
    temp1.append(el.split("_"))
for split in temp1:
    temp2 = []
    for elem in split:
        temp2.append(elem.title())
    result.append("".join(temp2))
print result

Basically you just follow the following scheme: 基本上你只需遵循以下方案:

result = [foo for bar in baz]

is turned into 变成了

result = []
for bar in baz:
    result.append(foo)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM