加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

这个Python脚本可以改进吗?

发布时间:2020-12-16 22:11:58 所属栏目:Python 来源:网络整理
导读:这个Python代码可以改进吗? def build_list(types): for x in types: for a in ['short','long','average']: for b in ['square','sloped','average']: for c in ['small','large','average']: for d in ['thin','thick','average']: for e in ['high','low

这个Python代码可以改进吗?

   def build_list(types):
        for x in types:
            for a in ['short','long','average']:
                for b in ['square','sloped','average']:
                    for c in ['small','large','average']:
                        for d in ['thin','thick','average']:
                            for e in ['high','low','average']:
                                for f in [True,False]:
                                    for g in [True,False]:
                                        for h in ['flat','average']:
                                            for i in ['long','short','average']:
                                                for j in [True,False]:
                                                    for k in ['thin','average']:
                                                        for l in ['thin','average']:
                                                            yield [x,a,b,c,d,e,f,g,h,i,j,k,l]
    facets_list = list(build_list(xrange(1,121)))
    print len(facets_list)
最佳答案
是.你可以使用itertools.product()

import itertools
facets_list = list(itertools.product(types,['short','average'],['square',['small',...))

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读