首页 > 编程知识 正文

python导入自定义的py文件,python解释器

时间:2023-05-05 08:19:58 阅读:111808 作者:4714

对销售订单中的销售零售类或单件深入分析销售工会的形态。 例如,一个订单包含ABC三种,它可以分为七种销售a、b、c、AB、AC、BC、ABC。 因此,需要针对各订单的所有相关种类制作全工会穷举清单,从而可以将一个工会对应表与销售记录相关联地进行分析。

要找到合适的工具,完全组合列出动态循环中的类别并不容易。 最近,我在python本机函数包itertools中找到了一个接近需求的函数组合。 经过一番辛苦,终于缩小到了自己想要的东西,做成笔记,分享如下:

官方网站文件https://docs.python.org/zh-cn/3/library/ITER tools.html

IterTools.combinations(Iters,n )元组输出迭代器根据列表,所有组合直接append被分为三个元组。

#组合(ABCD ),2 )-- AB AC AD BC BD CD

#组合(范围(4),3 )-- 012 013 023 123

原始处方combinationsdefcombinations (iterable,r ) :pool=tuple(iterable ) n=len ) pool ) ifrn : return indices=LLE fori=in-r : break else : return indices [ I ]=1forjinrange (i1,r ) : indices [ j ]=indices [ j-1 ]1yeld tupp

importitertoolsasitsprint (list (its.combinations (' ABC ',1 ) )、end='n ' ) print (list ) its.combinations n ' ) print(list ) its.combinations(ABC ),3 ) )、)、b )、)、c )、)、c ) )、c )、c ) 4]list22=[]forIinrange(1,len(list1)1) 3360iter=its.combinang I ) list.append(list(ITER ) ) print ) list

import itertools as itslist3=['A '、' b '、' c '、' d']list4=[]forIinrange(1, len(list1)1) 3360iter=list I ) list4.extend(list(ITER ) print (list4) )、)、b )、)、c )、)、d

list5=[]for j in list4: str1=,' l=str1.join(j ) list5.append(l ) print ) list5),' a ',' b,' c' b,' 让我们列一个数字清单,看看你是怎么进来和怎么出来的

*由于返回到数字元素的元组格式,因此无法将在原始列表2中输入的数字列表修改为字符格式进行拼接。 *

list6=[]for j i

n list2: str1 = ',' l = str1.join(j) list6.append(l)print(list6) ---------------------------------------------------------------------------TypeError Traceback (most recent call last)<ipython-input-5-a0d372cf3412> in <module> 2 for j in list2: 3 str1 = ','----> 4 l = str1.join(j) 5 list6.append(l) 6 print(list6)TypeError: sequence item 0: expected str instance, tuple found

既然不完美.....

那么手术开始了,修改官方配方,自定义以完成最终按字符拼接 修改后的自定义combs配方 def combs(iterable, r): pool = list(map(str,iterable)) #手术1换心 替换成字符串 n = len(pool) if r > n: return indices = list(range(r)) yield list(pool[i] for i in indices) while True: for i in reversed(range(r)): if indices[i] != i + n - r: break else: return indices[i] += 1 for j in range(i+1, r): indices[j] = indices[j-1] + 1 yield list(pool[i] for i in indices) #手术2换皮 替换成列表输出 print(list(combs(['A','B','C','D'],2))) [['A', 'B'], ['A', 'C'], ['A', 'D'], ['B', 'C'], ['B', 'D'], ['C', 'D']]

加上上面两段小代码*数字列表输入,数字转字符列表输出*

list11 = [1,2,3,4]list22 = []list33 = []for j in range(1,len(list11)+1): #list 组扩展 iter = combs(list11,j) list22.extend(list(iter))for k in list22: #list 转字符拼接 str2 = ',' l = str2.join(k) list33.append(l) print(list33) ['1', '2', '3', '4', '1,2', '1,3', '1,4', '2,3', '2,4', '3,4', '1,2,3', '1,2,4', '1,3,4', '2,3,4', '1,2,3,4']

又满意了点 :)

好,无自由毋宁死,*分组函数打包,设定条件自由选择需要在输入类表内参与组合的元素个数,自定义函数Ncombs()*

def Ncombs(lst0,st=None,ed=None): def combs(iterable, r): pool = list(map(str,iterable)) n = len(pool) if r > n: return indices = list(range(r)) yield list(pool[i] for i in indices) while True: for i in reversed(range(r)): if indices[i] != i + n - r: break else: return indices[i] += 1 for j in range(i+1, r): indices[j] = indices[j-1] + 1 yield list(pool[i] for i in indices) lst1=[] lst2=[] if (st == None) & (ed == None): for j in range(1,len(lst0)+1): #不传参,全部组合 iter = combs(lst0,j) lst1.extend(list(iter)) elif (ed == None): for j in range(1,len(lst0)+1): #传最大组合元素数 if (j >= st): iter = combs(lst0,j) lst1.extend(list(iter)) elif (st == None): for j in range(1,len(lst0)+1): #传最小组合元素数 if (j <= ed): iter = combs(lst0,j) lst1.extend(list(iter)) else: for j in range(1,len(lst0)+1): #传组合元素数的范围 if (j >= st)&(j <= ed): iter = combs(lst0,j) lst1.extend(list(iter)) for k in lst1: #list 转字符拼接 bk = ',' l = bk.join(k) lst2.append(l) return lst2print(Ncombs(['Aa','B','Cc','D','Ee'],ed=3)) ['Aa', 'B', 'Cc', 'D', 'Ee', 'Aa,B', 'Aa,Cc', 'Aa,D', 'Aa,Ee', 'B,Cc', 'B,D', 'B,Ee', 'Cc,D', 'Cc,Ee', 'D,Ee', 'Aa,B,Cc', 'Aa,B,D', 'Aa,B,Ee', 'Aa,Cc,D', 'Aa,Cc,Ee', 'Aa,D,Ee', 'B,Cc,D', 'B,Cc,Ee', 'B,D,Ee', 'Cc,D,Ee'] *简单建自定义函数库,调用:)* import mydatafunc as myprint(my.netcombs([1,2,3,4,'A'],3,3)) ['1,2,3', '1,2,4', '1,2,A', '1,3,4', '1,3,A', '1,4,A', '2,3,4', '2,3,A', '2,4,A', '3,4,A']

自定义函数—参数说明

首参:可迭代字符串,列表,元组组合元素最小个数组合元素最大个数
仅传最小显示最小至全元素的全组合,仅传最大ed=#N显示1至N个全组合;最小最大一致=n显示n个元素全组合,不传任何参数显示全组合。

以上自定义组合函数完成,就可以按单号分组,拼接产品类,引用my.netcombs打开相关全组合,爆炸函数打爆一下,完美的订单类别全组合清单就实现了。

刚学python不久,纪念一下。

目录

官网文件  

原始配方combinations

因为只能按参2个数输出,按网上资料修改成*全组合展示的代码,保持元组分组形式*

那么手术开始了,修改官方配方,自定义以完成最终按字符拼接

修改后的自定义combs配方


版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。