# 3 using lists a list is another python sequence type

• Lab Report
• 29
• 100% (10) 10 out of 10 people found this document helpful

This preview shows page 5 - 8 out of 29 pages.

3. Using lists A list is another Python sequence type, similar to an array. It’s different from an array because the values that it contains can all have different types. A single list can contain int values, float values, and string values. Elements in a list can even be other lists! A list is created by giving a name to the list of values enclosed in square brackets and separated by commas. For example, values_with_different_types = [ ' data ' , 8, [ ' lab ' , 3]] . Lists can be useful when working with tables because they can describe the contents of one row in a table, which often corresponds to a sequence of values with different types. A list of lists can be used to describe multiple rows. Each column in a table is a collection of values with the same type (an array). If you create a table column from a list, it will automatically be converted to an array. A row, on the ther hand, mixes types. Here’s a table from Chapter 5. (Run the cell below.) In [20]: # Run this cell to recreate the table flowers = Table() . with_columns( ' Number of petals ' , make_array( 8 , 34 , 5 ), ' Name ' , make_array( ' lotus ' , ' sunflower ' , ' rose ' ) ) flowers Out[20]: Number of petals | Name 8 | lotus 34 | sunflower 5 | rose Question 3.1. Create a list that describes a new fourth row of this table. The details can be whatever you want, but the list must contain two values: the number of petals (an int value) and the name of the flower (a string). For example, your flower could be "pondweed"! (A flower with zero petals)
5
Out[21]: [3, ' lily ' ] In [22]: _ = ok . grade( ' q3_1 ' ) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Running tests --------------------------------------------------------------------- Test summary Passed: 1 Failed: 0 [ooooooooook] 100.0% passed Question 3.2. my_flower fits right in to the table from chapter 5. Complete the cell below to cre- ate a table of seven flowers that includes your flower as the fourth row followed by other_flowers as the last three rows. You can use with_row to create a new table with one extra row by passing a list of values and with_rows to create a table with multiple extra rows by passing a list of lists of values.
6
Passed: 1 Failed: 0 [ooooooooook] 100.0% passed 1.4 4. Analyzing datasets With just a few table methods, we can answer some interesting questions about the IMDb dataset. If we want just the ratings of the movies, we can get an array that contains the data in that column: In [25]: imdb . column( "Rating" ) Out[25]: array([8.4, 8.3, 8.3, 8.6, 8.2, 8.3, 8.1, 8.3, 8.2, 8. , 8.1, 8.2, 8.3, 8.3, 8.1, 8.4, 8.5, 8.2, 8.1, 8.4, 8.1, 8.1, 9.2, 8. , 8.2, 8.1, 8.2, 8.5, 8. , 8.3, 8.1, 8. , 8. , 8.3, 8.1, 8. , 8. , 8.3, 8.4, 8.1, 8.1, 8.5, 8.5, 8. , 8.3, 8.1, 8. , 8.6, 8.5, 8.3, 8.3, 8. , 8.2, 9.2, 8.2, 8.5, 8. , 8.9, 8.4, 8.2, 8.1, 8.3, 8.1, 8.1, 8.1, 8.3, 8.2, 8.3, 8.7, 8.3, 8.6, 8. , 8.1, 8.2, 8.5, 8.3, 8.9, 8. , 8.6, 8.3, 8.1, 8.7, 8.4, 8.1, 8.4, 8. , 8.5, 8.8, 8.2, 8.2, 8.5, 9. , 8. , 8. , 8.3, 8.4, 8.6, 8.5, 8.7, 8.4, 8.1, 8.1, 8.1, 8.7, 8.4, 8.9, 8.1, 8.2, 8. , 8.5, 8.5, 8. , 8. , 8.4, 8.1, 8.1, 8. , 8. , 8.3, 8.1, 8. , 8.3, 8. , 8. , 8. , 8. , 8. , 8. , 8. , 8.7, 8.3, 8. , 8. , 8.5, 8. , 8.1, 8.1, 8.1, 8.3, 8.2, 8.3, 8.9, 8.2, 8.2, 8. , 8.3, 8.2, 8.9, 8.5, 8.5, 8.1, 8.1, 8.5, 8.3, 8. , 8.2, 8.7, 8.3, 8.5, 8.1, 8.3, 8.2, 8.4, 8.1, 8.1, 8.1, 8. , 8.2, 8. , 8.6, 8.3, 8.2, 8. , 8.3, 8. , 8.2, 8. , 8.2, 8.8, 8.1, 8. , 8.1,