Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
lesson_1 18 March 2019
{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hello world\n"
]
}
],
"source": [
"print ('hello world')"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"True + True"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"import random"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.13249756656017464"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"random.random()"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.006853896177252294\n"
]
},
{
"ename": "NameError",
"evalue": "name 'k' is not defined",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-28-b137cc1738cd>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m100\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mrandom\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrandom\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m \u001b[0mk\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mrandom\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrandom\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mNameError\u001b[0m: name 'k' is not defined"
]
}
],
"source": [
"\n",
"k = []\n",
"for i in range(100):\n",
" print (random.random())\n",
" k[i] = random.random()"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"a = 'mitsos'"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
"a=5"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [],
"source": [
"k = []"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(k)"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"k = [1]"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(k)"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"k = [[]]\n",
"len(k)"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [],
"source": [
"k = [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(k)"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [],
"source": [
"k = []"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [],
"source": [
"k.append(4)"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[4]"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"k"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[4, [4]]"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"k.append([4])\n",
"k"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[4, [4], 4]"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"k.extend([4])\n",
"k"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"17.3 µs ± 88 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"k = []\n",
"for i in range(100):\n",
" k.append(random.random())\n",
"# k"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"13.3 µs ± 99.2 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"k = [random.random() for i in range(100)] # List comprehension"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5.01 µs ± 28.5 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"c=0\n",
"for item in k:\n",
" if item<0.3:\n",
" c+=1\n",
"c"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5.22 µs ± 12.1 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"sum([1 for item in k if item<0.3])"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10.3 µs ± 83.7 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"sum([item<0.3 for item in k])"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"12.3 µs ± 38.1 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"sum(item<0.3 for item in k)"
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {},
"outputs": [],
"source": [
"l = [item<0.3 for item in k]"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"32"
]
},
"execution_count": 72,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"l.count(True)"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {},
"outputs": [],
"source": [
"k = [random.randint(1,10) for i in range(1000)]"
]
},
{
"cell_type": "code",
"execution_count": 77,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[6, 5, 5, 9, 4, 8, 9, 1, 2, 2]"
]
},
"execution_count": 77,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"k[:10]"
]
},
{
"cell_type": "code",
"execution_count": 78,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 8, 10, 7, 9, 8, 6, 2, 7, 10]"
]
},
"execution_count": 78,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"k[-10:]"
]
},
{
"cell_type": "code",
"execution_count": 82,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[6, 4, 6, 9, 5, 2, 4, 3]"
]
},
"execution_count": 82,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"k[22:30]"
]
},
{
"cell_type": "code",
"execution_count": 83,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[6, 6, 5, 4]"
]
},
"execution_count": 83,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"k[22:30:2]"
]
},
{
"cell_type": "code",
"execution_count": 85,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2, 4, 5, 6]"
]
},
"execution_count": 85,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"k[30:22:-2]"
]
},
{
"cell_type": "code",
"execution_count": 89,
"metadata": {},
"outputs": [],
"source": [
"##k[::-1]"
]
},
{
"cell_type": "code",
"execution_count": 90,
"metadata": {},
"outputs": [],
"source": [
"from collections import Counter"
]
},
{
"cell_type": "code",
"execution_count": 92,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Counter({6: 105,\n",
" 5: 90,\n",
" 9: 110,\n",
" 4: 93,\n",
" 8: 90,\n",
" 1: 98,\n",
" 2: 95,\n",
" 10: 87,\n",
" 7: 111,\n",
" 3: 121})"
]
},
"execution_count": 92,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Counter(k)"
]
},
{
"cell_type": "code",
"execution_count": 94,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Counter({'s': 13,\n",
" 'l': 12,\n",
" 'd': 12,\n",
" 'f': 12,\n",
" 'k': 12,\n",
" 'j': 12,\n",
" 'g': 13,\n",
" 'h': 14,\n",
" 'a': 1,\n",
" 'r': 1,\n",
" 'i': 1})"
]
},
"execution_count": 94,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Counter('sldfkjghsdljfhalkjhgsdklfjghsdklfjghsdkjfghsdlkfghsrighslkdfjghsdfkljghsldkfjghsldkfjghsdklfjhgsldkjfgh')"
]
},
{
"cell_type": "code",
"execution_count": 95,
"metadata": {},
"outputs": [],
"source": [
"k1 = [random.randint(1,10) for i in range(1000)]\n",
"k2 = [random.randint(1,10) for i in range(1000)]\n",
"\n",
"c1 = Counter(k1)\n",
"c2 = Counter(k2)"
]
},
{
"cell_type": "code",
"execution_count": 97,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Counter({1: 207,\n",
" 7: 187,\n",
" 8: 196,\n",
" 6: 180,\n",
" 3: 193,\n",
" 2: 212,\n",
" 5: 218,\n",
" 4: 206,\n",
" 10: 202,\n",
" 9: 199})"
]
},
"execution_count": 97,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c1+c2"
]
},
{
"cell_type": "code",
"execution_count": 99,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'a', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'r', 's', 't', 'u', 'w'}"
]
},
"execution_count": 99,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"set('wrjthweklrthlaekjghsldkjghseilughadkljghsdfjghsdklfjghseilrghsdlkfjghsdlkfjgh')"
]
},
{
"cell_type": "code",
"execution_count": 103,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{0, 1, 2, 3, 4}"
]
},
"execution_count": 103,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"set([i%5 for i in range(10000)])"
]
},
{
"cell_type": "code",
"execution_count": 105,
"metadata": {},
"outputs": [],
"source": [
"a = set(['a', 'b', 'c'])\n",
"b = set(['b', 'c', 'd'])"
]
},
{
"cell_type": "code",
"execution_count": 106,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'a'}"
]
},
"execution_count": 106,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a-b"
]
},
{
"cell_type": "code",
"execution_count": 107,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'d'}"
]
},
"execution_count": 107,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b-a"
]
},
{
"cell_type": "code",
"execution_count": 110,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'a', 'b', 'c', 'd'}"
]
},
"execution_count": 110,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b | a"
]
},
{
"cell_type": "code",
"execution_count": 112,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'b', 'c'}"
]
},
"execution_count": 112,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a & b"
]
},
{
"cell_type": "code",
"execution_count": 114,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{0, 1, 2, 3, 4}"
]
},
"execution_count": 114,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"set([i%5 for i in range(10000)])"
]
},
{
"cell_type": "code",
"execution_count": 116,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{0, 1, 2, 3, 4}"
]
},
"execution_count": 116,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"{i%5 for i in range(10000)} # Set comprehension "
]
},
{
"cell_type": "code",
"execution_count": 119,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100}"
]
},
"execution_count": 119,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"{i:i*i for i in range(1,11)} # Dictionary comprehension"
]
},
{
"cell_type": "code",
"execution_count": 120,
"metadata": {},
"outputs": [],
"source": [
"def f():\n",
" return 1"
]
},
{
"cell_type": "code",
"execution_count": 122,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 122,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f()"
]
},
{
"cell_type": "code",
"execution_count": 124,
"metadata": {},
"outputs": [],
"source": [
"def f():\n",
" return 1\n",
" return 2\n",
" return 3"
]
},
{
"cell_type": "code",
"execution_count": 125,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 125,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f()"
]
},
{
"cell_type": "code",
"execution_count": 126,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 126,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f()"
]
},
{
"cell_type": "code",
"execution_count": 127,
"metadata": {},
"outputs": [],
"source": [
"def g():\n",
" yield 1\n",
" yield 2\n",
" yield 3"
]
},
{
"cell_type": "code",
"execution_count": 128,
"metadata": {},
"outputs": [],
"source": [
"gen = g()"
]
},
{
"cell_type": "code",
"execution_count": 129,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 129,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next(gen)"
]
},
{
"cell_type": "code",
"execution_count": 130,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 130,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next(gen)"
]
},
{
"cell_type": "code",
"execution_count": 131,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 131,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next(gen)"
]
},
{
"cell_type": "code",
"execution_count": 132,
"metadata": {},
"outputs": [
{
"ename": "StopIteration",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mStopIteration\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-132-6e72e47198db>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mnext\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mgen\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mStopIteration\u001b[0m: "
]
}
],
"source": [
"next(gen)"
]
},
{
"cell_type": "code",
"execution_count": 133,
"metadata": {},
"outputs": [],
"source": [
"def g():\n",
" while True:\n",
" yield 1\n",
" yield 2\n",
" yield 3"
]
},
{
"cell_type": "code",
"execution_count": 134,
"metadata": {},
"outputs": [],
"source": [
"gen = g()"
]
},
{
"cell_type": "code",
"execution_count": 135,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 135,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next(gen)"
]
},
{
"cell_type": "code",
"execution_count": 136,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 136,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next(gen)"
]
},
{
"cell_type": "code",
"execution_count": 137,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 137,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next(gen)"
]
},
{
"cell_type": "code",
"execution_count": 138,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 138,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next(gen)"
]
},
{
"cell_type": "code",
"execution_count": 139,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 139,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next(gen)"
]
},
{
"cell_type": "code",
"execution_count": 140,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 140,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next(gen)"
]
},
{
"cell_type": "code",
"execution_count": 141,
"metadata": {},
"outputs": [],
"source": [
"g = (i for i in range(3))\n"
]
},
{
"cell_type": "code",
"execution_count": 142,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"generator"
]
},
"execution_count": 142,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(g)"
]
},
{
"cell_type": "code",
"execution_count": 144,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 144,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next(g)"
]
},
{
"cell_type": "code",
"execution_count": 145,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 145,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next(g)"
]
},
{
"cell_type": "code",
"execution_count": 146,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 146,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next(g)"
]
},
{
"cell_type": "code",
"execution_count": 147,
"metadata": {},
"outputs": [
{
"ename": "StopIteration",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mStopIteration\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-147-e734f8aca5ac>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mnext\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mg\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mStopIteration\u001b[0m: "
]
}
],
"source": [
"next(g)"
]
},
{
"cell_type": "code",
"execution_count": 148,
"metadata": {},
"outputs": [],
"source": [
"k1 = [random.randint(1,10) for i in range(1000)]"
]
},
{
"cell_type": "code",
"execution_count": 160,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"54.8 µs ± 192 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"Counter(k1)"
]
},
{
"cell_type": "code",
"execution_count": 162,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"154 µs ± 300 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"d = {}\n",
"for item in k1:\n",
" if not item in d:\n",
" d[item]=1\n",
" else:\n",
" d[item]+=1\n",
"#d"
]
},
{
"cell_type": "code",
"execution_count": 166,
"metadata": {},
"outputs": [],
"source": [
"from collections import defaultdict"
]
},
{
"cell_type": "code",
"execution_count": 168,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"defaultdict(int,\n",
" {9: 108,\n",
" 8: 98,\n",
" 4: 107,\n",
" 6: 88,\n",
" 10: 98,\n",
" 7: 96,\n",
" 5: 105,\n",
" 1: 107,\n",
" 3: 102,\n",
" 2: 91})"
]
},
"execution_count": 168,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d = defaultdict(int)\n",
"for item in k1:\n",
" d[item]+=1\n",
"d"
]
},
{
"cell_type": "code",
"execution_count": 169,
"metadata": {},
"outputs": [],
"source": [
"d = defaultdict(int)"
]
},
{
"cell_type": "code",
"execution_count": 171,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 171,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d[50]"
]
},
{
"cell_type": "code",
"execution_count": 173,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 173,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d[100]"
]
},
{
"cell_type": "code",
"execution_count": 174,
"metadata": {},
"outputs": [],
"source": [
"e = defaultdict(list)"
]
},
{
"cell_type": "code",
"execution_count": 175,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[]"
]
},
"execution_count": 175,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"e[50]"
]
},
{
"cell_type": "code",
"execution_count": 176,
"metadata": {},
"outputs": [],
"source": [
"t = {}"
]
},
{
"cell_type": "code",
"execution_count": 177,
"metadata": {},
"outputs": [
{
"ename": "KeyError",
"evalue": "50",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-177-9889fbe0f0c8>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mt\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m50\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mKeyError\u001b[0m: 50"
]
}
],
"source": [
"t[50]"
]
},
{
"cell_type": "code",
"execution_count": 155,
"metadata": {},
"outputs": [],
"source": [
"d = {}\n",
"d['mitsos']=4"
]
},
{
"cell_type": "code",
"execution_count": 156,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 156,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'kostas' in d"
]
},
{
"cell_type": "code",
"execution_count": 178,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 178,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'mitsos' in d"
]
},
{
"cell_type": "code",
"execution_count": 179,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 179,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'os' in 'mitsos'"
]
},
{
"cell_type": "code",
"execution_count": 181,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 181,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"5 in [1,2,3,4,5]"
]
},
{
"cell_type": "code",
"execution_count": 182,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"15"
]
},
"execution_count": 182,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"3*5"
]
},
{
"cell_type": "code",
"execution_count": 183,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'hellohellohellohello'"
]
},
"execution_count": 183,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"4*'hello'"
]
},
{
"cell_type": "code",
"execution_count": 184,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]"
]
},
"execution_count": 184,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"4 * [1,2,3,4]"
]
},
{
"cell_type": "code",
"execution_count": 185,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]"
]
},
"execution_count": 185,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = 5*[[1,2,3]]\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": 186,
"metadata": {},
"outputs": [],
"source": [
"b = [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]\n"
]
},
{
"cell_type": "code",
"execution_count": 188,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[1, 2, 3], [1, 2, 5], [1, 2, 3], [1, 2, 3], [1, 2, 3]]"
]
},
"execution_count": 188,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b[1][2]=5\n",
"b"
]
},
{
"cell_type": "code",
"execution_count": 189,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]"
]
},
"execution_count": 189,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": 190,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[1, 2, 5], [1, 2, 5], [1, 2, 5], [1, 2, 5], [1, 2, 5]]"
]
},
"execution_count": 190,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a[1][2]=5\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": 199,
"metadata": {},
"outputs": [],
"source": [
"def f(x,y):\n",
" while True:\n",
" for k in range(x,y+1):\n",
" yield k"
]
},
{
"cell_type": "code",
"execution_count": 197,
"metadata": {},
"outputs": [],
"source": [
"g = f(3,5)"
]
},
{
"cell_type": "code",
"execution_count": 200,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"function"
]
},
"execution_count": 200,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(f)"
]
},
{
"cell_type": "code",
"execution_count": 201,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"generator"
]
},
"execution_count": 201,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(g)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 196,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[3,\n",
" 4,\n",
" 5,\n",
" 3,\n",
" 4,\n",
" 5,\n",
" 3,\n",
" 4,\n",
" 5,\n",
" 3,\n",
" 4,\n",
" 5,\n",
" 3,\n",
" 4,\n",
" 5,\n",
" 3,\n",
" 4,\n",
" 5,\n",
" 3,\n",
" 4,\n",
" 5,\n",
" 3,\n",
" 4,\n",
" 5,\n",
" 3,\n",
" 4,\n",
" 5,\n",
" 3,\n",
" 4,\n",
" 5,\n",
" 3,\n",
" 4,\n",
" 5,\n",
" 3,\n",
" 4,\n",
" 5,\n",
" 3,\n",
" 4,\n",
" 5,\n",
" 3,\n",
" 4,\n",
" 5,\n",
" 3,\n",
" 4,\n",
" 5,\n",
" 3,\n",
" 4,\n",
" 5,\n",
" 3,\n",
" 4,\n",
" 5,\n",
" 3,\n",
" 4,\n",
" 5,\n",
" 3,\n",
" 4,\n",
" 5,\n",
" 3,\n",
" 4,\n",
" 5,\n",
" 3,\n",
" 4,\n",
" 5,\n",
" 3,\n",
" 4,\n",
" 5,\n",
" 3,\n",
" 4,\n",
" 5,\n",
" 3,\n",
" 4,\n",
" 5,\n",
" 3,\n",
" 4,\n",
" 5,\n",
" 3,\n",
" 4,\n",
" 5,\n",
" 3,\n",
" 4,\n",
" 5,\n",
" 3,\n",
" 4,\n",
" 5,\n",
" 3,\n",
" 4,\n",
" 5,\n",
" 3,\n",
" 4,\n",
" 5,\n",
" 3,\n",
" 4,\n",
" 5,\n",
" 3,\n",
" 4,\n",
" 5,\n",
" 3,\n",
" 4,\n",
" 5,\n",
" 3]"
]
},
"execution_count": 196,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[next(g) for i in range(100)]"
]
},
{
"cell_type": "code",
"execution_count": 202,
"metadata": {},
"outputs": [],
"source": [
"l = [0, -1, 3, 3, 2, -2, -4, 4, 2, -1, -2, 3, 2, -1, 0, 3, 5, -3, 5, -3, \n",
"-4, -4, 5, -5, -4, 2, 1, 1, -1, 1, 5, 5, 2, 0, 0, 0, -3, 2, 1, 5, 3, 2, \n",
"2, -5, -2, -3, 1, -5, -4, 3, 3, -5, 2, 0, -2, -1, -1, 1, 2, 3, -3, 4, -2, \n",
"-4, 0, 0, 0, -4, -3, -1, -1, -2, -4, -3, 0, 4, 1, 4, -4, 3, 5, 5, -1, -2, \n",
"0, 3, -1, -2, 3, 5, 2, -3, -2, 1, 1, 5, -4, -5, -5, -3]"
]
},
{
"cell_type": "code",
"execution_count": 203,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 203,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"max(3,5)"
]
},
{
"cell_type": "code",
"execution_count": 204,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[7, 8]"
]
},
"execution_count": 204,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"max([3,5], [7,8])"
]
},
{
"cell_type": "code",
"execution_count": 205,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[3, 5]"
]
},
"execution_count": 205,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"max([3,5], [3,4])"
]
},
{
"cell_type": "code",
"execution_count": 211,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[31, 43]"
]
},
"execution_count": 211,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"max([ [sum(l[:N]), N] for N in range(100)])"
]
},
{
"cell_type": "code",
"execution_count": 213,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[0, 0],\n",
" [0, 1],\n",
" [-1, 2],\n",
" [2, 3],\n",
" [5, 4],\n",
" [7, 5],\n",
" [5, 6],\n",
" [1, 7],\n",
" [5, 8],\n",
" [7, 9],\n",
" [6, 10],\n",
" [4, 11],\n",
" [7, 12],\n",
" [9, 13],\n",
" [8, 14],\n",
" [8, 15],\n",
" [11, 16],\n",
" [16, 17],\n",
" [13, 18],\n",
" [18, 19],\n",
" [15, 20],\n",
" [11, 21],\n",
" [7, 22],\n",
" [12, 23],\n",
" [7, 24],\n",
" [3, 25],\n",
" [5, 26],\n",
" [6, 27],\n",
" [7, 28],\n",
" [6, 29],\n",
" [7, 30],\n",
" [12, 31],\n",
" [17, 32],\n",
" [19, 33],\n",
" [19, 34],\n",
" [19, 35],\n",
" [19, 36],\n",
" [16, 37],\n",
" [18, 38],\n",
" [19, 39],\n",
" [24, 40],\n",
" [27, 41],\n",
" [29, 42],\n",
" [31, 43],\n",
" [26, 44],\n",
" [24, 45],\n",
" [21, 46],\n",
" [22, 47],\n",
" [17, 48],\n",
" [13, 49],\n",
" [16, 50],\n",
" [19, 51],\n",
" [14, 52],\n",
" [16, 53],\n",
" [16, 54],\n",
" [14, 55],\n",
" [13, 56],\n",
" [12, 57],\n",
" [13, 58],\n",
" [15, 59],\n",
" [18, 60],\n",
" [15, 61],\n",
" [19, 62],\n",
" [17, 63],\n",
" [13, 64],\n",
" [13, 65],\n",
" [13, 66],\n",
" [13, 67],\n",
" [9, 68],\n",
" [6, 69],\n",
" [5, 70],\n",
" [4, 71],\n",
" [2, 72],\n",
" [-2, 73],\n",
" [-5, 74],\n",
" [-5, 75],\n",
" [-1, 76],\n",
" [0, 77],\n",
" [4, 78],\n",
" [0, 79],\n",
" [3, 80],\n",
" [8, 81],\n",
" [13, 82],\n",
" [12, 83],\n",
" [10, 84],\n",
" [10, 85],\n",
" [13, 86],\n",
" [12, 87],\n",
" [10, 88],\n",
" [13, 89],\n",
" [18, 90],\n",
" [20, 91],\n",
" [17, 92],\n",
" [15, 93],\n",
" [16, 94],\n",
" [17, 95],\n",
" [22, 96],\n",
" [18, 97],\n",
" [13, 98],\n",
" [8, 99]]"
]
},
"execution_count": 213,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[ [sum(l[:N]), N] for N in range(100)]"
]
},
{
"cell_type": "code",
"execution_count": 214,
"metadata": {},
"outputs": [],
"source": [
"reads = [(27, 46),\n",
" (19, 35),\n",
" (12, 32),\n",
" (38, 50),\n",
" (88, 96),\n",
" (17, 37),\n",
" (80, 83),\n",
" (85, 98),\n",
" (29, 42),\n",
" (25, 31),\n",
" (12, 31),\n",
" (16, 21),\n",
" (4, 18),\n",
" (36, 56),\n",
" (47, 53),\n",
" (37, 54),\n",
" (83, 95),\n",
" (64, 69),\n",
" (12, 16),\n",
" (59, 75)]"
]
},
{
"cell_type": "code",
"execution_count": 230,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(7, 30)"
]
},
"execution_count": 230,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"max([(c, pos) for pos, c in Counter([z for i,j in reads for z in range(i,j)]).items()])"
]
},
{
"cell_type": "code",
"execution_count": 236,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{0, 1, 2, 3, 56, 57, 58, 75, 76, 77, 78, 79, 98, 99}"
]
},
"execution_count": 236,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"set(range(100)) - set([z for i,j in reads for z in range(i,j)])"
]
},
{
"cell_type": "code",
"execution_count": 232,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Counter({27: 6,\n",
" 28: 6,\n",
" 29: 7,\n",
" 30: 7,\n",
" 31: 5,\n",
" 32: 4,\n",
" 33: 4,\n",
" 34: 4,\n",
" 35: 3,\n",
" 36: 4,\n",
" 37: 4,\n",
" 38: 5,\n",
" 39: 5,\n",
" 40: 5,\n",
" 41: 5,\n",
" 42: 4,\n",
" 43: 4,\n",
" 44: 4,\n",
" 45: 4,\n",
" 19: 5,\n",
" 20: 5,\n",
" 21: 4,\n",
" 22: 4,\n",
" 23: 4,\n",
" 24: 4,\n",
" 25: 5,\n",
" 26: 5,\n",
" 12: 4,\n",
" 13: 4,\n",
" 14: 4,\n",
" 15: 4,\n",
" 16: 4,\n",
" 17: 5,\n",
" 18: 4,\n",
" 46: 3,\n",
" 47: 4,\n",
" 48: 4,\n",
" 49: 4,\n",
" 88: 3,\n",
" 89: 3,\n",
" 90: 3,\n",
" 91: 3,\n",
" 92: 3,\n",
" 93: 3,\n",
" 94: 3,\n",
" 95: 2,\n",
" 80: 1,\n",
" 81: 1,\n",
" 82: 1,\n",
" 85: 2,\n",
" 86: 2,\n",
" 87: 2,\n",
" 96: 1,\n",
" 97: 1,\n",
" 4: 1,\n",
" 5: 1,\n",
" 6: 1,\n",
" 7: 1,\n",
" 8: 1,\n",
" 9: 1,\n",
" 10: 1,\n",
" 11: 1,\n",
" 50: 3,\n",
" 51: 3,\n",
" 52: 3,\n",
" 53: 2,\n",
" 54: 1,\n",
" 55: 1,\n",
" 83: 1,\n",
" 84: 1,\n",
" 64: 2,\n",
" 65: 2,\n",
" 66: 2,\n",
" 67: 2,\n",
" 68: 2,\n",
" 59: 1,\n",
" 60: 1,\n",
" 61: 1,\n",
" 62: 1,\n",
" 63: 1,\n",
" 69: 1,\n",
" 70: 1,\n",
" 71: 1,\n",
" 72: 1,\n",
" 73: 1,\n",
" 74: 1})"
]
},
"execution_count": 232,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Counter([z for i,j in reads for z in range(i,j)])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#a=1\n",
"#a = 1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 226,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_items([('mitsos', 2), ('kostas', 5)])"
]
},
"execution_count": 226,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = {'mitsos': 2, 'kostas': 5}\n",
"a.items()"
]
},
{
"cell_type": "code",
"execution_count": 220,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[(1, 'kostas'),\n",
" (1, 'mitsos'),\n",
" (2, 'kostas'),\n",
" (2, 'mitsos'),\n",
" (3, 'kostas'),\n",
" (3, 'mitsos')]"
]
},
"execution_count": 220,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[(i,j) for i in [1,2,3] for j in ['kostas', 'mitsos']]"
]
},
{
"cell_type": "code",
"execution_count": 222,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 kostas\n",
"1 mitsos\n",
"2 kostas\n",
"2 mitsos\n",
"3 kostas\n",
"3 mitsos\n"
]
}
],
"source": [
"for i in [1,2,3]:\n",
" for j in ['kostas', 'mitsos']:\n",
" print (i,j)"
]
},
{
"cell_type": "code",
"execution_count": 237,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"27\n",
"28\n",
"29\n",
"30\n",
"31\n",
"32\n",
"33\n",
"34\n",
"35\n",
"36\n",
"37\n",
"38\n",
"39\n",
"40\n",
"41\n",
"42\n",
"43\n",
"44\n",
"45\n",
"19\n",
"20\n",
"21\n",
"22\n",
"23\n",
"24\n",
"25\n",
"26\n",
"27\n",
"28\n",
"29\n",
"30\n",
"31\n",
"32\n",
"33\n",
"34\n",
"12\n",
"13\n",
"14\n",
"15\n",
"16\n",
"17\n",
"18\n",
"19\n",
"20\n",
"21\n",
"22\n",
"23\n",
"24\n",
"25\n",
"26\n",
"27\n",
"28\n",
"29\n",
"30\n",
"31\n",
"38\n",
"39\n",
"40\n",
"41\n",
"42\n",
"43\n",
"44\n",
"45\n",
"46\n",
"47\n",
"48\n",
"49\n",
"88\n",
"89\n",
"90\n",
"91\n",
"92\n",
"93\n",
"94\n",
"95\n",
"17\n",
"18\n",
"19\n",
"20\n",
"21\n",
"22\n",
"23\n",
"24\n",
"25\n",
"26\n",
"27\n",
"28\n",
"29\n",
"30\n",
"31\n",
"32\n",
"33\n",
"34\n",
"35\n",
"36\n",
"80\n",
"81\n",
"82\n",
"85\n",
"86\n",
"87\n",
"88\n",
"89\n",
"90\n",
"91\n",
"92\n",
"93\n",
"94\n",
"95\n",
"96\n",
"97\n",
"29\n",
"30\n",
"31\n",
"32\n",
"33\n",
"34\n",
"35\n",
"36\n",
"37\n",
"38\n",
"39\n",
"40\n",
"41\n",
"25\n",
"26\n",
"27\n",
"28\n",
"29\n",
"30\n",
"12\n",
"13\n",
"14\n",
"15\n",
"16\n",
"17\n",
"18\n",
"19\n",
"20\n",
"21\n",
"22\n",
"23\n",
"24\n",
"25\n",
"26\n",
"27\n",
"28\n",
"29\n",
"30\n",
"16\n",
"17\n",
"18\n",
"19\n",
"20\n",
"4\n",
"5\n",
"6\n",
"7\n",
"8\n",
"9\n",
"10\n",
"11\n",
"12\n",
"13\n",
"14\n",
"15\n",
"16\n",
"17\n",
"36\n",
"37\n",
"38\n",
"39\n",
"40\n",
"41\n",
"42\n",
"43\n",
"44\n",
"45\n",
"46\n",
"47\n",
"48\n",
"49\n",
"50\n",
"51\n",
"52\n",
"53\n",
"54\n",
"55\n",
"47\n",
"48\n",
"49\n",
"50\n",
"51\n",
"52\n",
"37\n",
"38\n",
"39\n",
"40\n",
"41\n",
"42\n",
"43\n",
"44\n",
"45\n",
"46\n",
"47\n",
"48\n",
"49\n",
"50\n",
"51\n",
"52\n",
"53\n",
"83\n",
"84\n",
"85\n",
"86\n",
"87\n",
"88\n",
"89\n",
"90\n",
"91\n",
"92\n",
"93\n",
"94\n",
"64\n",
"65\n",
"66\n",
"67\n",
"68\n",
"12\n",
"13\n",
"14\n",
"15\n",
"59\n",
"60\n",
"61\n",
"62\n",
"63\n",
"64\n",
"65\n",
"66\n",
"67\n",
"68\n",
"69\n",
"70\n",
"71\n",
"72\n",
"73\n",
"74\n"
]
}
],
"source": [
"for start, end in reads:\n",
" for position in range(start, end):\n",
" print (position)"
]
},
{
"cell_type": "code",
"execution_count": 240,
"metadata": {},
"outputs": [],
"source": [
"a = ['AA', 'AC', 'CC', 'AA', 'AA', 'CC', 'AC', 'AA', 'AA', 'CC']"
]
},
{
"cell_type": "code",
"execution_count": 242,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2, 1, 0, 2, 2, 0, 1, 2, 2, 0]"
]
},
"execution_count": 242,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[x.count('A') for x in a]"
]
},
{
"cell_type": "code",
"execution_count": 243,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"12"
]
},
"execution_count": 243,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sum([x.count('A') for x in a])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# ALLELE FREQUENCY"
]
},
{
"cell_type": "code",
"execution_count": 246,
"metadata": {},
"outputs": [],
"source": [
"af_A = sum([x.count('A') for x in a])/(2*len(a))"
]
},
{
"cell_type": "code",
"execution_count": 248,
"metadata": {},
"outputs": [],
"source": [
"af_C = sum([x.count('C') for x in a])/(2*len(a))"
]
},
{
"cell_type": "code",
"execution_count": 249,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.6 0.4\n"
]
}
],
"source": [
"print (af_A, af_C)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### MINOR ALLELE FREQUENCy (MAF)"
]
},
{
"cell_type": "code",
"execution_count": 250,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.4"
]
},
"execution_count": 250,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"min(af_A, af_C)"
]
},
{
"cell_type": "code",
"execution_count": 251,
"metadata": {},
"outputs": [],
"source": [
"fn = 'Desktop/gencode.v29.annotation.gff3'"
]
},
{
"cell_type": "code",
"execution_count": 252,
"metadata": {},
"outputs": [],
"source": [
"f = open(fn)"
]
},
{
"cell_type": "code",
"execution_count": 253,
"metadata": {},
"outputs": [],
"source": [
"lines = [f.readline() for x in range(100)]"
]
},
{
"cell_type": "code",
"execution_count": 263,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'ID': 'UTR5:ENST00000335137.4',\n",
" 'Parent': 'ENST00000335137.4',\n",
" 'gene_id': 'ENSG00000186092.6',\n",
" 'transcript_id': 'ENST00000335137.4',\n",
" 'gene_type': 'protein_coding',\n",
" 'gene_name': 'OR4F5',\n",
" 'transcript_type': 'protein_coding',\n",
" 'transcript_name': 'OR4F5-201',\n",
" 'exon_number': '1',\n",
" 'exon_id': 'ENSE00002319515.2',\n",
" 'level': '3',\n",
" 'protein_id': 'ENSP00000334393.3',\n",
" 'transcript_support_level': 'NA',\n",
" 'tag': 'basic,appris_principal_1,CCDS',\n",
" 'ccdsid': 'CCDS30547.1',\n",
" 'havana_gene': 'OTTHUMG00000001094.4'}"
]
},
"execution_count": 263,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dict([x.split('=') for x in lines[81].split('\\t')[-1].strip().split(';')])"
]
}
],
"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.7.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.