- 5.1 Introduction
- 5.2 Lists
- 5.3 Tuples
- 5.4 Unpacking Sequences
- 5.5 Sequence Slicing
- 5.6 del Statement
- 5.7 Passing Lists to Functions
- 5.8 Sorting Lists
- 5.9 Searching Sequences
- 5.10其他列表方法
- 5.11 Simulating Stacks with Lists
- 5.12 List Comprehensions
- 5.13 Generator Expressions
- 5.14 Filter, Map and Reduce
- 5.15 Other Sequence Processing Functions
- 5.16 Two-Dimensional Lists
- 5.17 Intro to Data Science: Simulation and Static Visualizations
- 5.18 Wrap-Up
Learn how to sort a list of tuples in Python.
Includes demonstrations of common Python list and tuple manipulations. You’ll see that lists (which are modifiable) and tuples (which are not) have many common capabilities. Each can hold items of the same or different types. Lists can dynamically resize as necessary, growing and shrinking at execution time. The Deitels discuss one-dimensional and two-dimensional lists.
Save 35% off the list price* of the related book or multi-format eBook (EPUB + MOBI + PDF) with discount codeARTICLE.
* Seeinformit.com/terms
Objectives
In this chapter, you’ll:
Create and initialize lists and tuples.
Refer to elements of lists, tuples and strings.
Sort and search lists, and search tuples.
Pass lists and tuples to functions and methods.
Use list methods to perform common manipulations, such as searching for items, sorting a list, inserting items and removing items.
Use additional Python functional-style programming capabilities, including lambdas and the functional-style programming operations filter, map and reduce.
Use functional-style list comprehensions to create lists quickly and easily, and use generator expressions to generate values on demand.
Use two-dimensional lists.
Enhance your analysis and presentation skills with the Seaborn and Matplotlib visualization libraries.
Outline
5.1Introduction
5.2Lists
5.3Tuples
5.4Unpacking Sequences
5.5序列切片
5.6delStatement
5.7Passing Lists to Functions
5.8Sorting Lists
5.9Searching Sequences
5.10Other List Methods
5.11Simulating Stacks with Lists
5.12List Comprehensions
5.13Generator Expressions
5.14Filter, Map and Reduce
5.15Other Sequence Processing Functions
5.16Two-Dimensional Lists
5.17Intro to Data Science: Simulation and Static Visualizations
5.17.1 Sample Graphs for 600, 60,000 and 6,000,000 Die Rolls
5.17.2 Visualizing Die-Roll Frequencies and Percentages
5.18Wrap-Up
5.1 Introduction
In the last two chapters, we briefly introduced the list and tuple sequence types for representing ordered collections of items.Collectionsare prepackaged data structures consisting of related data items. Examples of collections include your favorite songs on your smartphone, your contacts list, a library’s books, your cards in a card game, your favorite sports team’s players, the stocks in an investment portfolio, patients in a cancer study and a shopping list. Python’s built-in collections enable you to store and access data conveniently and efficiently. In this chapter, we discuss lists and tuples in more detail.
We’ll demonstrate common list and tuple manipulations. You’ll see that lists (which are modifiable) and tuples (which are not) have many common capabilities. Each can hold items of the same or different types. Lists candynamically resizeas necessary, growing and shrinking at execution time. We discuss one-dimensional and two-dimensional lists.
In the preceding chapter, we demonstrated random-number generation and simulated rolling a six-sided die. We conclude this chapter with our next Intro to Data Science section, which uses the visualization libraries Seaborn and Matplotlib to interactively develop static bar charts containing the die frequencies. In the next chapter’s Intro to Data Science section, we’ll present an animated visualization in which the bar chart changesdynamicallyas the number of die rolls increases—you’ll see the law of large numbers “in action.”