我怎样才能让这 3 行代码更干
How can i make these 3 lines of code more DRY
此代码:
if len(group['elements']) > 0:
groups.append(group)
group = {'bla': '', 'elements': []}
在下面的例子中重复了 3 次。我想在 1 行中完成(至少减少它)。有可能吗?那我该怎么做呢?
collection_of_items = [
['strong', 'a', ['a'], '', 'strong', ['a'], ['a'], 'a', 'a', [], ''],
['strong', 'a', ['a'], '', 'strong', 'a']
]
groups = []
for items in collection_of_items:
group = {'bla': '', 'elements': []}
for item in items:
if hasattr(item, 'lower'):
if item == 'strong':
group['bla'] = item
elif item =='a':
group['elements'].append(item)
elif item == '':
# Make it DRY <---------------------------------------
if len(group['elements']) > 0:
groups.append(group)
group = {'bla': '', 'elements': []}
else:
if 'a' in item:
group['elements'].append(item[0])
else:
# Make it DRY <---------------------------------------
if len(group['elements']) > 0:
groups.append(group)
group = {'bla': '', 'elements': []}
# Make it DRY <---------------------------------------
if len(group['elements']) > 0:
groups.append(group)
group = {'bla': '', 'elements': []}
print(groups)
修改这 3 行,
注意:除了示例代码的结构外,什么都不能改变
抱歉有错误。
将该代码放入一个函数中,并在需要时调用它。但严重的是,4 space 缩进。
collection_of_items = [
['strong', 'a', ['a'], '', 'strong', ['a'], ['a'], 'a', 'a', [], ''],
['strong', 'a', ['a'], '', 'strong', 'a']
]
groups = []
def my_func(g):
if len(g['elements']) > 0:
groups.append(g)
g = {'bla': '', 'elements': []}
return g
for items in collection_of_items:
group = {'bla': '', 'elements': []}
for item in items:
if hasattr(item, 'lower'):
if item == 'strong':
group['bla'] = item
elif item =='a':
group['elements'].append(item)
elif item == '':
group = my_func(group)
else:
if 'a' in item:
group['elements'].append(item[0])
else:
group = my_func(group)
group = my_func(group)
print(groups)
此代码:
if len(group['elements']) > 0:
groups.append(group)
group = {'bla': '', 'elements': []}
在下面的例子中重复了 3 次。我想在 1 行中完成(至少减少它)。有可能吗?那我该怎么做呢?
collection_of_items = [
['strong', 'a', ['a'], '', 'strong', ['a'], ['a'], 'a', 'a', [], ''],
['strong', 'a', ['a'], '', 'strong', 'a']
]
groups = []
for items in collection_of_items:
group = {'bla': '', 'elements': []}
for item in items:
if hasattr(item, 'lower'):
if item == 'strong':
group['bla'] = item
elif item =='a':
group['elements'].append(item)
elif item == '':
# Make it DRY <---------------------------------------
if len(group['elements']) > 0:
groups.append(group)
group = {'bla': '', 'elements': []}
else:
if 'a' in item:
group['elements'].append(item[0])
else:
# Make it DRY <---------------------------------------
if len(group['elements']) > 0:
groups.append(group)
group = {'bla': '', 'elements': []}
# Make it DRY <---------------------------------------
if len(group['elements']) > 0:
groups.append(group)
group = {'bla': '', 'elements': []}
print(groups)
修改这 3 行,
注意:除了示例代码的结构外,什么都不能改变
抱歉有错误。
将该代码放入一个函数中,并在需要时调用它。但严重的是,4 space 缩进。
collection_of_items = [
['strong', 'a', ['a'], '', 'strong', ['a'], ['a'], 'a', 'a', [], ''],
['strong', 'a', ['a'], '', 'strong', 'a']
]
groups = []
def my_func(g):
if len(g['elements']) > 0:
groups.append(g)
g = {'bla': '', 'elements': []}
return g
for items in collection_of_items:
group = {'bla': '', 'elements': []}
for item in items:
if hasattr(item, 'lower'):
if item == 'strong':
group['bla'] = item
elif item =='a':
group['elements'].append(item)
elif item == '':
group = my_func(group)
else:
if 'a' in item:
group['elements'].append(item[0])
else:
group = my_func(group)
group = my_func(group)
print(groups)