Peter Chen - 1 year ago 57
Python Question

Use two for loops in python

I have a dictionary, which records how many drum beats in every measure:

``````{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}
``````

For example, there is a drum beat in first measure, and there are 16 drum beats in 46th measure.

Then I have another dictionary, which records the score(0~3) the player plays in every drum beat:

``````{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}
``````

For example, The player got three points in first drum beat, and got three points in 269th drum beat.

Then I need to calculate the average score in every measure the player got.
For example, the player got 3/1 = 3 points(average) in first measure, and got (3+3)/2 = 3(average) in second measure.

I know I need to use two for loops, but I don't know how to start with, can anyone help me? Thanks

More examples:

``````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.
``````

``````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
``````

Average_result is what you want.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download