zoukankan      html  css  js  c++  java
  • Sequences of sequences

    I have focused on lists of tuples, but almost all the examples in this chapter also work with lists of lists, tuples of tuples, and tuples of lists. To avoid enumerating the possible combinations, it is sometimes easier to talk about sequences of sequences. In many contexts, the different kinds of sequences (strings, lists and tuples) can be used interchangeably. So how and why do you choose one over the others.

    To start with the obvious, strings are more limited than other sequences because the elements have to be characters. They are also immutable. If you need the ability to change the characters in a string (as opposed to creating a new string), you might want to use a list of characters instead.

    Lists are more common than tuples, mostly because they are mutable. But there are a few cases where you might prefer tuples:

    • In some contexts, like a return statement, it is syntactically simpler to create a tuple than a list.
    • If you want to use a sequence as a dictionary key, you have to use an immutable type like a tuple or string.
    • If you passing a sequence as an argument to a function, using tuples reduces the potential for unexpected behavior due to aliasing.

    Because tuples are immutable, they don’t provide methods like sort and reverse, which modify existing lists. But Python provides the built-in functions sorted and reversed, which take any sequence as a parameter and return a new list with the same elements in a different order.

                           

     

    from Thinking in Python

  • 相关阅读:
    基于Ubuntu + nextCloud 搭建自己的私人网盘
    基于Ubuntu部署 memcached 服务
    基于Ubuntu搭建Seafile专属网盘
    基于ubuntu搭建 Discuz 论坛
    基于ubuntu搭建 WordPress 个人博客
    基于CentOS 搭建 FTP 文件服务
    搭建 WordPress 个人博客
    Scale-out NAS 和scale-up NAS 系统的优缺点
    鱼缸的启示:Scale-out和Scale-up架构
    整死你个妖精,CDN西游捉妖记!
  • 原文地址:https://www.cnblogs.com/ryansunyu/p/3866439.html
Copyright © 2011-2022 走看看