1

I have a dictionary with multiple key value pairs resembling this:

sampling_strategy = {1259: 302, 568: 245, 698: 132, 534: 126, 971: 112, 446: 103, 614: 102, 860: 97, 989: 96, 1037: 89, 1309: 82, 1251: 76, 1339: 76, 556: 74, 1245: 68, 502: 67, 1429: 61, 985: 58, 1212: 58, 953: 52, 13: 51, 1256: 49, 1002: 47, 979: 45, 185: 43, 961: 43, 841: 41, 1697: 41, 510: 37, 994: 37, 1421: 37, 747: 36, 1218: 36, 1451: 36, 1201: 35, 1248: 35, 1449: 34, 982: 33, 1435: 33, 579: 32, 1342: 32, 1425: 31, 487: 29, 1044: 29, 583: 28, 682: 28, 1031: 28, 1200: 28, 207: 27, 236: 27, 891: 27, 964: 27, 1185: 27, 1221: 27, 1283: 26, 1234: 25, 1452: 25, 1555: 25, 582: 24, 1715: 24, 225: 23, 1027: 23, 1464: 23, 999: 22, 1237: 22, 1595: 22, 27: 21, 211: 21, 754: 21, 969: 21, 1568: 21, 193: 20, 787: 20, 959: 20, 1196: 20, 581: 19, 216: 18, 505: 18, 752: 18, 956: 18, 1282: 18, 1392: 18, 70: 17, 621: 17, 966: 17, 1560: 17, 40: 16, 51: 16, 1413: 16, 1773: 16, 60: 15, 83: 15, 627: 15, 1444: 15, 1631: 15, 1703: 15, 1716: 15, 110: 14, 214: 14, 626: 14, 894: 14, 1022: 14, 1552: 14, 1646: 14, 624: 13, 1396: 13, 1450: 13, 1642: 13, 1684: 13, 495: 12, 965: 12, 1034: 12, 1397: 12, 1448: 12, 1553: 12, 94: 11, 117: 11, 218: 11, 753: 11, 1238: 11, 1554: 11, 1593: 11, 1774: 11, 98: 10, 191: 10, 790: 10, 1336: 10, 1378: 10, 1491: 10, 1627: 10, 1644: 10, 250: 10, 492: 10, 530: 10, 1020: 10, 1047: 10, 1130: 10, 1406: 10, 1457: 10, 1693: 10, 1696: 10, 107: 10, 194: 10, 657: 10, 842: 10, 892: 10, 1260: 10, 1479: 10, 1682: 10, 1686: 8, 1771: 8, 4: 7, 23: 7, 501: 7, 655: 7, 788: 7, 893: 7, 908: 7, 1029: 7, 1108: 7, 1147: 7, 1307: 7, 1355: 7, 1363: 7, 1456: 7, 1465: 7, 1666: 7, 1669: 7, 1685: 7, 1690: 7, 124: 6, 201: 6, 683: 6, 1016: 6, 1058: 6, 1113: 6, 1117: 6, 1124: 6, 1135: 6, 1140: 6, 1270: 6, 1271: 6, 1471: 6, 1578: 6, 1639: 6, 1647: 6, 1659: 6, 0: 5, 168: 5, 246: 5, 319: 5, 433: 5, 447: 5, 676: 5, 1033: 5, 1070: 5, 1112: 5, 1167: 5, 1213: 5, 1268: 5, 1286: 5, 1315: 5, 1369: 5, 1394: 5, 1461: 5, 1463: 5, 1531: 5, 1594: 5, 1623: 5, 1628: 5, 1635: 5, 1663: 5, 1689: 5, 1695: 5, 1751: 5, 183: 4, 208: 4, 357: 4, 672: 4, 691: 4, 694: 4, 700: 4, 806: 4, 853: 4, 912: 4, 929: 4, 974: 4, 1023: 4, 1028: 4, 1050: 4, 1084: 4, 1136: 4, 1157: 4, 1215: 4, 1269: 4, 1279: 4, 1324: 4, 1403: 4, 1475: 4, 1529: 4, 1532: 4, 1564: 4, 1566: 4, 1589: 4, 1603: 4, 1615: 4, 1617: 4, 1626: 4, 1630: 4, 1632: 4, 1668: 4, 1679: 4, 1687: 4, 1692: 4, 1694: 4, 1720: 4, 1: 3, 8: 3, 14: 3, 33: 3, 45: 3, 192: 3, 197: 3, 205: 3, 215: 3, 321: 3, 340: 3, 354: 3, 367: 3, 409: 3, 451: 3, 667: 3, 674: 3, 677: 3, 699: 3, 708: 3, 771: 3, 789: 3, 807: 3, 830: 3, 897: 3, 970: 3, 990: 3, 1008: 3, 1074: 3, 1105: 3, 1109: 3, 1131: 3, 1141: 3, 1144: 3, 1151: 3, 1235: 3, 1263: 3, 1281: 3, 1298: 3, 1313: 3, 1337: 3, 1347: 3, 1385: 3, 1411: 3, 1414: 3, 1417: 3, 1420: 3, 1454: 3, 1455: 3, 1563: 3, 1579: 3, 1588: 3, 1638: 3, 1652: 3, 1662: 3, 1671: 3, 1680: 3, 1691: 3, 1699: 3, 1702: 3, 1710: 3, 1714: 3, 1742: 3, 1746: 3, 1772: 3, 1775: 3, 10: 2, 15: 2, 32: 2, 39: 2, 41: 2, 46: 2, 47: 2, 48: 2, 64: 2, 65: 2, 67: 2, 81: 2, 114: 2, 115: 2, 118: 2, 120: 2, 125: 2, 126: 2, 139: 2, 154: 2, 158: 2, 164: 2, 169: 2, 173: 2, 182: 2, 186: 2, 190: 2, 196: 2, 202: 2, 219: 2, 223: 2, 240: 2, 243: 2, 244: 2, 249: 2, 253: 2, 266: 2, 268: 2, 291: 2, 294: 2, 297: 2, 304: 2, 310: 2, 311: 2, 314: 2, 315: 2, 316: 2, 320: 2, 322: 2, 329: 2, 349: 2, 350: 2, 351: 2, 355: 2, 356: 2, 364: 2, 366: 2, 372: 2, 374: 2, 375: 2, 377: 2, 381: 2, 383: 2, 426: 2, 429: 2, 431: 2, 444: 2, 449: 2, 450: 2, 452: 2, 462: 2, 464: 2, 488: 2, 490: 2, 493: 2, 507: 2, 546: 2, 549: 2, 575: 2, 597: 2, 602: 2, 610: 2, 620: 2, 663: 2, 670: 2, 679: 2, 681: 2, 693: 2, 703: 2, 707: 2, 712: 2, 725: 2, 732: 2, 759: 2, 792: 2, 797: 2, 799: 2, 804: 2, 813: 2, 832: 2, 838: 2, 840: 2, 857: 2, 858: 2, 865: 2, 867: 2, 875: 2, 919: 2, 923: 2, 927: 2, 932: 2, 939: 2, 950: 2, 954: 2, 960: 2, 962: 2, 977: 2, 983: 2, 991: 2, 1004: 2, 1009: 2, 1030: 2, 1049: 2, 1052: 2, 1072: 2, 1081: 2, 1087: 2, 1090: 2, 1094: 2, 1106: 2, 1110: 2, 1121: 2, 1126: 2, 1139: 2, 1146: 2, 1152: 2, 1155: 2, 1158: 2, 1163: 2, 1166: 2, 1172: 2, 1177: 2, 1187: 2, 1197: 2, 1199: 2, 1243: 2, 1253: 2, 1258: 2, 1265: 2, 1266: 2, 1273: 2, 1276: 2, 1284: 2, 1288: 2, 1290: 2, 1293: 2, 1302: 2, 1304: 2, 1321: 2, 1357: 2, 1362: 2, 1372: 2, 1380: 2, 1381: 60, 1382: 60, 1384: 60, 1399: 60, 1410: 60, 1412: 60, 1469: 60, 1470: 60, 1472: 60, 1478: 60, 1494: 60, 1506: 60, 1509: 60, 1520: 60, 1547: 60, 1556: 60, 1558: 60, 1580: 60, 1581: 60, 1586: 60, 1592: 60, 1604: 60, 1606: 60, 1611: 60, 1613: 60, 1614: 60, 1622: 60, 1625: 60, 1640: 60, 1654: 60, 1665: 60, 1704: 60, 1731: 60, 1738: 60, 1743: 60, 1749: 60, 1767: 60, 2: 50, 3: 50, 5: 50, 6: 50, 7: 50, 9: 50, 12: 50, 17: 50, 18: 50, 19: 50, 20: 50, 25: 50, 26: 50, 29: 50, 30: 1, 31: 1, 34: 1, 36: 1, 37: 1, 38: 1, 42: 1, 43: 1, 44: 1, 50: 1, 52: 1, 53: 1, 55: 1, 56: 1, 57: 1, 58: 1, 59: 1, 61: 1, 62: 1, 63: 1, 66: 1, 68: 1, 71: 1, 72: 1, 75: 1, 76: 1, 77: 1, 78: 1, 79: 1, 80: 1, 82: 1, 84: 1, 87: 1, 88: 1, 89: 1, 90: 1, 91: 1, 93: 1, 95: 1, 96: 1, 97: 1, 99: 1, 100: 1, 101: 1, 102: 1, 103: 1, 104: 1, 105: 1, 106: 1, 108: 1, 109: 1, 111: 1, 113: 1, 122: 1, 123: 1, 128: 1, 129: 1, 130: 1, 131: 1, 133: 1, 135: 1, 136: 1, 138: 1, 140: 1, 143: 1, 144: 1, 145: 1, 146: 1, 147: 1, 148: 1, 150: 1, 151: 1, 152: 1, 153: 1, 156: 1, 159: 1, 160: 1, 163: 1, 166: 1, 167: 1, 170: 1, 171: 1, 172: 1, 174: 1, 175: 1, 177: 1, 178: 1, 179: 1, 180: 1, 181: 1, 184: 1, 187: 1, 188: 1, 189: 1, 195: 1, 198: 1, 199: 1, 200: 1, 204: 1, 206: 1, 210: 1, 212: 1, 213: 1, 217: 1, 220: 1, 221: 1, 222: 1, 226: 1, 227: 1, 229: 1, 230: 1, 231: 1, 233: 1, 235: 1, 237: 1, 238: 1, 239: 1, 241: 1, 242: 1, 245: 1, 248: 1, 251: 1, 252: 1, 254: 1, 255: 1, 256: 1, 258: 1, 259: 1, 260: 1, 261: 1, 263: 1, 265: 1, 269: 1, 270: 1, 271: 1, 273: 1, 275: 1, 276: 1, 277: 1, 278: 1, 279: 1, 280: 1, 281: 1, 282: 1, 283: 1, 285: 1, 286: 1, 287: 1, 288: 1, 289: 1, 292: 1, 296: 1, 299: 1, 300: 1, 301: 1, 302: 1, 303: 1, 305: 1, 306: 1, 307: 1, 308: 1, 309: 1, 312: 1, 313: 1, 318: 1, 323: 1, 324: 1, 325: 1, 326: 1, 327: 1, 328: 1, 330: 1, 331: 1, 332: 1, 333: 1, 334: 1, 335: 1, 336: 1, 337: 1, 338: 1, 339: 1, 341: 1, 342: 1, 345: 1, 347: 1, 353: 1, 358: 1, 361: 1, 362: 1, 363: 1, 365: 1, 368: 1, 369: 1, 373: 1, 376: 1, 379: 1, 380: 1, 382: 1, 385: 1, 388: 1, 390: 1, 395: 1, 396: 1, 397: 1, 399: 1, 400: 1, 401: 1, 402: 1, 403: 1, 404: 1, 405: 1, 406: 1, 407: 1, 408: 1, 412: 1, 413: 1, 414: 1, 415: 1, 416: 1, 418: 1, 420: 1, 421: 1, 423: 1, 425: 1, 427: 1, 428: 1, 430: 1, 432: 1, 436: 1, 437: 1, 438: 1, 440: 1, 441: 1, 442: 1, 445: 1, 454: 1, 455: 1, 456: 1, 457: 1, 458: 1, 459: 1, 460: 1, 461: 1, 463: 1, 466: 1, 467: 1, 468: 1, 469: 1, 470: 1, 471: 1, 472: 1, 474: 1, 475: 1, 476: 1, 477: 1, 478: 1, 479: 1, 480: 1, 481: 1, 482: 1, 483: 1, 485: 1, 486: 1, 489: 1, 494: 1, 496: 1, 497: 1, 499: 1, 500: 1, 503: 1, 504: 1, 506: 1, 508: 1, 509: 1, 512: 1, 513: 1, 514: 1, 516: 1, 517: 1, 519: 1, 520: 1, 522: 1, 523: 1, 524: 1, 525: 1, 527: 1, 529: 1, 531: 1, 532: 1, 535: 1, 536: 1, 537: 1, 539: 1, 540: 1, 541: 1, 542: 1, 543: 1, 544: 1, 545: 1, 547: 1, 550: 1, 551: 1, 552: 1, 553: 1, 554: 1, 557: 1, 558: 1, 559: 1, 560: 1, 561: 1, 563: 1, 564: 1, 566: 1, 567: 1, 569: 1, 570: 1, 571: 1, 572: 1, 576: 1}`

I want to replace all the pairs where the value is 1 to 50 and all where the value is 2 to 60 and so on: My resulting dict should look like {572:50, 576:50} for the last two values. Is there a way to do this without having to manually change each value? I want to change all values when value<= 10 to a higher value.

5
  • 1
    ericlippert.com/2014/03/05/how-to-debug-small-programs Commented Feb 11, 2019 at 20:39
  • what happens to {1259: 302} ? Commented Feb 11, 2019 at 20:40
  • @ParitoshSingh: that remains as it is.. so I only want to change the values of keys which have value <= 10; Commented Feb 11, 2019 at 20:41
  • What do you mean by "and so on"? would 3 be 70 and 4 be 80? Commented Feb 11, 2019 at 20:42
  • @liamhawkins: sorry for the wrong wording ... so I want to replace all values where values <=10; .. so when value = 1, I want to make it 50, 2 should be 60, 3 should be 70... Commented Feb 11, 2019 at 20:43

2 Answers 2

4

This can be done by looping through the dictionary with .items()

for key, value in sampling_strategy.items():
     if value <= 10:
         sampling_strategy[key] = 40 + value * 10
Sign up to request clarification or add additional context in comments.

2 Comments

Use "for key, value in sampling_strategy.iteritems()" for python 2.7
.items() works in 2.7 unless you need an iterator specifically
1

How to do it with a dictionary comprehension:

new_dict = {key: value * 10 + 40 if value <= 10 else key: value for (key, value) in sampling_strategy.items()}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.