1
0
mirror of https://github.com/gsi-upm/sitc synced 2024-11-18 04:22:28 +00:00
sitc/python/1_3_Sequences.ipynb

719 lines
13 KiB
Plaintext
Raw Normal View History

2016-02-01 12:05:58 +00:00
{
"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": [
2016-02-15 08:01:37 +00:00
"Department of Telematic Engineering Systems, Universidad Politécnica de Madrid, © 2016 Carlos A. Iglesias"
2016-02-01 12:05:58 +00:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Built-in Types: Sequences"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Sequences** are ordered collections of elements. The basic built-int sequence types are **lists**, **tuples** and **range** objets. There other sequence types for text strings (str, Strings) or binary data.\n",
"\n",
"Sequences can be mutable or inmutable, if their value can be changed or not, respectively.\n",
"* **lists** are mutable ordered sequences of values\n",
"* **tuples** are inmutable ordered sequences of values\n",
"* **ranges** are immutable sequences of numbers"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"## 1. Lists"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"l = [1, 2, 3, 4, 5, 6]"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
2016-02-15 08:01:37 +00:00
"outputs": [],
2016-02-01 12:05:58 +00:00
"source": [
"l"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
2016-02-15 08:01:37 +00:00
"outputs": [],
2016-02-01 12:05:58 +00:00
"source": [
"l[0:3] # we can use slicing in sequence types"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"other_list = [1, 0.0, \"hola\"] #lists can have elements of different types"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
2016-02-15 08:01:37 +00:00
"outputs": [],
2016-02-01 12:05:58 +00:00
"source": [
"other_list"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
2016-02-15 08:01:37 +00:00
"outputs": [],
2016-02-01 12:05:58 +00:00
"source": [
"l + other_list # we can add lists (append)"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
2016-02-15 08:01:37 +00:00
"outputs": [],
2016-02-01 12:05:58 +00:00
"source": [
"l * 3 # we can add n times a list"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
2016-02-15 08:01:37 +00:00
"outputs": [],
2016-02-01 12:05:58 +00:00
"source": [
"len(l) # length of a list (as Strings)"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"l.append(7) #append at the end of the list. Check help with Shift-tab, and methods with tab"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
2016-02-15 08:01:37 +00:00
"outputs": [],
2016-02-01 12:05:58 +00:00
"source": [
"l"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
2016-02-15 08:01:37 +00:00
"outputs": [],
2016-02-01 12:05:58 +00:00
"source": [
"l.pop() # remove last element"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
2016-02-15 08:01:37 +00:00
"outputs": [],
2016-02-01 12:05:58 +00:00
"source": [
"l"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
2016-02-15 08:01:37 +00:00
"outputs": [],
2016-02-01 12:05:58 +00:00
"source": [
"l.pop(2) # remove element at index 2"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
2016-02-15 08:01:37 +00:00
"outputs": [],
2016-02-01 12:05:58 +00:00
"source": [
"l"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"l.insert(2,3) # insert at index 2 the value 3"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
2016-02-15 08:01:37 +00:00
"outputs": [],
2016-02-01 12:05:58 +00:00
"source": [
"l"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"l.reverse()"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
2016-02-15 08:01:37 +00:00
"outputs": [],
2016-02-01 12:05:58 +00:00
"source": [
"l"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"l.sort()"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
2016-02-15 08:01:37 +00:00
"outputs": [],
2016-02-01 12:05:58 +00:00
"source": [
"l"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"l.remove(3) # remove first ocurrence of 3 from l. Remember: remove (element) vs pop(index)"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
2016-02-15 08:01:37 +00:00
"outputs": [],
2016-02-01 12:05:58 +00:00
"source": [
"l"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"l[0] = 0 # lists are mutable"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
2016-02-15 08:01:37 +00:00
"outputs": [],
2016-02-01 12:05:58 +00:00
"source": [
"l"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
2016-02-15 08:01:37 +00:00
"outputs": [],
2016-02-01 12:05:58 +00:00
"source": [
"2 in l # check if an element is in a list"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
2016-02-15 08:01:37 +00:00
"outputs": [],
2016-02-01 12:05:58 +00:00
"source": [
"7 in l # check if an element is in a list "
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
2016-02-15 08:01:37 +00:00
"outputs": [],
2016-02-01 12:05:58 +00:00
"source": [
"4 not in l # check if an element is not in a list"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
2016-02-15 08:01:37 +00:00
"outputs": [],
2016-02-01 12:05:58 +00:00
"source": [
"l.index(4) # search for an item"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
2016-02-15 08:01:37 +00:00
"outputs": [],
2016-02-01 12:05:58 +00:00
"source": [
"l.index(-1) # search for an item, error since it is not in the list"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"matrix = [[1,2], [3,4]] # matrix"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
2016-02-15 08:01:37 +00:00
"outputs": [],
2016-02-01 12:05:58 +00:00
"source": [
"matrix"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
2016-02-15 08:01:37 +00:00
"outputs": [],
2016-02-01 12:05:58 +00:00
"source": [
"matrix[0][0]"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
2016-02-15 08:01:37 +00:00
"outputs": [],
2016-02-01 12:05:58 +00:00
"source": [
"matrix[0][1]"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
2016-02-15 08:01:37 +00:00
"outputs": [],
2016-02-01 12:05:58 +00:00
"source": [
"type(matrix)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Tuples"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2016-02-16 10:38:05 +00:00
"Tuples are *inmutable* sequences, typically used to store collections of heterogeneous data.\n",
2016-02-01 12:05:58 +00:00
"\n",
"The main difference from lists is that lists are *mutable* while tuples are *inmutable*. Thus, tuples have not methods such as insert(), pop(), remove() or append() to change them.\n",
"\n",
"Tuples are faster than lists. Its main usage is when the collection is constant, or you do not want it can be changed (write protected). \n",
"\n",
"Tuples can be converted into lists and vice-versa, with the methods list() and tuple()."
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"tuple = ('a', 1)"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
2016-02-15 08:01:37 +00:00
"outputs": [],
2016-02-01 12:05:58 +00:00
"source": [
"tuple"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Tuples implement all the common [sequence operators](https://docs.python.org/3/library/stdtypes.html#typesseq-common), such as slicing, concatenation, len, etc."
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
2016-02-15 08:01:37 +00:00
"outputs": [],
2016-02-01 12:05:58 +00:00
"source": [
"tuple[::-1]"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
2016-02-15 08:01:37 +00:00
"outputs": [],
2016-02-01 12:05:58 +00:00
"source": [
"len(tuple)"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
2016-02-15 08:01:37 +00:00
"outputs": [],
2016-02-01 12:05:58 +00:00
"source": [
"tuple * 2 + ('b', 'c', 2.1, True)"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
2016-02-15 08:01:37 +00:00
"outputs": [],
2016-02-01 12:05:58 +00:00
"source": [
"tuple[1]"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
2016-02-15 08:01:37 +00:00
"outputs": [],
2016-02-01 12:05:58 +00:00
"source": [
"tuple[1] = 2 # Error, tuples are inmutable"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
2016-02-15 08:01:37 +00:00
"outputs": [],
2016-02-01 12:05:58 +00:00
"source": [
"type(tuple)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3. Range"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"A [range](https://docs.python.org/3/library/stdtypes.html#range) represents an immutable sequence of numbers. Ranges are created with two constructors: *range(stop)* or *range(start, stop, [step])*. \n",
"\n",
"The default *start* is 0 and the default *step* is 1. It generates a sequence [start, stop)"
]
},
{
"cell_type": "code",
2016-03-15 15:18:26 +00:00
"execution_count": 1,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"r = range(10)"
]
},
{
"cell_type": "code",
2016-03-15 15:18:26 +00:00
"execution_count": 2,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
2016-03-15 15:18:26 +00:00
"outputs": [
{
"data": {
"text/plain": [
"range(0, 10)"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
2016-02-01 12:05:58 +00:00
"source": [
"r"
]
},
{
"cell_type": "code",
2016-03-15 15:18:26 +00:00
"execution_count": 3,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
2016-03-15 15:18:26 +00:00
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
2016-02-01 12:05:58 +00:00
"source": [
"5 in r # check if a number is in a range"
]
},
{
"cell_type": "code",
2016-03-15 15:18:26 +00:00
"execution_count": 4,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
2016-03-15 15:18:26 +00:00
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
2016-02-01 12:05:58 +00:00
"source": [
"r[2] # Get a value"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
2016-02-15 08:01:37 +00:00
"outputs": [],
2016-02-01 12:05:58 +00:00
"source": [
"type(r)"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
2016-02-15 08:01:37 +00:00
"outputs": [],
2016-02-01 12:05:58 +00:00
"source": [
"list(range(10))"
]
},
{
"cell_type": "code",
2016-02-15 08:01:37 +00:00
"execution_count": null,
2016-02-01 12:05:58 +00:00
"metadata": {
"collapsed": false
},
2016-02-15 08:01:37 +00:00
"outputs": [],
2016-02-01 12:05:58 +00:00
"source": [
"list(range(1,10,2))"
]
},
{
"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",
2016-02-15 08:01:37 +00:00
"© 2016 Carlos A. Iglesias, Universidad Politécnica de Madrid."
2016-02-01 12:05:58 +00:00
]
}
],
"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",
2016-02-15 08:01:37 +00:00
"version": "3.5.1"
2016-02-01 12:05:58 +00:00
}
},
"nbformat": 4,
"nbformat_minor": 0
}