Files
2024-09-21 11:20:44 +02:00

837 lines
15 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Kollekciók"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### [Halmaz](https://docs.python.org/3/library/stdtypes.html#set-types-set-frozenset)\n",
"\n",
"- A halmaz adattípus a matematikai halmazfogalom számítógépes megfelelője.\n",
"- Halmazt indexelni nem lehet, a tartalmazásvizsgálat O(1) időben fut le."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# Hozzunk létre egy s nevű halmazváltozót!\n",
"s = {10, 20, 30}"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(set, 3)"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Ellenőrizzük s típusát és elemszámát!\n",
"type(s), len(s)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Tartalmazásvizsgálat.\n",
"20 in s"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"21 in s"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# Elem hozzáadása a halmazhoz.\n",
"s.add(40)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{10, 20, 30, 40}"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{1, 2, 3, 4, 5, 6, 7}"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Halmazműveletek.\n",
"{1, 2, 3, 4, 5} | {4, 5, 6, 7} # unió"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{4, 5}"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"{1, 2, 3, 4, 5} & {4, 5, 6, 7} # metszet"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{1, 2, 3}"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"{1, 2, 3, 4, 5} - {4, 5, 6, 7} # kivonás"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{6, 7}"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"{4, 5, 6, 7} - {1, 2, 3, 4, 5}"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{10, 2.5, 'alma'}"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Az elemek típusa nem feltétlenül azonos.\n",
"{10, 2.5, 'alma'}"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{(20, 30), 10, 2.5, 'alma'}"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# A halmazba bármilyen nem módosítható típusú elemet be lehet tenni.\n",
"{10, 2.5, 'alma', (20, 30)}"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "unhashable type: 'list'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[13], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# ...módosíthatót viszont nem lehet!\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m {\u001b[38;5;241m10\u001b[39m, \u001b[38;5;241m2.5\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124malma\u001b[39m\u001b[38;5;124m'\u001b[39m, [\u001b[38;5;241m20\u001b[39m, \u001b[38;5;241m30\u001b[39m]}\n",
"\u001b[1;31mTypeError\u001b[0m: unhashable type: 'list'"
]
}
],
"source": [
"# ...módosíthatót viszont nem lehet!\n",
"{10, 2.5, 'alma', [20, 30]}"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"# Elem eltávolítása.\n",
"s = {20, 30, 40}\n",
"s.remove(20)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{30, 40}"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"40\n",
"20\n",
"30\n"
]
}
],
"source": [
"# Iterálás egy halmaz elemein (a sorrend meglepő is lehet).\n",
"s = {20, 30, 40}\n",
"for x in s:\n",
" print(x)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"set()"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Üres halmaz létrehozása.\n",
"set()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### [Szótár](https://docs.python.org/3/library/stdtypes.html#mapping-types-dict)\n",
"\n",
"- A szótár kulcs-érték párok halmaza, ahol a kulcsok egyediek.\n",
"- A kulcs lehet egyszerű típus, tuple vagy bármely módosíthatatlan adatszerkezet.\n",
"- Indexelni a kulccsal lehet, O(1) időben."
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"# Hozzunk létre egy d nevű szótárváltozót!\n",
"d = {'a': 10, 'b': 20}"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'a': 10, 'b': 20}"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Ellenőrizzük le d típusát és elemszámát!\n",
"type(d)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(d)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"20"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Létező kulcshoz tartozó érték lekérdezése.\n",
"d['b']"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"ename": "KeyError",
"evalue": "'banán'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[23], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# Nem létező kulcshoz tartozó érték lekérdezése.\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m d[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mbanán\u001b[39m\u001b[38;5;124m'\u001b[39m]\n",
"\u001b[1;31mKeyError\u001b[0m: 'banán'"
]
}
],
"source": [
"# Nem létező kulcshoz tartozó érték lekérdezése.\n",
"d['banán']"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"# Kulcshoz tartozó érték módosítása.\n",
"d['a'] = 100"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'a': 100, 'b': 20}"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"# Új kulcs-érték pár beszúrása.\n",
"d['cc'] = 'valami'"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'a': 100, 'b': 20, 'cc': 'valami'}"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
"# Kulcs-érték pár törlése.\n",
"del d['b']"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'a': 100, 'cc': 'valami'}"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Benne van-e egy kulcs a szótárban?\n",
"'cc' in d"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'xx' in d"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"100 in d"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{(10, 20): 'sör', (30, 40): 'bor'}"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Akár tuple is lehet szótárkulcs.\n",
"{(10, 20): 'sör', (30, 40): 'bor'}"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a\n",
"b\n",
"c\n"
]
}
],
"source": [
"# Iterálás egy szótár elemein.\n",
"d = {'a': 1, 'b': 2, 'c': 3}\n",
"for x in d:\n",
" # x-ben csak az aktuális kulcs van.\n",
" print(x)"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a 1\n",
"b 2\n",
"c 3\n"
]
}
],
"source": [
"# Iterálás egy szótár elemein.\n",
"d = {'a': 1, 'b': 2, 'c': 3}\n",
"for x in d:\n",
" # Most a kulcsokhoz tartozó értékeket is kiírjuk.\n",
" print(x, d[x])"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{}"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Üres szótár létrehozása.\n",
"{}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Konverzió\n",
"\n",
"Ezen adattípusok nevei is használhatók függvényként az adott adattípusra való konvertálásra, amennyiben a konverziónak van értelme."
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{1, 2}"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"set((1, 1, 2)) # tuple => set"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{1, 2}"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"set([1, 1, 2]) # list => set"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'a': 1, 'b': 2}"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dict([('a', 1), ('b', 2)]) # párok listája => dict"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['a', 'b']"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list({'a': 1, 'b': 2}) # szótárkulcsok listája"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[('a', 1), ('b', 2)]"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list({'a': 1, 'b': 2}.items()) # dict => párok listája"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}