【发布时间】:2015-10-09 23:53:53
【问题描述】:
我完成了以下 Python 脚本,它应该返回一个子列表列表。
def checklisting(inputlist, repts):
result = []
temprs = []
ic = 1;
for x in inputlist
temprs.append(x)
ic += 1
if ic == repts:
ic = 1
result.append(temprs)
return result
示例:如果我使用以下参数调用函数:
checklisting(['a', 'b', 'c', 'd'], 2)
它会返回
[['a', 'b'], ['c', 'd']]
或者如果我这样称呼它:
checklisting(['a', 'b', 'c', 'd'], 4)
它会返回
[['a', 'b', 'c', 'd']]
然而它返回的是一个奇怪的巨大列表:
>>> l.checklisting(['a','b','c','d'], 2)
[['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd']]
请有人帮忙!我需要该脚本来编译包含数据的列表:
['water tax', 20, 'per month', 'electric tax', 1, 'per day']
其背后的逻辑是将列表中 repts 大小的序列分隔为子列表,以便更好、更容易地组织。我不想要任意的子列表块,因为其他问题中的这些子列表没有正确指定序列的大小。
【问题讨论】:
-
它对我不起作用:(