自动简化/重构Python代码(例如,用于循环->列表理解)? [英] Automatically simplifying/refactoring Python code (e.g. for loops -> list comprehension)?
问题描述
在Python中,我非常喜欢使用列表理解时实现的简洁程度.我喜欢做简洁的列表理解:
myList = [1, 5, 11, 20, 30, 35] #input data
bigNumbers = [x for x in myList if x > 10]
但是,我经常遇到更详细的实现,例如:
myList = [1, 5, 11, 20, 30, 35] #input data
bigNumbers = []
for i in xrange(0, len(myList)):
if myList[i] > 10:
bigNumbers.append(myList[i])
当for loop
仅浏览一个数据结构(例如myList[]
)时,通常会有一个简单的列表理解语句,等效于循环.
考虑到这一点,是否有一个重构工具可以将冗长的Python循环转换为简洁的列表理解语句?
上一个 StackOverflow 2to3 是一种重构工具,可以执行任意重构,只要您可以使用语法模式指定它们即可.您可能要寻找的模式是这个 这可以安全地重构为 在您的特定示例中,这将给出 然后,您可以进行另一次重构,将xrange(0,N)替换为xrange(N),
另一个替换 使用 此重构存在几个问题: 如果您想学习如何编写2to3修复程序,请看 Lennart Regebro的书. /p> In Python, I really enjoy how concise an implementation can be when using list comprehension. I love to do concise list comprehensions this: However, I often encounter more verbose implementations like this: When a Previous StackOverflow questions have asked for advice on transforming loops into list comprehension. But, I have yet to find a question about automatically converting loops into list comprehension expressions. Motivation: There are numerous ways to answer the question "what does it mean for code to be clean?" Personally, I find that making code concise and getting rid of some of the fluff tends to make code cleaner and more readable. Naturally there's a line in the sand between "concise code" and "incomprehensible one-liners." Still, I often find it satisfying to write and work with concise code. 2to3 is a refactoring tool that can perform arbitrary refactorings, as long as you can specify them with a syntactical pattern. The pattern you might want to look for is this This can be refactored safely to In your specific example, this would give Then, you can have another refactoring that replaces xrange(0, N) with xrange(N),
and another one that replaces with There are several problems with this refactoring: If you want to learn how to write 2to3 fixers, take a look at Lennart Regebro's book. 这篇关于自动简化/重构Python代码(例如,用于循环->列表理解)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!VARIABLE1 = []
for VARIABLE2 in EXPRESSION1:
if EXPRESSION2:
VARIABLE1.append(EXPRESSION3)
VARIABLE1 = [EXPRESSION3 for VARIABLE2 in EXPRESSION1 if EXPRESSION2]
bigNumbers = [myList[i] for i in xrange(0, len(myList)) if myList[i] > 10]
[VARIABLE1[VARIABLE2] for VARIABLE2 in xrange(len(VARIABLE1)) if EXPRESSION1]
[VARIABLE3 for VARIABLE3 in VARIABLE1 if EXPRESSION1PRIME]
EXPRESSION1PRIME
必须为EXPRESSION1
,且所有出现
VARIABLE1[VARIABLE2]
替换为VARIABLE3.这是可能的
2to3,但需要显式代码才能进行遍历和替换.EXPRESSION1PRIME
不能再包含以下内容:
VARIABLE1
.也可以使用显式代码进行检查.x
;
没有合理的方法可以自动完成此操作.你可以
为此选择了回收VARIABLE1
(即i
),但这可能会造成混淆
因为它表明i
仍然是索引.选择合成材料可能会起作用
名称,例如VARIABLE1_VARIABLE2
(即myList_i
),然后检查是否
否则不会使用.iter(VARIABLE1)
时.无法自动执行此操作.myList = [1, 5, 11, 20, 30, 35] #input data
bigNumbers = [x for x in myList if x > 10]
myList = [1, 5, 11, 20, 30, 35] #input data
bigNumbers = []
for i in xrange(0, len(myList)):
if myList[i] > 10:
bigNumbers.append(myList[i])
for loop
only looks through one data structure (e.g. myList[]
), there is usually a straightforward list comprehension statement that is equivalent to the loop.
With this in mind, is there a refactoring tool that converts verbose Python loops into concise list comprehension statements?
VARIABLE1 = []
for VARIABLE2 in EXPRESSION1:
if EXPRESSION2:
VARIABLE1.append(EXPRESSION3)
VARIABLE1 = [EXPRESSION3 for VARIABLE2 in EXPRESSION1 if EXPRESSION2]
bigNumbers = [myList[i] for i in xrange(0, len(myList)) if myList[i] > 10]
[VARIABLE1[VARIABLE2] for VARIABLE2 in xrange(len(VARIABLE1)) if EXPRESSION1]
[VARIABLE3 for VARIABLE3 in VARIABLE1 if EXPRESSION1PRIME]
EXPRESSION1PRIME
must be EXPRESSION1
with all occurrences of
VARIABLE1[VARIABLE2]
replaced by VARIABLE3. This is possible with
2to3, but requires explicit code to do the traversal and replacement.EXPRESSION1PRIME
then must not contain no further occurrences of
VARIABLE1
. This can also be checked with explicit code.x
;
there is no reasonable way to have this done automatically. You could
chose to recycle VARIABLE1
(i.e. i
) for that, but that may be confusing
as it suggests that i
is still an index. It might work to pick a synthetic
name, such as VARIABLE1_VARIABLE2
(i.e. myList_i
), and check whether
that's not used otherwise.iter(VARIABLE1)
. It's not possible to do this automatically.