{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# The following list represents the participation in a competition. \n", "# It Shows the name of the coutry and the number of teams of this country in this competition\n", "a={\"Portugal\":5, \"Spain\":4, \"Italy\":3, \"France\":5 }\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Portugal', 'France']\n" ] } ], "source": [ "# What is the name of the country (or countries) that has more teams \n", "# in the competition\n", "highest = max(a.values())\n", "\n", "print([key for key, value in a.items() if value == highest])" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Portugal', 'France']" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "MoreTeams=[]\n", "for country in a.keys():\n", " if a[country] == max(a.values()):\n", " MoreTeams.append(country)\n", "MoreTeams" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Portugal', 'France']" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "max_value = max(a.values())\n", "[k for k,v in a.items() if v == max_value]" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Italy']" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# What is the name of the country (or countries) that has less \n", "# teams in the competition\n", "\n", "MoreTeams=[]\n", "for country in a.keys():\n", " if a[country] == min(a.values()):\n", " MoreTeams.append(country)\n", "MoreTeams\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['France', 'Italy', 'Portugal', 'Spain']" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# sort alfabietically the names of the countries: result is a list\n", "A=[key for key, value in a.items()]\n", "A.sort()\n", "A\n" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['France', 'Italy', 'Portugal', 'Spain']" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sorted(a.keys())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# sort the values: result is a list\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 4, 5, 5]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sorted(a.values())" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[('France', 5), ('Italy', 3), ('Portugal', 5), ('Spain', 4)]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# sort the dictionary\n", "sorted(a.items()) \n" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'France': 5, 'Italy': 3, 'Portugal': 5, 'Spain': 4}\n" ] } ], "source": [ "x=0\n", "aa={}\n", "for value in A:\n", " aa[A[x]]=a[A[x]]\n", " x=x+1\n", "print(aa)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'France': 5, 'Italy': 3, 'Portugal': 5, 'Spain': 4}" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dict(sorted(a.items()))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.5" } }, "nbformat": 4, "nbformat_minor": 2 }