使用本地 function/intermediate 变量提高可读性的 python 列表理解
Improve pythons list conprehensions using local function/intermediate variable for readability
我要对齐(这里是简单方程列表的左侧,当前是字符串)。基本示例是这样的:
414 / 46 = 9
3 / 1 = 3
114 / 38 = 3
给定的代码将returns对齐(在示例中为“=”)列表:
414 / 46 = 9
3 / 1 = 3
114 / 38 = 3
列表可能会变大(由于目标平台有限)。
现在我破解了一个嵌套列表理解表达式。
此代码中的两个表达式 do 都有效。
但是向其他编码人员展示我在可读性/代码效率方面经历了一些 "resistance"。我喜欢创建其他(新手和经验丰富的)编码人员也能快速理解的代码。另一方面,这可能不会对目标系统造成太大压力。
正在查看Intermediate variable in a list comprehension for simultaneous filtering and transformation
和
Most elegant way to modify elements of nested lists in place 暗示 有时列表理解不是正确的工具 。
使用普通循环 for e in l
我不知道如何就地交换列表中的方程字符串。
我可以将表达式转换成一些 旧的 c 风格索引循环 for i in xrange
循环,将新的(修剪过的)lexpr 方程分配给给定的索引.
我想更好地了解在哪里使用哪些选项(也许为什么)?
在这两个方面:理解别人的代码和性能。
我自己尝试了这两个变体(注释行):
def trim_equations_lwidth( equation_list, width ):
'''expects string-expressions like "1 + 3 = x"
and trims all of the left expr to same width.
Expects width to be >= length of lexp
'''
#ltrimmed_equations = [ equation.replace( equation[:equation.find('=')], equation[:equation.find('=')].rjust(width, ' ')) for (equation) in equation_list ]
ltrimmed_equations = [ equation.replace(lexpr, lexpr.rjust(width, ' ')) for (lexpr, equation) in ( ( equ[:equ.find('=')], equ) for (equ) in equation_list )]
return ltrimmed_equations
我建议使用本地函数:
def trim_equations_lwidth( equation_list, width ):
'''expects string-expressions like "1 + 3 = x"
and trims all of the left expr to same width.
Expects width to be >= length of lexp
'''
def _replace(equation):
lexpr = equation[:equation.find('=')]
return equation.replace(lexpr, lexpr.rjust(width, ' '))
ltrimmed_equations = [_replace(equation) for equation in equation_list]
return ltrimmed_equations
我要对齐(这里是简单方程列表的左侧,当前是字符串)。基本示例是这样的:
414 / 46 = 9
3 / 1 = 3
114 / 38 = 3
给定的代码将returns对齐(在示例中为“=”)列表:
414 / 46 = 9
3 / 1 = 3
114 / 38 = 3
列表可能会变大(由于目标平台有限)。
现在我破解了一个嵌套列表理解表达式。 此代码中的两个表达式 do 都有效。 但是向其他编码人员展示我在可读性/代码效率方面经历了一些 "resistance"。我喜欢创建其他(新手和经验丰富的)编码人员也能快速理解的代码。另一方面,这可能不会对目标系统造成太大压力。
正在查看Intermediate variable in a list comprehension for simultaneous filtering and transformation 和 Most elegant way to modify elements of nested lists in place 暗示 有时列表理解不是正确的工具 。
使用普通循环 for e in l
我不知道如何就地交换列表中的方程字符串。
我可以将表达式转换成一些 旧的 c 风格索引循环 for i in xrange
循环,将新的(修剪过的)lexpr 方程分配给给定的索引.
我想更好地了解在哪里使用哪些选项(也许为什么)? 在这两个方面:理解别人的代码和性能。
我自己尝试了这两个变体(注释行):
def trim_equations_lwidth( equation_list, width ):
'''expects string-expressions like "1 + 3 = x"
and trims all of the left expr to same width.
Expects width to be >= length of lexp
'''
#ltrimmed_equations = [ equation.replace( equation[:equation.find('=')], equation[:equation.find('=')].rjust(width, ' ')) for (equation) in equation_list ]
ltrimmed_equations = [ equation.replace(lexpr, lexpr.rjust(width, ' ')) for (lexpr, equation) in ( ( equ[:equ.find('=')], equ) for (equ) in equation_list )]
return ltrimmed_equations
我建议使用本地函数:
def trim_equations_lwidth( equation_list, width ):
'''expects string-expressions like "1 + 3 = x"
and trims all of the left expr to same width.
Expects width to be >= length of lexp
'''
def _replace(equation):
lexpr = equation[:equation.find('=')]
return equation.replace(lexpr, lexpr.rjust(width, ' '))
ltrimmed_equations = [_replace(equation) for equation in equation_list]
return ltrimmed_equations