{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ " # Other built-in types\n", "\n", " Python has a lot of objects built-in. Here we are going to have a quick look at those you are most likely to use at the start.\n", "\n", " In this notebook, we'll see:\n", " - lists\n", " - tuples\n", " - dictionaries\n", " - copy vs. pointers\n", " - some interesting methods of these builtin types (including strings) as exercises." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ---\n", " # Lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " Lists look like arrays but are not arrays! Lists can contain elements of different types and are always 1 dimension.\n", "\n", " A list can probably have any object as an element: other lists, strings, numbers, arrays, functions etc. A list simply allows you to create a collection of objects. It doesn't inform you in any way how those objects are related to each other (or if indeed they are related)." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "First list length: 2\n", "I'm still 2 elements long: 2\n", "Easy to add elements to lists: [['a', 'b'], 3, 5]\n" ] } ], "source": [ "li = [] # Empty list\n", "li = ['a',1] # List of 2 elements\n", "print(\"First list length: \",len(li))\n", "li = [['a','b'],3] # A list can have another list as an element. But li is still a 2 elements list.\n", "print(\"I'm still 2 elements long: \", len(li))\n", "li.append(5)\n", "print(\"Easy to add elements to lists: \", li)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " Lists are indexable. Python also says \"subscriptable\".\n", " \n", " Lists are iterable. They can be used to build loops on." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['a', 'b']\n", "3\n", "5\n" ] } ], "source": [ "for n in li:\n", " print(n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "List elements can be changed, i.e lists are **mutable** objects:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['a', 'b']\n", "new\n", "['new', 3, 5]\n" ] } ], "source": [ "print(li[0])\n", "li[0]='new'\n", "print(li[0])\n", "print(li)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Strings are **immutable** objects. Its elements cannot be changed." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'str' object does not support item assignment", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"Claire\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0ma\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"Z\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mTypeError\u001b[0m: 'str' object does not support item assignment" ] } ], "source": [ "a=\"Claire\"\n", "a[0]=\"Z\"\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " If you have a subscriptable object as a list element, how do you access this element's subscripts? For example, you have the word \"new\" in the first element of a list and you want to know the last character:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'w'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "li[0][-1]" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "scrolled": true }, "outputs": [ { "ename": "TypeError", "evalue": "'int' object is not subscriptable", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Returns an error with non-subscriptable objects\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mli\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: 'int' object is not subscriptable" ] } ], "source": [ "# Returns an error with non-subscriptable objects\n", "li[1][-1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ## Convert to list\n", " You can convert an iterable object to a list with the builtin function: `list()`.\n", " Each element of the initial object will become an element in the list:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['C', 'l', 'a', 'i', 'r', 'e']" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a=\"Claire\"\n", "li=list(a)\n", "li" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Claire']" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "li=[a]\n", "li" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ---\n", " # Tuples\n", "\n", " Tuples are like lists but are immutable. That means elements of a tuple can't be changed, added, removed. It can be useful for a collection of objects you don't want to inadvertently change in your code. For example, a collection of names of experiments, paths, models etc. Although tuples are rarely used and people tend to simply use lists." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1,)" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t = () # Empty tuple\n", "t = (1,2,3)\n", "t = 4,5,6 # Parentheses are optional!\n", "t = 1, # Don't forget the comma to define a 1-element tuple!\n", "t" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'tuple' object does not support item assignment", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# As said earlier, they are immutable\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mt\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mt\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" ] } ], "source": [ "# As said earlier, they are immutable\n", "t=1,2,3\n", "t[0]=3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ## Convert to tuple\n", " Same as lists, you can convert iterable objects to tuples with the builtin function: `tuple()`." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(1, 3, 4)\n", "([1, 3, 4],)\n" ] } ], "source": [ "a = [1,3,4]\n", "b = tuple(a)\n", "print(b)\n", "# Here again if you want to keep the list as an element of the tuple:\n", "c = (a,)\n", "print(c)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ---\n", " # Copy or not?\n", " There is another consequence to all this talk about mutable or immutable. It has to do with how Python manages memory.\n", " To reduce the memory footprint of a program, Python will try to make pointers instead of copying the same values at different places in memory.\n", " That means if 2 variables are the same mutable object both will change if an element of one is changed.\n", " If one of the object is assigned to something else, then only this object will change.\n", " Let's see what this means with an example for a list" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3] [2, 2, 3]\n" ] } ], "source": [ "# If we change the first element of one list, the other one changes too.\n", "li = [1,2,3]\n", "li2 = list(li)\n", "li2[0]=2\n", "print(li, li2)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2, 2, 3] [1, 2, 3]\n" ] } ], "source": [ "# If we reassign li to a different list (even if it's the same as initially), only li changes.\n", "li = [1,2,3]\n", "print(li2,li)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 2, 3]" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "li=[1,2,3]\n", "li[0]=2\n", "li" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " The easiest way to make a copy of a mutable object is with the `list()` and `dict()` functions (see below for `dict()`).\n", " These will only do a shallow copy of the object. It is usually enough.\n", " There are ways to make deep copies of mutable objects. That is rarely needed, we'll see it at the end, depending on time.\n", "\n", " Since it is impossible to change only 1 element of an immutable object, any change made on those isn't reflected to other objects initially pointing to the same memory." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ---\n", " # Dictionaries\n", "\n", " Dictionaries allow you to label values. For example, if you'd like to keep track of a grid description (rectilinear), you might need a grid name, first lon/lat, the resolution, last lon/lat. A dictionary allows you to keep all those values together in one object and allows you to refer to each by name instead of position for example." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'name': 'my_grid',\n", " 'first_lon': -180.0,\n", " 'first_lat': -90.0,\n", " 'last_lon': 180.0,\n", " 'last_lat': 90.0,\n", " 'res': 0.5}" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d={\n", " \"name\":\"my_grid\",\n", " \"first_lon\":-180.,\n", " \"first_lat\":-90.,\n", " \"last_lon\":180.,\n", " \"last_lat\":90.,\n", " \"res\":0.5\n", "}\n", "d" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'my_grid'" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d[\"name\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " The values associated to each key can be very complicated objects. For example, we could have only \"first_point\" and \"last_point\" keys, each pointing to a dictionary with lat and lon as keys:" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'lon': -180.0, 'lat': -90.0} {'lon': 180.0, 'lat': 90.0}\n" ] } ], "source": [ "point0={\"lon\":-180., \"lat\":-90.}\n", "point1=dict(point0)\n", "point1[\"lon\"]=180.\n", "point1[\"lat\"]=90.\n", "print(point0, point1)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'lon': -180.0, 'lat': -90.0}" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d={'name':\"my_grid\", \"first_point\":point0, \"last_point\":point1, \"res\":0.5}\n", "d[\"first_point\"]" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-180.0" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d[\"first_point\"][\"lon\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ## Get the keys and values" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "name\n", "first_point\n", "last_point\n", "res\n", "End keys \n", "\n", "my_grid\n", "{'lon': -180.0, 'lat': -90.0}\n", "{'lon': 180.0, 'lat': 90.0}\n", "0.5\n", "End values \n", "\n", "name my_grid\n", "first_point {'lon': -180.0, 'lat': -90.0}\n", "last_point {'lon': 180.0, 'lat': 90.0}\n", "res 0.5\n", "End pairs \n", "\n", "('name', 'my_grid')\n", "('first_point', {'lon': -180.0, 'lat': -90.0})\n", "('last_point', {'lon': 180.0, 'lat': 90.0})\n", "('res', 0.5)\n" ] } ], "source": [ "# Get the keys in a dictionary:\n", "for k in d.keys():\n", " print(k)\n", "print(\"End keys \\n\")\n", "\n", "# Get the values:\n", "for v in d.values():\n", " print(v)\n", "print(\"End values \\n\")\n", "\n", "# Get the pairs of keys and values:\n", "for k,v in d.items():\n", " print(k,v)\n", "print(\"End pairs \\n\")\n", "\n", "# As you can see here, loops in Python can have several loop variables!\n", "# If we give only 1 loop variable:\n", "for k in d.items():\n", " print(k)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ## Add a (key,value) pair" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'name': 'my_grid',\n", " 'first_point': {'lon': -180.0, 'lat': -90.0},\n", " 'last_point': {'lon': 180.0, 'lat': 90.0},\n", " 'res': 0.5,\n", " 'projection': 'cartesian'}" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d['projection'] = \"cartesian\"\n", "d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " If you have several grids you want to keep track of, each key could have a list of values (or tuples). Or you could create a list of dictionaries. So you have 1 dictionary per grid and you keep them all together in a list (or tuple). The second option might be better as it allows you to create a dictionary for a new grid more easily by simply appending its definition dictionary to the list." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ---\n", " # Exercises\n", " Below are a few exercises or examples for you to go through.\n", "\n", " Don't forget to use either the inline help (?var or `tab`) or Google and Stack Overflow." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ## String formatting with dictionaries\n", " We saw earlier that f-strings are the most readable way to format a string.\n", "\n", " With dictionaries, `str.format()` can be quite powerful and useful:" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'name': 'my_grid', 'first_point': [-180.0, -90.0], 'last_point': [180.0, 90.0], 'res': 0.5}\n", "My grid my_grid has 0.5 degrees resolution, starts at [-180.0, -90.0], ends at [180.0, 90.0]\n", "My grid my_grid has 0.5 degrees resolution, starts at [-180.0, -90.0], ends at [180.0, 90.0]\n" ] } ], "source": [ "# Let's define a dictionary:\n", "point0=[-180., -90.]\n", "point1=[180.,90.]\n", "d={'name':\"my_grid\", \"first_point\":point0, \"last_point\":point1, \"res\":0.5}\n", "print(d)\n", "# Let's print all this information nicely with f-string and then `str.format()`\n", "print(f\"My grid {d['name']} has {d['res']} degrees resolution, starts at {d['first_point']}, ends at {d['last_point']}\")\n", "print(\"My grid {name} has {res} degrees resolution, starts at {first_point}, ends at {last_point}\".format(**d))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `**` operator allows us to take a dictionary of key-value pairs and unpack it into keyword arguments in a function call. It can be quite useful in other functions. For example to define plot characteristics instead of having a long line of arguments. Or to easily use the same arguments for different functions.\n", "\n", "There is also a `*` operator.\n", "See this page for details: https://treyhunner.com/2018/10/asterisks-in-python-what-they-are-and-how-to-use-them/\n", "This page is using a few things we haven't seen but it's very thorough." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ---\n", " ## Split and join strings\n", " If reading tabulated data or a filename with fields separated by a separator ('\\_', space, etc.), it's common one might want to split the different fields into separate strings or values.\n", " Inversely, you might want to create files that have fields separated by '\\_'. You can use `+` to add the different fields, but it gets long pretty quickly. Python provides a better way.\n", " \n", " #### Split\n", " Split the following string along the \"_\" separator. Hint: check the string's methods. \n", " \n", " What is the type of the result?\n", "
\n", "
\n", "
\n", "
\n", " \n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n",
    "b=a.split(sep=\"_\")\n",
    "
\n", "
\n", "
\n", "
\n", "
" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['tas', 'day', 'MRI-ESM1', 'historical', 'r1i1p1', '18510101-18601231.nc']" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Split the following string along the \"_\" separator. Hint: check the string's methods. What is the type of the result?\n", "a=\"tas_day_MRI-ESM1_historical_r1i1p1_18510101-18601231.nc\"\n", "a.split(sep=\"_\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Join\n", "Join together the elements of the following list to form a filename with the same format as above.\n", "\n", "Again check the string's methods. Be careful how you specify the separator and the strings to join!\n", "
\n", "
\n", "
\n", "
\n", " \n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n",
    "c='.'.join(l[-2:])\n",
    "d = '-'.join([l[-3],c])\n",
    "f = l[:-3]\n",
    "f.append(d)\n",
    "\"_\".join(f)\n",
    "
\n", "
\n", "
\n", "
\n", "
" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "l = [\"ta\", \"day\", \"ACCESS\", \"historical\",\"r1i1p1\", \"18510101\", \"19001231\",\"nc\"]" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'18510101-19001231.nc'" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c='.'.join(l[-2:])\n", "d = '-'.join([l[-3],c])\n", "'-'.join([l[-3],'.'.join(l[-2:])])" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['ta', 'day', 'ACCESS', 'historical', 'r1i1p1', '18510101-19001231.nc']" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f = l[:-3]\n", "f.append(d)\n", "f" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'ta_day_ACCESS_historical_r1i1p1_18510101-19001231.nc'" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c='.'.join(l[-2:])\n", "d = '-'.join([l[-3],c])\n", "f = l[:-3]\n", "f.append(d)\n", "\"_\".join(f)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ---\n", " ## Zip function\n", " It allows you to zip together 2 iterables. So that the first elements of the 2 original iterables become together the 1st element of the result, etc.\n", "\n", " Obviously, it's much easier to see examples.\n" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[('C', 'C'), ('l', 'a'), ('a', 'r'), ('i', 'o'), ('r', 'u'), ('e', 'g')]" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a=\"Claire\"\n", "b=\"Carouge\"\n", "c=list(zip(a,b))\n", "c" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'name': 'John', 'age': 32, 'place': 'Paris'}" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Given the 2 lists below, create a dictionary where the keys come from the list \"keys\" and values from \"values\"\n", "keys=['name', 'age', 'place']\n", "values=['John', 32, 'Paris']\n", "d = dict(zip(keys,values))\n", "d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ---\n", " These are all the concepts we wanted to introduce.\n", " As a summary we've seen:\n", " - strings, lists, tuples, dictionaries\n", " - how to format strings with `str.format()` and f-strings\n", " - Builtin functions:\n", " - print()\n", " - len()\n", " - any()\n", " - all()\n", " - list()\n", " - tuple()\n", " - dict()\n", " - For loops\n", " - If constructs\n", " - A few useful methods and other builtin functions in the exercises.\n", " For additional builtin functions: https://docs.python.org/3/library/functions.html" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, ['a', 'b']]" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l=[1,2]\n", "a=['a','b']\n", "l.append(a)\n", "l" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 'a', 'b']" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l=[1,2]\n", "a=['a','b']\n", "l.extend(a)\n", "l" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'ta_day_ACCESS_historical_r1i1p1_18510101-19001231.nc'" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = [\"ta\", \"day\", \"ACCESS\", \"historical\",\"r1i1p1\", \"18510101\", \"19001231\",\"nc\"]\n", "c='.'.join(l[-2:])\n", "d = '-'.join([l[-3],c])\n", "\"_\".join([*l[:-3],d])" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['ta', 'day', 'ACCESS', 'historical', 'r1i1p1']\n", "ta day ACCESS historical r1i1p1\n", "ta day\n" ] } ], "source": [ "print(l[:-3])\n", "print(*l[:-3])\n", "print(l[0],l[1])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "file_extension": ".py", "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.6" }, "mimetype": "text/x-python", "name": "python", "npconvert_exporter": "python", "pygments_lexer": "ipython3", "version": 3 }, "nbformat": 4, "nbformat_minor": 2 }