官术网_书友最值得收藏!

4 Built-In Data Structures Part 1: Lists and Sets

Python has a rich collection of built-in data structures. These data structures are sometimes called "containers" or "collections" because they contain a collection of inpidual items. These structures cover a wide variety of common programming situations.

We'll look at an overview of the various collections that are built-in and what problems they solve. After the overview, we will look at the list and set collections in detail in this chapter, and then dictionaries in Chapter 5, Built-In Data Structures Part 2: Dictionaries.

The built-in tuple and string types have been treated separately. These are sequences, making them similar in many ways to the list collection. In Chapter 1, Numbers, Strings, and Tuples, we emphasized the way strings and tuples behave more like immutable numbers than like the mutable list collection.

The next chapter will look at dictionaries, as well as some more advanced topics also related to lists and sets. In particular, it will look at how Python handles references to mutable collection objects. This has consequences in the way functions need to be defined that accept lists or sets as parameters.

In this chapter, we'll look at the following recipes, all related to Python's built-in data structures:

  • Choosing a data structure
  • Building lists – literals, appending, and comprehensions
  • Slicing and dicing a list
  • Deleting from a list – deleting, removing, popping, and filtering
  • Writing list-related type hints
  • Reversing a copy of a list
  • Building sets – literals, adding, comprehensions, and operators
  • Removing items from a setremove(), pop(), and difference
  • Writing set-related type hints
主站蜘蛛池模板: 汤阴县| 镇沅| 永城市| 新晃| 兴化市| 凌海市| 庆安县| 阳泉市| 安徽省| 辽阳市| 区。| 麻栗坡县| 尚义县| 丹东市| 商南县| 沅陵县| 芜湖市| 城步| 弥勒县| 孝昌县| 准格尔旗| 竹北市| 织金县| 庆元县| 延川县| 分宜县| 新绛县| 石泉县| 赞皇县| 洞头县| 盐城市| 乃东县| 天水市| 建瓯市| 勐海县| 柏乡县| 永丰县| 铜陵市| 宿迁市| 淳安县| 贵溪市|