numList =[1,3,5,7,9]from itertools import combinationsfor i inrange(1,len(numList)+1):# xrange will return the values 1,2,3,4 in this loopprint(list(combinations(numList, i)))输出:
[('1',),('3',),('5',),('7',),('9',)][('1','3'),('1','5'),('1','7'),('1','9'),('3','5'),('3','7'),('3','9'),('5','7'),('5','9'),('7','9')][('1','3','5'),('1','3','7'),('1','3','9'),('1','5','7'),('1','5','9'),('1','7','9'),('3','5','7'),('3','5','9'),('3','7','9'),('5','7','9')][('1','3','5','7'),('1','3','5','9'),('1','3','7','9'),('1','5','7','9'),('3','5','7','9')][('1','3','5','7','9')]
[[[x,y]for x in list1]for y in list2]
输出
[[[1,5],[2,5],[3,5],[4,5],[5,5]],[[1,6],[2,6],[3,6],[4,6],[5,6]],[[1,7],[2,7],[3,7],[4,7],[5,7]],[[1,8],[2,8],[3,8],[4,8],[5,8]],[[1,9],[2,9],[3,9],[4,9],[5,9]]][[x,y]for x in list1 for y in list2][[1,5],[1,6],[1,7],[1,8],[1,9],[2,5],[2,6],[2,7],[2,8],[2,9],[3,5],[3,6],[3,7],[3,8],[3,9],[4,5],[4,6],[4,7],[4,8],[4,9],[5,5],[5,6],[5,7],[5,8],[5,9]]
repeat第一个列表,permutate第二个列表和zip它们一起
from itertools import permutations, repeata =[1,2,3]b =[4,5,6]print(list(list(zip(r, p))for(r, p)inzip(repeat(a), permutations(b))))[[(1,4),(2,5),(3,6)],[(1,4),(2,6),(3,5)],[(1,5),(2,4),(3,6)],[(1,5),(2,6),(3,4)],[(1,6),(2,4),(3,5)],[(1,6),(2,5),(3,4)]]
贴一下灵神的题解里面的解释~ 就是一种优化策略,logtrick
class Solution {
public:int minimumDifference(vector<int>& nums, int k) {int res 0x3f3f3f3f;int n nums.size();for(int i0;i<n;i){res min(res,abs(nums[i]-k));for(int j i-1;j&g…
目录 1.算法原理2.改进点3.结果展示4.参考文献5.代码获取 1.算法原理
【智能算法】灰狼算法(GWO)原理及实现
2.改进点
混沌反向学习策略
融合Logistic混沌映射和Tent混沌映射生成Logistic-Tent复合混沌映射: Z i 1 { ( r Z i ( 1 − Z i ) ( 4 −…