我试着解决下面的练习,不使用datetime
练习:
Given a list of int, such that the First three int represent a date, the second three elementi represent a date etc..modify lst by grouping every triple in One string with the numbers separeted by "/".
示例:
lst = [1, 2, 2013, 23, 9, 2011, 10, 11, 2000]
groupd(lst)
lst
['1/2/2013', '23/9/2011', '10/11/2000']
我的尝试:
lst = [1, 2, 2013, 23, 9, 2011, 10, 11, 2000].
stri = str(lst).
def groupd(lst):.
cont = 1.
a = (stri.replace(',', '/')).
for x in lst:.
if len[x]>2:.
lst.insert(lst[0],a )].
print(a).
print(groupd(lst)).
PS:对不起我的英语!!谢谢大家
这更像是一种函数方法,其中答案是通过递归函数传递的
如果您不想使用列表理解或groupby,这也是解决此类问题的基本方法
可以使用
zip
创建元组,然后将它们格式化为字符串:从偏移量(第一个参数到切片)开始,同时跳过项目(第三个参数到切片)允许窗口行为
更一般地说:
您可以使用
itertools
中的groupby
按索引对列表进行分组:相关问题 更多 >
编程相关推荐