|
440 | 440 | },
|
441 | 441 | {
|
442 | 442 | "cell_type": "code",
|
443 |
| - "execution_count": null, |
| 443 | + "execution_count": 76, |
444 | 444 | "metadata": {},
|
445 |
| - "outputs": [], |
| 445 | + "outputs": [ |
| 446 | + { |
| 447 | + "ename": "TypeError", |
| 448 | + "evalue": "list indices must be integers or slices, not str", |
| 449 | + "output_type": "error", |
| 450 | + "traceback": [ |
| 451 | + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", |
| 452 | + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", |
| 453 | + "Cell \u001b[1;32mIn[76], line 74\u001b[0m\n\u001b[0;32m 71\u001b[0m \u001b[39mreturn\u001b[39;00m diccionario_par\n\u001b[0;32m 72\u001b[0m \u001b[39mreturn\u001b[39;00m res\n\u001b[1;32m---> 74\u001b[0m OrdenarDiccionario(dicc, \u001b[39m'\u001b[39;49m\u001b[39mclave1\u001b[39;49m\u001b[39m'\u001b[39;49m) \n", |
| 454 | + "Cell \u001b[1;32mIn[76], line 68\u001b[0m, in \u001b[0;36mOrdenarDiccionario\u001b[1;34m(diccionario_par, clave, descendente)\u001b[0m\n\u001b[0;32m 66\u001b[0m \u001b[39mfor\u001b[39;00m i \u001b[39min\u001b[39;00m \u001b[39mrange\u001b[39m(\u001b[39m0\u001b[39m, cantidadValores):\n\u001b[0;32m 67\u001b[0m newArr2\u001b[39m.\u001b[39mappend(newArrSorted[i][key])\n\u001b[1;32m---> 68\u001b[0m res[key] \u001b[39m=\u001b[39m newArr2\n\u001b[0;32m 69\u001b[0m \u001b[39m#Si no tienen valores devolvemos el mismo diccionario:\u001b[39;00m\n\u001b[0;32m 70\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[0;32m 71\u001b[0m \u001b[39mreturn\u001b[39;00m diccionario_par\n", |
| 455 | + "\u001b[1;31mTypeError\u001b[0m: list indices must be integers or slices, not str" |
| 456 | + ] |
| 457 | + } |
| 458 | + ], |
446 | 459 | "source": [
|
447 |
| - "dicc = {\n", |
448 |
| - " 'clave1':['c','a','b'],\n", |
449 |
| - " 'clave2':['casa','auto','barco'],\n", |
450 |
| - " 'clave3':[1,2,3]\n", |
451 |
| - " }\n", |
452 | 460 | "def OrdenarDiccionario(diccionario_par, clave, descendente=True):\n",
|
453 | 461 | " '''\n",
|
454 | 462 | " Esta función recibe como parámetro un diccionario, cuyas listas de valores tienen el mismo\n",
|
|
493 | 501 | "\n",
|
494 | 502 | " #print (len(diccionario_par[list(diccionario_par.keys())[0]]))\n",
|
495 | 503 | " # chequeamos qué valor estamos reciendo\n",
|
496 |
| - " print()\n", |
| 504 | + " #print()\n", |
497 | 505 | " if len(diccionario_par[list(diccionario_par.keys())[0]])>0: \n",
|
498 | 506 | " cantidadValores=len(diccionario_par[list(diccionario_par.keys())[0]])\n",
|
499 | 507 | " # convertimos el diccionario de listas, a una lista de diccionarios:\t\n",
|
|
502 | 510 | " for key in diccionario_par:\n",
|
503 | 511 | " newObj[key]=diccionario_par[key][i]\n",
|
504 | 512 | " newArr.append(newObj)\n",
|
505 |
| - " #print(newArr) Aquí se generó una lista de diccionarios\n", |
506 |
| - " # Ahora ordenemos\n", |
507 |
| - " newArrSorted = sorted(newArr, key=lambda d: d[clave], reverse=not (descendente))\n", |
508 |
| - " print (newArrSorted) #verificar contenido del nuevo arreglo\n", |
509 |
| - " # Convertimos la lista de diccionarios, en un diccionario de listas:\n", |
510 |
| - " for key in diccionario_par:\n", |
511 |
| - " newArr2 = []\n", |
512 |
| - " for i in range(0, cantidadValores):\n", |
513 |
| - " newArr2.append(newArrSorted[i][key])\n", |
514 |
| - " res[key] = newArr2\n", |
| 513 | + " #print(newArr) Aquí se generó una lista de diccionarios\n", |
| 514 | + " # Ahora ordenemos\n", |
| 515 | + " newArrSorted = sorted(newArr, key=lambda d: d[clave], reverse=not (descendente))\n", |
| 516 | + " # print (newArrSorted) #verificar contenido del nuevo arreglo\n", |
| 517 | + " # Convertimos la lista de diccionarios, en un diccionario de listas:\n", |
| 518 | + " for key in diccionario_par:\n", |
| 519 | + " newArr2 = []\n", |
| 520 | + " for i in range(0, cantidadValores):\n", |
| 521 | + " newArr2.append(newArrSorted[i][key])\n", |
| 522 | + " res[key] = newArr2\n", |
515 | 523 | " #Si no tienen valores devolvemos el mismo diccionario:\n",
|
516 | 524 | " else:\n",
|
517 |
| - " return diccionario_par\n", |
| 525 | + " return diccionario_par\n", |
| 526 | + " \n", |
518 | 527 | " return res\n",
|
519 |
| - "\n", |
| 528 | + "dicc = {\n", |
| 529 | + " 'clave1':['c','a','b'],\n", |
| 530 | + " 'clave2':['casa','auto','barco'],\n", |
| 531 | + " 'clave3':[1,2,3]\n", |
| 532 | + " }\n", |
520 | 533 | "OrdenarDiccionario(dicc, 'clave1') "
|
521 | 534 | ]
|
522 | 535 | }
|
|
0 commit comments