Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.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中使用两个for循环_Python_For Loop - Fatal编程技术网

在python中使用两个for循环

在python中使用两个for循环,python,for-loop,Python,For Loop,我有一本字典,它记录了每一小节的击鼓次数: {0: 1, 1: 2, 2: 2, 3: 2, 4: 2, 5: 3, 6: 2, 7: 2, 8: 2, 9: 2, 10: 4, 11: 2, 12: 3, 13: 4, 14: 4, 15: 3, 16: 5, 17: 5, 18: 6, 19: 4, 20: 4, 21: 5, 22: 4, 23: 6, 24: 6, 25: 6, 26: 7, 27: 6, 28: 7, 29: 4, 30: 7, 31: 8, 32: 6, 33: 7,

我有一本字典,它记录了每一小节的击鼓次数:

{0: 1, 1: 2, 2: 2, 3: 2, 4: 2, 5: 3, 6: 2, 7: 2, 8: 2, 9: 2, 10: 4, 11: 2, 12: 3, 13: 4, 14: 4, 15: 3, 16: 5, 17: 5, 18: 6, 19: 4, 20: 4, 21: 5, 22: 4, 23: 6, 24: 6, 25: 6, 26: 7, 27: 6, 28: 7, 29: 4, 30: 7, 31: 8, 32: 6, 33: 7, 34: 8, 35: 8, 36: 9, 37: 8, 38: 12, 39: 7, 40: 10, 41: 8, 42: 14, 43: 13, 44: 13, 45: 16}
例如,第一小节有一个鼓点,第46小节有16个鼓点

然后我有另一本字典,它记录了演奏者在每个鼓点上演奏的乐谱(0~3):

{0: 3, 1: 3, 2: 3, 3: 3, 4: 3, 5: 1, 6: 2, 7: 3, 8: 2, 9: 2, 10: 1, 11: 1, 12: 0, 13: 3, 14: 3, 15: 1, 16: 1, 17: 3, 18: 1, 19: 3, 20: 1, 21: 3, 22: 1, 23: 3, 24: 3, 25: 2, 26: 2, 27: 2, 28: 2, 29: 2, 30: 3, 31: 3, 32: 2, 33: 2, 34: 3, 35: 1, 36: 1, 37: 3, 38: 0, 39: 1, 40: 2, 41: 3, 42: 3, 43: 0, 44: 2, 45: 2, 46: 1, 47: 2, 48: 1, 49: 1, 50: 1, 51: 2, 52: 1, 53: 2, 54: 3, 55: 1, 56: 1, 57: 0, 58: 0, 59: 3, 60: 2, 61: 0, 62: 0, 63: 1, 64: 0, 65: 2, 66: 1, 67: 1, 68: 1, 69: 0, 70: 3, 71: 3, 72: 3, 73: 2, 74: 1, 75: 1, 76: 1, 77: 1, 78: 1, 79: 2, 80: 2, 81: 2, 82: 2, 83: 0, 84: 0, 85: 1, 86: 0, 87: 1, 88: 2, 89: 3, 90: 1, 91: 2, 92: 2, 93: 3, 94: 3, 95: 2, 96: 1, 97: 0, 98: 3, 99: 3, 100: 1, 101: 0, 102: 3, 103: 3, 104: 1, 105: 3, 106: 0, 107: 2, 108: 0, 109: 1, 110: 0, 111: 1, 112: 0, 113: 2, 114: 0, 115: 2, 116: 0, 117: 1, 118: 3, 119: 1, 120: 3, 121: 3, 122: 3, 123: 1, 124: 1, 125: 0, 126: 2, 127: 0, 128: 3, 129: 3, 130: 1, 131: 0, 132: 3, 133: 2, 134: 1, 135: 1, 136: 0, 137: 1, 138: 0, 139: 3, 140: 3, 141: 1, 142: 2, 143: 3, 144: 1, 145: 2, 146: 0, 147: 0, 148: 0, 149: 1, 150: 0, 151: 3, 152: 0, 153: 1, 154: 0, 155: 3, 156: 2, 157: 3, 158: 1, 159: 0, 160: 1, 161: 1, 162: 1, 163: 2, 164: 3, 165: 0, 166: 1, 167: 1, 168: 2, 169: 0, 170: 1, 171: 0, 172: 1, 173: 1, 174: 1, 175: 0, 176: 1, 177: 1, 178: 2, 179: 3, 180: 0, 181: 1, 182: 3, 183: 0, 184: 3, 185: 0, 186: 1, 187: 0, 188: 1, 189: 2, 190: 0, 191: 1, 192: 0, 193: 0, 194: 0, 195: 2, 196: 1, 197: 0, 198: 2, 199: 1, 200: 0, 201: 2, 202: 2, 203: 0, 204: 1, 205: 3, 206: 1, 207: 0, 208: 0, 209: 1, 210: 0, 211: 0, 212: 0, 213: 3, 214: 0, 215: 1, 216: 1, 217: 0, 218: 3, 219: 0, 220: 2, 221: 0, 222: 3, 223: 1, 224: 0, 225: 3, 226: 0, 227: 0, 228: 1, 229: 0, 230: 2, 231: 1, 232: 0, 233: 3, 234: 0, 235: 2, 236: 1, 237: 0, 238: 2, 239: 3, 240: 3, 241: 0, 242: 1, 243: 0, 244: 2, 245: 0, 246: 2, 247: 0, 248: 1, 249: 0, 250: 0, 251: 1, 252: 0, 253: 1, 254: 0, 255: 1, 256: 1, 257: 0, 258: 0, 259: 1, 260: 1, 261: 0, 262: 1, 263: 0, 264: 1, 265: 0, 266: 3, 267: 0, 268: 3}
例如,玩家在第一次击鼓中得了三分,在第269次击鼓中得了三分

然后,我需要计算球员在每一项测试中的平均得分。 例如,玩家在第一次测量中得到3/1=3分(平均值),在第二次测量中得到(3+3)/2=3分(平均值)

我知道我需要使用两个for循环,但我不知道如何开始,有人能帮我吗?谢谢

更多示例:

The average score in first measure the player got is 3/1 = 3,
The average score in second measure the player got is (3+3)/2 = 3,
The average score in third  measure the player got is (3+3)/2 = 3,
The average score in fourth measure the player got is (1+2)/2 = 1.5,
The average score in fifth  measure the player got is (3+2)/2 = 2.5,
The average score in sixth  measure the player got is (2+1+1)/3 = 1.33,
The average score in seventh measure the player got is (0+3)/2 = 1.5,
The average score in eighth  measure the player got is (3+1)/2 = 2
,etc.
beatCounter会记录你在乐谱中的位置

Num_NumOfBeats_Mapping = {0: 1, 1: 2, 2: 2, 3: 2, 4: 2, 5: 3, 6: 2, 7: 2, 8: 2, 9: 2, 10: 4, 11: 2, 12: 3, 13: 4, 14: 4, 15: 3, 16: 5, 17: 5, 18: 6, 19: 4, 20: 4, 21: 5, 22: 4, 23: 6, 24: 6, 25: 6, 26: 7, 27: 6, 28: 7, 29: 4, 30: 7, 31: 8, 32: 6, 33: 7, 34: 8, 35: 8, 36: 9, 37: 8, 38: 12, 39: 7, 40: 10, 41: 8, 42: 14, 43: 13, 44: 13, 45: 16}
result = {0: 3, 1: 3, 2: 3, 3: 3, 4: 3, 5: 1, 6: 2, 7: 3, 8: 2, 9: 2, 10: 1, 11: 1, 12: 0, 13: 3, 14: 3, 15: 1, 16: 1, 17: 3, 18: 1, 19: 3, 20: 1, 21: 3, 22: 1, 23: 3, 24: 3, 25: 2, 26: 2, 27: 2, 28: 2, 29: 2, 30: 3, 31: 3, 32: 2, 33: 2, 34: 3, 35: 1, 36: 1, 37: 3, 38: 0, 39: 1, 40: 2, 41: 3, 42: 3, 43: 0, 44: 2, 45: 2, 46: 1, 47: 2, 48: 1, 49: 1, 50: 1, 51: 2, 52: 1, 53: 2, 54: 3, 55: 1, 56: 1, 57: 0, 58: 0, 59: 3, 60: 2, 61: 0, 62: 0, 63: 1, 64: 0, 65: 2, 66: 1, 67: 1, 68: 1, 69: 0, 70: 3, 71: 3, 72: 3, 73: 2, 74: 1, 75: 1, 76: 1, 77: 1, 78: 1, 79: 2, 80: 2, 81: 2, 82: 2, 83: 0, 84: 0, 85: 1, 86: 0, 87: 1, 88: 2, 89: 3, 90: 1, 91: 2, 92: 2, 93: 3, 94: 3, 95: 2, 96: 1, 97: 0, 98: 3, 99: 3, 100: 1, 101: 0, 102: 3, 103: 3, 104: 1, 105: 3, 106: 0, 107: 2, 108: 0, 109: 1, 110: 0, 111: 1, 112: 0, 113: 2, 114: 0, 115: 2, 116: 0, 117: 1, 118: 3, 119: 1, 120: 3, 121: 3, 122: 3, 123: 1, 124: 1, 125: 0, 126: 2, 127: 0, 128: 3, 129: 3, 130: 1, 131: 0, 132: 3, 133: 2, 134: 1, 135: 1, 136: 0, 137: 1, 138: 0, 139: 3, 140: 3, 141: 1, 142: 2, 143: 3, 144: 1, 145: 2, 146: 0, 147: 0, 148: 0, 149: 1, 150: 0, 151: 3, 152: 0, 153: 1, 154: 0, 155: 3, 156: 2, 157: 3, 158: 1, 159: 0, 160: 1, 161: 1, 162: 1, 163: 2, 164: 3, 165: 0, 166: 1, 167: 1, 168: 2, 169: 0, 170: 1, 171: 0, 172: 1, 173: 1, 174: 1, 175: 0, 176: 1, 177: 1, 178: 2, 179: 3, 180: 0, 181: 1, 182: 3, 183: 0, 184: 3, 185: 0, 186: 1, 187: 0, 188: 1, 189: 2, 190: 0, 191: 1, 192: 0, 193: 0, 194: 0, 195: 2, 196: 1, 197: 0, 198: 2, 199: 1, 200: 0, 201: 2, 202: 2, 203: 0, 204: 1, 205: 3, 206: 1, 207: 0, 208: 0, 209: 1, 210: 0, 211: 0, 212: 0, 213: 3, 214: 0, 215: 1, 216: 1, 217: 0, 218: 3, 219: 0, 220: 2, 221: 0, 222: 3, 223: 1, 224: 0, 225: 3, 226: 0, 227: 0, 228: 1, 229: 0, 230: 2, 231: 1, 232: 0, 233: 3, 234: 0, 235: 2, 236: 1, 237: 0, 238: 2, 239: 3, 240: 3, 241: 0, 242: 1, 243: 0, 244: 2, 245: 0, 246: 2, 247: 0, 248: 1, 249: 0, 250: 0, 251: 1, 252: 0, 253: 1, 254: 0, 255: 1, 256: 1, 257: 0, 258: 0, 259: 1, 260: 1, 261: 0, 262: 1, 263: 0, 264: 1, 265: 0, 266: 3, 267: 0, 268: 3}
sum = 0
start_value = 0
for n in range(len(Num_NumOfBeats_Mapping)):
    i = Num_NumOfBeats_Mapping[n]
    for s in range(start_value,start_value+i):
        sum += result[s]
    Average_result.append(sum/i)
    sum = 0
    start_value += i
print Average_result
beatCounter会记录你在乐谱中的位置

Num_NumOfBeats_Mapping = {0: 1, 1: 2, 2: 2, 3: 2, 4: 2, 5: 3, 6: 2, 7: 2, 8: 2, 9: 2, 10: 4, 11: 2, 12: 3, 13: 4, 14: 4, 15: 3, 16: 5, 17: 5, 18: 6, 19: 4, 20: 4, 21: 5, 22: 4, 23: 6, 24: 6, 25: 6, 26: 7, 27: 6, 28: 7, 29: 4, 30: 7, 31: 8, 32: 6, 33: 7, 34: 8, 35: 8, 36: 9, 37: 8, 38: 12, 39: 7, 40: 10, 41: 8, 42: 14, 43: 13, 44: 13, 45: 16}
result = {0: 3, 1: 3, 2: 3, 3: 3, 4: 3, 5: 1, 6: 2, 7: 3, 8: 2, 9: 2, 10: 1, 11: 1, 12: 0, 13: 3, 14: 3, 15: 1, 16: 1, 17: 3, 18: 1, 19: 3, 20: 1, 21: 3, 22: 1, 23: 3, 24: 3, 25: 2, 26: 2, 27: 2, 28: 2, 29: 2, 30: 3, 31: 3, 32: 2, 33: 2, 34: 3, 35: 1, 36: 1, 37: 3, 38: 0, 39: 1, 40: 2, 41: 3, 42: 3, 43: 0, 44: 2, 45: 2, 46: 1, 47: 2, 48: 1, 49: 1, 50: 1, 51: 2, 52: 1, 53: 2, 54: 3, 55: 1, 56: 1, 57: 0, 58: 0, 59: 3, 60: 2, 61: 0, 62: 0, 63: 1, 64: 0, 65: 2, 66: 1, 67: 1, 68: 1, 69: 0, 70: 3, 71: 3, 72: 3, 73: 2, 74: 1, 75: 1, 76: 1, 77: 1, 78: 1, 79: 2, 80: 2, 81: 2, 82: 2, 83: 0, 84: 0, 85: 1, 86: 0, 87: 1, 88: 2, 89: 3, 90: 1, 91: 2, 92: 2, 93: 3, 94: 3, 95: 2, 96: 1, 97: 0, 98: 3, 99: 3, 100: 1, 101: 0, 102: 3, 103: 3, 104: 1, 105: 3, 106: 0, 107: 2, 108: 0, 109: 1, 110: 0, 111: 1, 112: 0, 113: 2, 114: 0, 115: 2, 116: 0, 117: 1, 118: 3, 119: 1, 120: 3, 121: 3, 122: 3, 123: 1, 124: 1, 125: 0, 126: 2, 127: 0, 128: 3, 129: 3, 130: 1, 131: 0, 132: 3, 133: 2, 134: 1, 135: 1, 136: 0, 137: 1, 138: 0, 139: 3, 140: 3, 141: 1, 142: 2, 143: 3, 144: 1, 145: 2, 146: 0, 147: 0, 148: 0, 149: 1, 150: 0, 151: 3, 152: 0, 153: 1, 154: 0, 155: 3, 156: 2, 157: 3, 158: 1, 159: 0, 160: 1, 161: 1, 162: 1, 163: 2, 164: 3, 165: 0, 166: 1, 167: 1, 168: 2, 169: 0, 170: 1, 171: 0, 172: 1, 173: 1, 174: 1, 175: 0, 176: 1, 177: 1, 178: 2, 179: 3, 180: 0, 181: 1, 182: 3, 183: 0, 184: 3, 185: 0, 186: 1, 187: 0, 188: 1, 189: 2, 190: 0, 191: 1, 192: 0, 193: 0, 194: 0, 195: 2, 196: 1, 197: 0, 198: 2, 199: 1, 200: 0, 201: 2, 202: 2, 203: 0, 204: 1, 205: 3, 206: 1, 207: 0, 208: 0, 209: 1, 210: 0, 211: 0, 212: 0, 213: 3, 214: 0, 215: 1, 216: 1, 217: 0, 218: 3, 219: 0, 220: 2, 221: 0, 222: 3, 223: 1, 224: 0, 225: 3, 226: 0, 227: 0, 228: 1, 229: 0, 230: 2, 231: 1, 232: 0, 233: 3, 234: 0, 235: 2, 236: 1, 237: 0, 238: 2, 239: 3, 240: 3, 241: 0, 242: 1, 243: 0, 244: 2, 245: 0, 246: 2, 247: 0, 248: 1, 249: 0, 250: 0, 251: 1, 252: 0, 253: 1, 254: 0, 255: 1, 256: 1, 257: 0, 258: 0, 259: 1, 260: 1, 261: 0, 262: 1, 263: 0, 264: 1, 265: 0, 266: 3, 267: 0, 268: 3}
sum = 0
start_value = 0
for n in range(len(Num_NumOfBeats_Mapping)):
    i = Num_NumOfBeats_Mapping[n]
    for s in range(start_value,start_value+i):
        sum += result[s]
    Average_result.append(sum/i)
    sum = 0
    start_value += i
print Average_result
平均结果就是你想要的


平均结果就是你想要的。

对于第三个度量,它将是(3+3+3)/2,对吗?你需要更好地解释问题,添加更多的例子对于第三个度量,它将是(3+3)/2为什么?这背后的逻辑是什么?你期望差距吗?或者为什么你使用字典而不是简单的列表?对于第三个度量,它将是(3+3+3)/2,对吗?你需要更好地解释问题,添加更多的例子对于第三个度量,它将是(3+3)/2为什么?这背后的逻辑是什么?你期望差距吗?或者为什么你使用字典而不是简单的列表?结果不是我想要的,我想要的结果是玩家在每个测量中得到的平均分数。例如,玩家在第一个度量中得到的平均分数是分数[0]/拍[0]=3,在第二个度量中得到的平均分数是(分数[1]+分数[2]/拍[1]=3,在第三个度量中得到的平均分数是(分数[3]+分数[4]/拍[2]=3,在第四个度量中得到的平均分数是(分数[1+2]=1.5)((分数[5]+分数)[6] )/beats[3]),这意味着分子的计数是分母。该代码会打印出每个度量中的平均分数。您运行代码了吗?结果不是我想要的,我想要的结果是玩家在每个度量中得到的平均分数。例如,玩家在第一个度量中得到的平均分数是分数[0]/beats[0]=3,玩家在第二个测量中得到的平均分数是(分数[1]+分数[2])/beats[1]=3,玩家在第三个测量中得到的平均分数是(分数[3]+分数[4])/beats[2]=3,玩家在第四个测量中得到的平均分数是(分数[5]+分数[6])/beats[3]),这意味着分子的计数是分母。该代码会打印出每个度量的平均分数。您运行该代码了吗?