Repository navigation
Expand file tree
/
Copy pathLists.html
More file actions
142 lines (121 loc) · 15.8 KB
/
Copy pathLists.html
File metadata and controls
142 lines (121 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<html>
<head>
<title>NodeBox | Lists</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta http-equiv="imagetoolbar" content="no" />
<meta name="description" content="" />
<meta name="keywords" content="NodeBox, Lists" />
<link rel="canonical" href="https://www.nodebox.net/code/Lists" />
<link type="text/css" rel="stylesheet" media="screen" href="/media/css/nbar.css">
<script type="text/javascript" src="/code/js/pop.js"></script>
<script type="text/javascript" src="/code/js/confirm.js"></script>
<link href="/code/css/default.css" rel="stylesheet" type="text/css" />
<link href="/code/css/print.css" rel="stylesheet" type="text/css" media="print" />
</head>
<body id="body">
<div class="nbar">
<ol>
<li><a class="node" href="/code/Home">NodeBox 1<span class="nbar-arrow"></span></a>
<ol class="nbar-dropdown">
<li><a href="/">Homepage</a></li>
<li><a href="/node/">NodeBox 3<small>Node-based app for generative design and data visualization</small></a></li>
<li><a href="/opengl/">NodeBox OpenGL<small>Hardware-accelerated cross-platform graphics library</small></a></li>
<li><a href="/code/Home">NodeBox 1<small>Generate 2D visuals using Python code (Mac OS X only)</small></a></li>
</ol>
</li>
<li><a class="gallery" href="/gallery/">Gallery</a></li>
<li><a class="documentation" href="/code/Tutorial">Documentation</a></li>
<li><a class="forum" href="http://support.nodebox.net/discussions">Forum</a></li>
<li><a class="blog" href="/blog/">Blog</a></li>
</ol>
</div>
<div id="all">
<div id="header_and_navigation">
<div id="header">
<a href="/code/Home"><img id="header_image" src="/code/g/header-small.jpg" width="800" /></a></div>
<div id="title">
<a href="/code/Home"><h1>NodeBox</h1>
<strong><em>Create visual output with Python programming code</em></strong>
</a></div>
<div id="contextual">
<div id="languages">
</div>
<div id="search">
<form id="sf" method="get">
<input type="text" id="q" name="q" value="" />
<a href="javascript:document.getElementById('sf').submit();">GO</a>
</form>
</div>
</div>
<div id="navigation">
<div id="navigation_public">
<a href="/code/Home">Home</a>
<a href="/code/Download">Download</a>
<a href="/code/Reference">Reference</a>
<a href="/code/Tutorial">Tutorial</a>
<a href="/code/Library">Library</a>
<a href="/code/Gallery">Gallery</a>
<a href="/code/About">About</a>
</div>
</div>
</div>
<div id="content">
<div id="nodebox-link">
<a href="/code/Home"><img src="/code/g/transparent.gif" /></a>
</div>
<div class="inside">
<h3>Lists</h3>
<p><a href="/code/Variables">Variables</a> can hold entire lists of data. Lists are essential when your scripts increase in complexity: they are a way to group bits and pieces of information that belong together, for example: all the words to draw on screen, all the possible colors from which the script can choose, all the fonts from which the script can choose.</p> <p>A list in NodeBox is no different from a list in real life: it can hold a summary of stuff to do, groceries to buy, a sheet of names and adressess. The main difference is that in real life, we usually start counting from one, while lists in programming <strong>start from zero</strong>.</p> <p>The first element in a list has index zero, the second element in a list has index one.</p> <p><span class="media"><img src="/code/data/media/lists-groceries.jpg" alt="" width="500" height="250" /></span> </p> <h2>Creating a list</h2> <p>So, what does the code look like? Below is the grocery list defined in NodeBox. The list is defined between <strong>square brackets</strong>. Each item in the list is a piece of text (a string), therefore they are embedded in quotes. The list is assigned to a variable named <em>groceries</em>, but we know that variables can have any name we like, so it could be named <em>g</em> or <em>stufftodo</em> as well for that matter.</p> <pre class="python">groceries = <span style="">[</span><span style="color: #ff0080;">"apples"</span>, <span style="color: #ff0080;">"pears"</span>, <span style="color: #ff0080;">"carrots"</span>, <span style="color: #ff0080;">"cucumber"</span>, <span style="color: #ff0080;">"yoghurt"</span>, <span style="color: #ff0080;">"milk"</span><span style="">]</span>
<span style="color: #530035;">print</span> <span style="">len</span><span style="">(</span>groceries<span style="">)</span>
>>> <span style="">6</span></pre></pre> <p>A list can contain any kind of data: strings, numbers, and even lists.<br /> The len(<em>list</em>) command returns the number of items in a list.</p> <h2>Retrieving and editing items in a list</h2> <p>You can reference items in a list directly, by adding the index of the item you want to the variable name, between square brackets:</p> <pre class="python"><span style="color: #530035;">print</span> groceries<span style="">[</span><span style="">0</span><span style="">]</span>
>>> apples</pre></pre> <pre class="python">groceries<span style="">[</span><span style="">3</span><span style="">]</span> = <span style="color: #ff0080;">"banana"</span>
<span style="color: #530035;">print</span> groceries
>>> <span style="">[</span><span style="color: #ff0080;">"apples"</span>, <span style="color: #ff0080;">"pears"</span>, <span style="color: #ff0080;">"carrots"</span>, <span style="color: #ff0080;">"banana"</span>, <span style="color: #ff0080;">"yoghurt"</span>, <span style="color: #ff0080;">"milk"</span><span style="">]</span></pre></pre> <p>Don't forget: items in a list start with index zero. That's why in the above example the fourth element is replaced. The fourth element has index 3.</p> <h2>Adding items to the list</h2> <p>Data in a list can change over the course of time. New items can be added to it, or old items deleted from it, just like you can add stuff to buy to a grocery sticky-note, or scratch out things.</p> <table border="0"> <tbody><tr> <td> <img src="/code/data/media/lists-append.jpg" alt="" /> </td> <td><br /><br /> To append an item to the end of the list: <pre class="python">groceries.<span style="">append</span><span style="">(</span><span style="color: #ff0080;">"eggs"</span><span style="">)</span>
<span style="color: #530035;">print</span> groceries
>>> <span style="">[</span><span style="color: #ff0080;">"apples"</span>, <span style="color: #ff0080;">"pears"</span>,
>>> <span style="color: #ff0080;">"carrots"</span>, <span style="color: #ff0080;">"banana"</span>,
>>> <span style="color: #ff0080;">"yoghurt"</span>, <span style="color: #ff0080;">"milk"</span>,
>>> <span style="color: #ff0080;">"eggs"</span><span style="">]</span></pre></pre> To remove the eggs from the list: <pre class="python"><span style="color: #530035;">del</span> groceries<span style="">[</span><span style="">6</span><span style="">]</span></pre></pre> </td> </tr> </tbody></table> <p>Items can be inserted halfway the list with the <em>list</em>.insert(<em>i</em>, <em>item</em>) command, where the <em>i</em> parameter is the position where you want the element to be.</p> <h2>Looping through the list</h2> <p><em>Looping through a list</em> is the same as going to the mall and working off your grocery list top to bottom: the first item to collect are <em>apples</em>, once you have those, you need to fetch <em>pears</em>, and so on.</p> <p>Going through all the items in a list is one of the most essential things you do when working with data. You could retrieve the items in a list manually when there are only a few of them, but not when the list contains hundreds or even thousands of, for example, strings of text that you want to draw to the screen.</p> <table border="0"> <tbody><tr> <td> <img src="/code/data/media/lists-loop.jpg" alt="" /> </td> <td><br /><br /> Using the for-loop: <pre class="python"><span style="color: #530035;">for</span> item <span style="color: #530035;">in</span> groceries:
<span style="color: #530035;">print</span> <span style="color: #ff0080;">"buy some "</span> + item
>>> buy some apples
>>> buy some pears
>>> buy some carrots
>>> buy some banana
>>> buy some yoghurt
>>> buy some milk
>>> buy some eggs</pre></pre> </td> </tr> </tbody></table> <p>In the above example, the indented command(s) below the <em>for</em> command are executed for each item in the list. The <em>item</em> variable consecutively contains each item in the list. After that, the loop ends.</p> <h2>Random items from the list</h2> <p>You can let the code decide on its own which items to draw from the list by using the <a href="/code/Reference_|_choice()">choice()</a> command.</p> <h2>Example</h2> <p>This is a very braindead example of how you would use small lists to generate compositions. The script has a list of words it can use, and a list of colors it can use. After that, the script is free to act on its own: it takes random words and colors from the two lists and draws them to the screen in random sizes and rotations. It retains a certain coherence between different runs because of its limited words and colors.</p> <p> </p> <p><span class="media"><img src="/code/data/media/lists-loop1.jpg" alt="" width="132" height="132" /></span> <span class="media"><img src="/code/data/media/lists-loop2.jpg" alt="" width="132" height="132" /></span> <span class="media"><img src="/code/data/media/lists-loop3.jpg" alt="" width="132" height="132" /></span> <span class="media"><img src="/code/data/media/lists-loop4.jpg" alt="" width="132" height="132" /></span> <span class="media"><img src="/code/data/media/lists-loop5.jpg" alt="" width="132" height="132" /></span> <span class="media"><img src="/code/data/media/lists-loop6.jpg" alt="" width="132" height="132" /></span> <span class="media"><img src="/code/data/media/lists-loop7.jpg" alt="" width="132" height="132" /></span> <span class="media"><img src="/code/data/media/lists-loop8.jpg" alt="" width="132" height="132" /></span> <span class="media"><img src="/code/data/media/lists-loop9.jpg" alt="" width="132" height="132" /></span> <span class="media"><img src="/code/data/media/lists-loop10.jpg" alt="" width="132" height="132" /></span> <span class="media"><img src="/code/data/media/lists-loop11.jpg" alt="" width="132" height="132" /></span> <span class="media"><img src="/code/data/media/lists-loop12.jpg" alt="" width="132" height="132" /></span> <span class="media"><img src="/code/data/media/lists-loop13.jpg" alt="" width="132" height="132" /></span> <span class="media"><img src="/code/data/media/lists-loop14.jpg" alt="" width="132" height="132" /></span> <span class="media"><img src="/code/data/media/lists-loop15.jpg" alt="" width="132" height="132" /></span> <span class="media"><img src="/code/data/media/lists-loop16.jpg" alt="" width="132" height="132" /></span> </p> <p>The code:</p> <pre class="python">words = <span style="">[</span><span style="color: #ff0080;">"Lists"</span>, <span style="color: #ff0080;">"are"</span>, <span style="color: #ff0080;">"fun"</span><span style="">]</span>
colors = <span style="">[</span><span style="color: #530035;">color</span><span style="">(</span><span style="">1</span>,<span style="">0</span>,<span style="">0</span><span style="">)</span>, <span style="color: #530035;">color</span><span style="">(</span><span style="">1</span>,<span style="">1</span>,<span style="">1</span><span style="">)</span>, <span style="color: #530035;">color</span><span style="">(</span><span style="">0</span>,<span style="">0</span>,<span style="">0</span><span style="">)</span><span style="">]</span>
<span style="color: #530035;">for</span> i <span style="color: #530035;">in</span> <span style="">range</span><span style="">(</span><span style="">40</span><span style="">)</span>:
x = <span style="color: #530035;">random</span><span style="">(</span><span style="color: #530035;">WIDTH</span><span style="">)</span>
y = <span style="color: #530035;">random</span><span style="">(</span><span style="color: #530035;">HEIGHT</span><span style="">)</span>
<span style="color: #530035;">rotate</span><span style="">(</span><span style="color: #530035;">random</span><span style="">(</span><span style="">360</span><span style="">)</span><span style="">)</span>
<span style="color: #530035;">fontsize</span><span style="">(</span><span style="color: #530035;">random</span><span style="">(</span><span style="">10</span>,<span style="">100</span><span style="">)</span><span style="">)</span>
<span style="color: #530035;">fill</span><span style="">(</span><span style="color: #530035;">choice</span><span style="">(</span>colors<span style="">)</span><span style="">)</span>
<span style="color: #530035;">text</span><span style="">(</span><span style="color: #530035;">choice</span><span style="">(</span>words<span style="">)</span>, x, y<span style="">)</span></pre></pre> <h2>Dictionaries</h2> <p>Dictionaries are extremely useful kinds of lists that have string indices, <em>keys</em>, that you define yourself. Each key is unique and references an item in the list. Dictionaries are created with <strong>curly braces</strong> instead of square brackets.</p> <pre class="python">heading = <span style="">{</span><span style="">}</span>
heading<span style="">[</span><span style="color: #ff0080;">"font"</span><span style="">]</span> = <span style="color: #ff0080;">"ConduitITC-Medium"</span>
heading<span style="">[</span><span style="color: #ff0080;">"size"</span><span style="">]</span> = <span style="">18</span>
heading<span style="">[</span><span style="color: #ff0080;">"fill"</span><span style="">]</span> = <span style="color: #530035;">color</span><span style="">(</span><span style="">1</span>,<span style="">0</span>,<span style="">0</span><span style="">)</span>
<span style="color: #530035;">print</span> heading.<span style="">keys</span><span style="">(</span><span style="">)</span>
>>> <span style="">[</span><span style="color: #ff0080;">"font"</span>, <span style="color: #ff0080;">"fill"</span>, <span style="color: #ff0080;">"size"</span><span style="">]</span>
<span style="color: #530035;">print</span> heading<span style="">[</span><span style="color: #ff0080;">"size"</span><span style="">]</span>
>>> <span style="">18</span>
<span style="color: #530035;">if</span> heading.<span style="">has_key</span><span style="">(</span><span style="color: #ff0080;">"fill"</span><span style="">)</span>: <span style="color: #530035;">print</span> <span style="color: #ff0080;">"found!"</span>
>>> found!</pre></pre> <h2>More on lists</h2> <p>There is a lot more stuff you can do with lists, like sorting and reversing, or writing filter functions for lists. The full documentation is <a href="http://docs.python.org/tut/node7.html#SECTION007100000000000000000">here</a>, in the Python documentation.</p>
</div>
</div>
<div id="footer">
Last modified: Jan 24 2007 | © 2004-2012 <a href="http://www.emrg.be/" class="noexternal">Experimental Media Research Group</a>
</div>
<img src="/code/g/footer.jpg" style="border:0" class="footer" width="800" height="334" />
</div>
<script>
try{e=document.getElementsByTagName("span");for(i=0;i<e.length;i++){if(e[i].className=="header_image"){src=e[i].getElementsByTagName("img")[0].src;document.getElementById("header_image").src=src;break;}}}catch(e){}
</script>
</body>
</html>