1
0
mirror of https://github.com/gsi-upm/sitc synced 2024-09-28 16:11:42 +00:00
sitc/python/1_4_Sets.ipynb
2016-02-01 13:05:58 +01:00

625 lines
11 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![](http://www.upm.es/sfs/Rectorado/Gabinete%20del%20Rector/Logos/UPM/EscPolitecnica/EscUpmPolit_p.gif \"UPM\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Course Notes for Learning Intelligent Systems"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Department of Telematic Engineering Systems, Universidad Politécnica de Madrid, © 2015 Carlos A. Iglesias"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Built-in Types: Sets and Mappings"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* *Sets* are unordered bags of values\n",
"* *Dictionaries* are unordered bags of key-values pairs\n",
"\n",
"A set object is an unordered collection of distinct objects. There are two built-in set types: **set** (mutable) and **frozenset** (inmutable).\n",
"\n",
"A mapping object maps hashable values to arbitrary objects. Mappings are mutable objects. There is only one bultin mapping type: **dictionary**."
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"## 1. Sets"
]
},
{
"cell_type": "code",
"execution_count": 413,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"set()"
]
},
"execution_count": 413,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_set = set() #create a set\n",
"my_set"
]
},
{
"cell_type": "code",
"execution_count": 414,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{1}"
]
},
"execution_count": 414,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_set.add(1) # add an element\n",
"my_set"
]
},
{
"cell_type": "code",
"execution_count": 415,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"my_set.add(2) # add another element"
]
},
{
"cell_type": "code",
"execution_count": 416,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{1, 2}"
]
},
"execution_count": 416,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_set"
]
},
{
"cell_type": "code",
"execution_count": 417,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{1, 2, 3}"
]
},
"execution_count": 417,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_set.add(3) # add another one\n",
"my_set"
]
},
{
"cell_type": "code",
"execution_count": 418,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{1, 2, 3}"
]
},
"execution_count": 418,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_set.add(1) #try to add a repeated element\n",
"my_set"
]
},
{
"cell_type": "code",
"execution_count": 419,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}"
]
},
"execution_count": 419,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s2 = set(range(10)) # we can create a set from a range\n",
"s2"
]
},
{
"cell_type": "code",
"execution_count": 420,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"l = ['a', 'a', 'b', 'c', 'c', 'c']"
]
},
{
"cell_type": "code",
"execution_count": 421,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{'a', 'b', 'c'}"
]
},
"execution_count": 421,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s3 = set(l) # if we create a set from a list, elements are not repeated\n",
"s3"
]
},
{
"cell_type": "code",
"execution_count": 422,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 422,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(s3) "
]
},
{
"cell_type": "code",
"execution_count": 423,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{0, 1, 2, 3, 4, 5, 'c', 6, 7, 8, 9, 'a', 'b'}"
]
},
"execution_count": 423,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s3.union(s2) # we can use set methods: union(), intersection(), difference(), ..."
]
},
{
"cell_type": "code",
"execution_count": 424,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 424,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"3 in my_set #check membership"
]
},
{
"cell_type": "code",
"execution_count": 425,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"set"
]
},
"execution_count": 425,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(s3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Dictionaries"
]
},
{
"cell_type": "code",
"execution_count": 426,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{'key1': 1, 'key2': 2, 'key3': 3}"
]
},
"execution_count": 426,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_dictionary = {'key1': 1, 'key2': 2, 'key3': 3} # pairs of key-value mappings\n",
"my_dictionary"
]
},
{
"cell_type": "code",
"execution_count": 427,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 427,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_dictionary['key1'] #retrieve a value given a key"
]
},
{
"cell_type": "code",
"execution_count": 428,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{'key1': 1, 'key2': 2, 'key3': 3}"
]
},
"execution_count": 428,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_dict = dict()\n",
"my_dict['key1'] = 1\n",
"my_dict['key2'] = 2\n",
"my_dict['key3'] = 3\n",
"my_dict # alternative way to create a dictionary "
]
},
{
"cell_type": "code",
"execution_count": 429,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 429,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_dict == my_dictionary # check if both dictionaries are equal"
]
},
{
"cell_type": "code",
"execution_count": 430,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{'one': {'two': {'three': 'Nested dict'}}}"
]
},
"execution_count": 430,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_dict2 = {'one': {'two': {'three': 'Nested dict'}}} #nested dictionary\n",
"my_dict2"
]
},
{
"cell_type": "code",
"execution_count": 431,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'Nested dict'"
]
},
"execution_count": 431,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_dict2['one']['two']['three'] #access the value"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Dictionaries have different methods, check them with Tab."
]
},
{
"cell_type": "code",
"execution_count": 473,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"dict_keys(['key2', 'key3', 'key1'])"
]
},
"execution_count": 473,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_dict.keys() # in Python3 we get a View object that changes when the dictionary changes"
]
},
{
"cell_type": "code",
"execution_count": 478,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"['key2', 'key3', 'key1']"
]
},
"execution_count": 478,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(my_dict.keys()) # we can convert it to a list, we see dicionaries are unordered"
]
},
{
"cell_type": "code",
"execution_count": 474,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"dict_values([2, 3, 1])"
]
},
"execution_count": 474,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_dict.values()"
]
},
{
"cell_type": "code",
"execution_count": 476,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[2, 3, 1]"
]
},
"execution_count": 476,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(my_dict.values())"
]
},
{
"cell_type": "code",
"execution_count": 479,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"dict"
]
},
"execution_count": 479,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(my_dict)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Licence"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The notebook is freely licensed under under the [Creative Commons Attribution Share-Alike license](https://creativecommons.org/licenses/by/2.0/). \n",
"\n",
"© 2015 Carlos A. Iglesias, Universidad Politécnica de Madrid."
]
}
],
"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.4.3+"
}
},
"nbformat": 4,
"nbformat_minor": 0
}