在发电机上需要帮助

在发电机上需要帮助

本文介绍了在发电机上需要帮助......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 你好, 我正试图了解如何使用发电机从一个根连续设置后续连续设置 : l = [1,2,3,4] 想生产: [1, 2],[2,3],[3,4],[1,2,3],[2,3,4] 但不幸的是不能,我想我可以通过使用子生成器 并且可能枚举,如果你帮忙可以解释一下 技巧吗?看起来像这个子发生器让我搞砸了。 (我认为应该也有[1,],[2,],[3,],[1,2] ,3,4]和 然后被过滤掉) bests 解决方案 [[1,2],[2,3],[3,4],[1,2,3],[2,3,4]] - Denis S. Otkidach http://www.python。 ru / [ru] 你的意思是: [1,2],[2,3],[3,4],[1, 2,3],[2,3,4],[1,3,4] (例如除了空集之外的电源组中的所有元素,带有 的元素和所有元素的集合。) 否则,请用语言描述你想要的集合 - 我看不到 这一点。 ... for x in xrange(2,len(l)): ... for x in xrange(0,len (l)-i + 1): ...产生l [j:j + i] 因为你的大脑比我快得多(尽管我最终得到了 完全相同的事情,除了变量名称)并打败我发布 答案,我会发布不可避免的生成器表达式版本 代替: consecutive_sets =(x [offset :offset + subset_size] $ x $ b for subset_size in xrange(2,len(x)) for offset in xrange(0,len(x)+ 1 - subset_size)) - Craig Ringer hello,i''m trying to understand how i could build following consecutive setsfrom a root one using generator :l = [1,2,3,4]would like to produce :[1,2], [2,3], [3,4], [1,2,3], [2,3,4]but unfortunately can not, i guess i can do it by using sub generatorand maybe enumerate, please if you help could you explain a bit thetrick ? looks like this sub generator thing mess me up.(i think it should may be also have [1,], [2,], [3,], [1,2,3,4] , andthen be filtered)bests 解决方案[[1, 2], [2, 3], [3, 4], [1, 2, 3], [2, 3, 4]]--Denis S. Otkidach http://www.python.ru/ [ru]Do you mean:[1,2], [2,3], [3,4], [1,2,3], [2,3,4], [1,3,4](E.g. all elements in the power set except the empty set, the sets withone element and the sets with all elements.)Otherwise, please describe your desired sets in verbal - I cannot seethe point. ... for i in xrange(2, len(l)): ... for j in xrange(0, len(l)-i+1): ... yield l[j:j+i]Since you have a much faster brain than I (though I ended up withexactly the same thing barring variable names) and beat me to postingthe answer, I''ll post the inevitable awful generator expression versioninstead:consecutive_sets = ( x[offset:offset+subset_size]for subset_size in xrange(2, len(x))for offset in xrange(0, len(x) + 1 - subset_size) )--Craig Ringer 这篇关于在发电机上需要帮助......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-24 04:52