我不建议使用列表理解重构。很难理解,因为列表推导严格意为迭代生成列表的简明表示法。如果您的编辑器具有矩形选择功能,则可以执行以下操作:
# First tab the sirnames out away from the given names. (They don't need to be neatly
# aligned like this,you can just copy paste a bunch of spaces.)
print('Jane','Bennet')
print('Elizabeth','Bennet')
print('Mary','Bennet')
# Use rectangular selection to get rid of the sir names and the print statements,# leaving the commas. An editor like Geany will also allow you to get rid of the
# trailing whitespace,making your code easier to navigate.
'Jane','Elizabeth','Mary',
# Add a variable initialization followed by square brackets around the given names.
# You can also pretty it up by indenting or deleting newlines as you see fit.
givenNames = [
'Jane',]
# Add your for loop.
givenNames = [
'Jane',]
for name in givenNames:
print(f"{name} bennet")
本文链接:https://www.f2er.com/2953083.html