Commit c90badaa authored by alan's avatar alan

v01

parent 5a472acd
......@@ -6,7 +6,7 @@
"source": [
"# 2. Python\n",
"## 2.1 Comprensiónes\n",
"Las comprensiones de python proveen de una forma consisa de crear listas, diccionarios y conjuntos. Su nombre proviene de teria de conjuntos en donde la *notación contructiva de conjuntos* o comprensión se define como:\n",
"Las comprensiones de python proveen de una forma consisa de crear listas, diccionarios y conjuntos. Su nombre proviene de teoria de conjuntos en donde la *notación constructiva de conjuntos* o comprensión se define como:\n",
"\n",
"\n",
"[Wikipedia](https://en.wikipedia.org/wiki/Set-builder_notation): Definir conjuntos por propiedades también se conoce como ***comprensión de conjuntos***, abstracción de conjuntos o como definición de la intención de un conjunto.\n",
......@@ -308,11 +308,35 @@
},
{
"cell_type": "code",
"execution_count": 67,
"execution_count": 17,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Los pares son:\n",
"[0, 2, 4, 6, 8]\n",
"Los Impares son:\n",
"[1, 3, 5, 7, 9]\n"
]
}
],
"source": [
"a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
"a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
"par = []\n",
"for i in a:\n",
" if i%2==0:\n",
" par.append(i)\n",
"print(\"Los pares son:\")\n",
"print( par)\n",
"\n",
"impar = []\n",
"for i in a:\n",
" if i%2!=0:\n",
" impar.append(i)\n",
"print(\"Los Impares son:\")\n",
"print(impar)\n"
]
},
{
......@@ -325,11 +349,42 @@
},
{
"cell_type": "code",
"execution_count": 69,
"execution_count": 56,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[(0, 'apple'), (1, 'banana'), (2, 'grapes'), (3, 'pear')]\n"
]
}
],
"source": [
"my_list = ['apple', 'banana', 'grapes', 'pear']\n",
"print(list(enumerate(my_list)))"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Elemento 0: apple.\n",
"Elemento 1: banana.\n",
"Elemento 2: grapes.\n",
"Elemento 3: pear.\n"
]
}
],
"source": [
"my_list = ['apple', 'banana', 'grapes', 'pear']\n"
"my_list = ['apple', 'banana', 'grapes', 'pear']\n",
"for indice, item in enumerate(my_list, 0):\n",
" print (\"Elemento %d: %s.\" % (indice, str(item)))"
]
},
{
......@@ -360,11 +415,28 @@
},
{
"cell_type": "code",
"execution_count": 74,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 84,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[5, 7, 97, 77, 23, 73, 61]\n"
]
}
],
"source": [
"a=[5, 7, 22, 97, 54, 62, 77, 23, 73, 61]"
"a = [5, 7, 22, 97, 54, 62, 77, 23, 73, 61] \n",
"pares = list(filter(lambda x: (x%2 != 0) , a)) \n",
"print(pares) "
]
},
{
......@@ -377,11 +449,23 @@
},
{
"cell_type": "code",
"execution_count": 76,
"execution_count": 88,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"193\n"
]
}
],
"source": [
"a = [5, 8, 10, 20, 50, 100] "
"a = [5, 8, 10, 20, 50, 100] \n",
"\n",
"from functools import reduce\n",
"sum = reduce((lambda x, y: x + y), a) \n",
"print (sum) "
]
},
{
......@@ -394,13 +478,25 @@
},
{
"cell_type": "code",
"execution_count": 78,
"execution_count": 94,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"35\n",
"5\n"
]
}
],
"source": [
"a = [5, 10, 15, 20]\n",
" \n",
"b = [30, 35, 40, 45]"
"b = [30, 35, 40, 45]\n",
"\n",
"\n",
"\n"
]
},
{
......@@ -423,11 +519,68 @@
},
{
"cell_type": "code",
"execution_count": 80,
"execution_count": 152,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<generator object all_perms at 0x7f125c651a98>"
]
},
"execution_count": 152,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def all_perms(s):\n",
" if len(s) <= 1: \n",
" yield s\n",
" else:\n",
" for i in range(len(s)):\n",
" for p in permutations(s[:i] + s[i+1:]):\n",
" yield s[i] + p\n",
" \n",
"\n",
"all_perms (['ABCD'])\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a = [\"a\", \"b\", \"c\"]"
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 141,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[('a', 'b', 'c'),\n",
" ('a', 'c', 'b'),\n",
" ('b', 'a', 'c'),\n",
" ('b', 'c', 'a'),\n",
" ('c', 'a', 'b'),\n",
" ('c', 'b', 'a')]"
]
},
"execution_count": 141,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import itertools\n",
"list(itertools.permutations([\"a\", \"b\", \"c\"])) "
]
},
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment