Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 列表上的动态嵌套循环_Python_List_Loops_Numpy_Nested - Fatal编程技术网

Python 列表上的动态嵌套循环

Python 列表上的动态嵌套循环,python,list,loops,numpy,nested,Python,List,Loops,Numpy,Nested,我有下面的代码来计算一些游戏中所有可能的赔率组合。我想做以下几件事 删除所有丑陋的嵌套循环 允许动态游戏,也就是说,我不必继续创建一个匹配列表,并向嵌套循环添加条目。(例如,我想添加匹配项k) 代码如下: import numpy as np """ Odds Predictions. ---------------------------------------- """ bet_amount = 1.00 # Set up odds of all teams in groups, to

我有下面的代码来计算一些游戏中所有可能的赔率组合。我想做以下几件事

  • 删除所有丑陋的嵌套循环
  • 允许动态游戏,也就是说,我不必继续创建一个匹配列表,并向嵌套循环添加条目。(例如,我想添加匹配项k)
  • 代码如下:

    import numpy as np
    
    """
    Odds Predictions.
    ----------------------------------------
    """
    
    bet_amount = 1.00
    
    # Set up odds of all teams in groups, touple contains odds for coming in first place and second place.
    match_a = [("RUS-W", 2.37), ("RUS-X", 3.20), ("RUS-L", 3.50)]
    match_b = [("ROM-W", 3.30), ("ROM-X", 3.10), ("ROM-L", 2.50)]
    match_c = [("FRA-W", 1.25), ("FRA-X", 6.00), ("FRA-L", 11.00)]
    match_d = [("ENG-W", 1.57), ("ENG-X", 4.00), ("END-L", 7.00)]
    match_e=  [("UKR-W", 1.57), ("UKR-X", 4.00), ("UKR-L", 7.00)]
    match_f = [("GER-W", 1.57), ("GER-X", 4.33), ("GER-L", 6.50)]
    match_g = [("ITA-W", 1.83), ("ITA-X", 3.50), ("ITA-L", 5.25)]
    match_h = [("CZK-W", 4.75), ("CZK-X", 3.50), ("CZK-L", 1.90)]
    match_i = [("SPA-W", 1.40), ("SPA-X", 4.75), ("SPA-L", 10.00)]
    match_j = [("BEL-W", 1.72), ("BEL-X", 3.80), ("BEL-L", 5.50)]
    
    totals = []
    total_rank = []
    count = 0
    
    print 'Computing Combinations'
    
    for mat_a in match_a:
      for mat_b in match_b:
        for mat_c in match_c:
          for mat_d in match_d:
            for mat_e in match_e:
              for mat_f in match_f:
                for mat_g in match_g:
                  for mat_h in match_h:
                    for mat_i in match_i:
                      for mat_j in match_j:
    
                        entry = '%s, %s, %s, %s, %s, %s, %s, %s, %s, %s,' % (mat_a[0] , mat_b[0] , mat_c[0] , mat_d[0] , mat_e[0] , mat_f[0], mat_g[0] , mat_h[0] , mat_i[0] , mat_j[0])
                        rank = mat_a[1] * mat_b[1] * mat_c[1] * mat_d[1] * mat_e[1] * mat_f[1]* mat_g[1] * mat_h[1] * mat_i[1] * mat_j[1]
    
                        totals.append([entry, rank])
                        total_rank.append(totals)
    
                        count += 1
    
    print 'Total Combinations : %d ' % count
    print 'Sorting Combinations'
    
    totals = sorted(totals,key=lambda x: x[1])
    
    print 'Bet Amount : %f' % bet_amount
    
    for count, total in enumerate(totals):
      if count < 100:
        print ' %s  ->  %s' % (total[0], total[1] * bet_amount)
      else:
        break;
    
    将numpy导入为np
    """
    赔率预测。
    ----------------------------------------
    """
    下注金额=1.00
    #设置小组中所有团队的赔率,touple包含获得第一名和第二名的赔率。
    匹配a=[((“RUS-W”,2.37),(“RUS-X”,3.20),(“RUS-L”,3.50)]
    匹配_b=[((“ROM-W”,3.30),(“ROM-X”,3.10),(“ROM-L”,2.50)]
    匹配c=[(“FRA-W”,1.25),(“FRA-X”,6.00),(“FRA-L”,11.00)]
    匹配d=[(“ENG-W”,1.57),(“ENG-X”,4.00),(“END-L”,7.00)]
    匹配e=[((“UKR-W”,1.57),(“UKR-X”,4.00),(“UKR-L”,7.00)]
    匹配f=[(“GER-W”,1.57),(“GER-X”,4.33),(“GER-L”,6.50)]
    匹配g=[((“ITA-W”,1.83),(“ITA-X”,3.50),(“ITA-L”,5.25)]
    匹配h=[((“CZK-W”,4.75),(“CZK-X”,3.50),(“CZK-L”,1.90)]
    匹配i=[(“SPA-W”,1.40),(“SPA-X”,4.75),(“SPA-L”,10.00)]
    match_j=[(“BEL-W”,1.72),(“BEL-X”,3.80),(“BEL-L”,5.50)]
    总计=[]
    总排名=[]
    计数=0
    打印“计算组合”
    对于比赛a中的材料a:
    对于match_b中的mat_b:
    对于match_c中的mat_c:
    对于匹配中的材料:
    对于匹配中的材料:
    对于匹配中的材料:
    对于匹配中的材料:
    对于匹配中的材料:
    对于mat_i,在Math_i中:
    对于match_j中的mat_j:
    条目=“%s、%s、%s、%s、%s、%s、%s、%s、%s、%s、%s、%s、%s、%s、%s、%s、%s、%s、%s、%s、%s、%s、%s、%s、%s、%s、%s、%s、%。(材料a[0]、材料c[0]、材料d[0]、材料e[0]、材料f[0]、材料g[0]、材料h[0])
    秩=mat_a[1]*mat_b[1]*mat_c[1]*mat_d[1]*mat_e[1]*mat_f[1]*mat_h[1]*mat_i[1]*mat_j[1]
    总计。追加([条目,排名])
    总排名追加(总计)
    计数+=1
    打印“总组合数:%d”%count
    打印“排序组合”
    总计=已排序(总计,键=λx:x[1])
    打印“下注金额:%f”%Bet\u金额
    对于计数,枚举中的总计(总计):
    如果计数小于100:
    打印“%s->%s%”(总计[0],总计[1]*下注金额)
    其他:
    打破
    
    您应该看看。此外,您的
    total_rank
    列表似乎毫无意义(请注意,它将反复包含对同一列表的许多引用),而
    count
    只是结果列表的
    len

    import itertools, operator
    matches = [match_a, match_b, match_c, match_d, match_e, match_f, match_g, match_h, match_i, match_j]
    
    for mat_x in itertools.product(*matches):
        entry = ", ".join(x[0] for x in mat_x)
        rank = reduce(operator.mul, (x[1] for x in mat_x))
        totals.append([entry, rank])
    
    print 'Total Combinations : %d ' % len(totals)
    
    您甚至可以将其列为一个列表:

    totals = [(", ".join(x[0] for x in mat_x), reduce(operator.mul, (x[1] for x in mat_x))) 
              for mat_x in itertools.product(*matches)]