列表

创建列表:
基本操作:
  • 索引

  • 切片

  • 追加

  • 删除

  • 长度

  • 切片

  • 循环

      • 包含

         #######################列表list类中提供的方法########################
        list2 = [ 'x','y','i','o','i','a','o','u','i']
        # 1、append() 在列表后面追加元素
        list1 = ['Google', 'Runoob', 'Taobao']
        list1.append(123)
        print (list1)
        list2.append('xiong')
        print(list2)
        # 2、clear() 清空列表
        list3 = [1,2,3,4,5]
        list3.clear()
        print(list3)
        # 3、copy() 浅拷贝
        list4 = list3.copy()
        print(list4)
        # 4、计算元素11出现的次数
        print(list2.count('i'))
        # 5、extend(iterable) 传入可迭代对象(将可迭代对象一个个加入到列表中) 扩展列表
        list1 = ['xjt','xd','xe']
        list2 = [11,22,'xiong','zhang','zhou',True,22]
        list2.extend(list1)
        print(list2) #--->[11, 22, 'xiong', 'zhang', 'zhou', True,22, 'xjt', 'xd', 'xe']
        # 6、index() 根据值获取索引位置 找到停止,左边优先
        print(list2.index(22)) #默认从0位置开始找
        print(list2.index(22,3)) #从第3个位置找22
        # 7、pop() 在指定索引位置插入元素
        list0 = [11,22,33]
        list0.insert(1,'xiong')
        print(list0)
        # 8、pop() 默认把列表最后一个值弹出 并且能获取弹出的值
        list0 = [11,22,33,'xiong',90]
        list0.pop()
        print(list0)
        list0.pop(2)
        print(list0)
        # 9、reversed() 将当前列表进行翻转
        list0 = [11,22,33,'xiong',90]
        list0.reverse()
        print(list0)
        # 10、sort() 排序,默认从小到大
        list1 = [11,22,55,33,44,2,99,10]
        list1.sort() #从小到大
        print(list1)
        list1.sort(reverse=True) #从大到小
        print(list1)
         class list(object):
        """
        list() -> new empty list
        list(iterable) -> new list initialized from iterable's items
        """
        def append(self, p_object): # real signature unknown; restored from __doc__
        """ L.append(object) -- append object to end """
        pass def count(self, value): # real signature unknown; restored from __doc__
        """ L.count(value) -> integer -- return number of occurrences of value """
        return 0 def extend(self, iterable): # real signature unknown; restored from __doc__
        """ L.extend(iterable) -- extend list by appending elements from the iterable """
        pass def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
        """
        L.index(value, [start, [stop]]) -> integer -- return first index of value.
        Raises ValueError if the value is not present.
        """
        return 0 def insert(self, index, p_object): # real signature unknown; restored from __doc__
        """ L.insert(index, object) -- insert object before index """
        pass def pop(self, index=None): # real signature unknown; restored from __doc__
        """
        L.pop([index]) -> item -- remove and return item at index (default last).
        Raises IndexError if list is empty or index is out of range.
        """
        pass def remove(self, value): # real signature unknown; restored from __doc__
        """
        L.remove(value) -- remove first occurrence of value.
        Raises ValueError if the value is not present.
        """
        pass def reverse(self): # real signature unknown; restored from __doc__
        """ L.reverse() -- reverse *IN PLACE* """
        pass def sort(self, cmp=None, key=None, reverse=False): # real signature unknown; restored from __doc__
        """
        L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
        cmp(x, y) -> -1, 0, 1
        """
        pass def __add__(self, y): # real signature unknown; restored from __doc__
        """ x.__add__(y) <==> x+y """
        pass def __contains__(self, y): # real signature unknown; restored from __doc__
        """ x.__contains__(y) <==> y in x """
        pass def __delitem__(self, y): # real signature unknown; restored from __doc__
        """ x.__delitem__(y) <==> del x[y] """
        pass def __delslice__(self, i, j): # real signature unknown; restored from __doc__
        """
        x.__delslice__(i, j) <==> del x[i:j] Use of negative indices is not supported.
        """
        pass def __eq__(self, y): # real signature unknown; restored from __doc__
        """ x.__eq__(y) <==> x==y """
        pass def __getattribute__(self, name): # real signature unknown; restored from __doc__
        """ x.__getattribute__('name') <==> x.name """
        pass def __getitem__(self, y): # real signature unknown; restored from __doc__
        """ x.__getitem__(y) <==> x[y] """
        pass def __getslice__(self, i, j): # real signature unknown; restored from __doc__
        """
        x.__getslice__(i, j) <==> x[i:j] Use of negative indices is not supported.
        """
        pass def __ge__(self, y): # real signature unknown; restored from __doc__
        """ x.__ge__(y) <==> x>=y """
        pass def __gt__(self, y): # real signature unknown; restored from __doc__
        """ x.__gt__(y) <==> x>y """
        pass def __iadd__(self, y): # real signature unknown; restored from __doc__
        """ x.__iadd__(y) <==> x+=y """
        pass def __imul__(self, y): # real signature unknown; restored from __doc__
        """ x.__imul__(y) <==> x*=y """
        pass def __init__(self, seq=()): # known special case of list.__init__
        """
        list() -> new empty list
        list(iterable) -> new list initialized from iterable's items
        # (copied from class doc)
        """
        pass def __iter__(self): # real signature unknown; restored from __doc__
        """ x.__iter__() <==> iter(x) """
        pass def __len__(self): # real signature unknown; restored from __doc__
        """ x.__len__() <==> len(x) """
        pass def __le__(self, y): # real signature unknown; restored from __doc__
        """ x.__le__(y) <==> x<=y """
        pass def __lt__(self, y): # real signature unknown; restored from __doc__
        """ x.__lt__(y) <==> x<y """
        pass def __mul__(self, n): # real signature unknown; restored from __doc__
        """ x.__mul__(n) <==> x*n """
        pass @staticmethod # known case of __new__
        def __new__(S, *more): # real signature unknown; restored from __doc__
        """ T.__new__(S, ...) -> a new object with type S, a subtype of T """
        pass def __ne__(self, y): # real signature unknown; restored from __doc__
        """ x.__ne__(y) <==> x!=y """
        pass def __repr__(self): # real signature unknown; restored from __doc__
        """ x.__repr__() <==> repr(x) """
        pass def __reversed__(self): # real signature unknown; restored from __doc__
        """ L.__reversed__() -- return a reverse iterator over the list """
        pass def __rmul__(self, n): # real signature unknown; restored from __doc__
        """ x.__rmul__(n) <==> n*x """
        pass def __setitem__(self, i, y): # real signature unknown; restored from __doc__
        """ x.__setitem__(i, y) <==> x[i]=y """
        pass def __setslice__(self, i, j, y): # real signature unknown; restored from __doc__
        """
        x.__setslice__(i, j, y) <==> x[i:j]=y Use of negative indices is not supported.
        """
        pass def __sizeof__(self): # real signature unknown; restored from __doc__
        """ L.__sizeof__() -- size of L in memory, in bytes """
        pass __hash__ = None

        元祖

        创建元祖:
        1
        2
        3
        ages = (1122334455)
        ages = tuple((1122334455))
        ##区别元组与函数的参数括号:元组创建时在最后面多加一个逗号,   函数参数括号中多加一个逗号会报错
        基本操作:
        • 索引
        1. tu = (111,222,'alex',[(33,44)],56)
        2. v = tu[2]
        • 切片
        1. v = tu[0:2]
        • 循环

        for item in tu:

          print(item)    

        • 长度
        • 包含
           ######tuple元组########
          #元组 元素不能别修改 不能增加 删除
          s = 'adfgadhs12a465ghh'
          li = ['alex','xiong',123,'xiaoming']
          tu = ("wang","zhou","zhang") print(tuple(s))
          print(tuple(li))
          print(list(tu))
          print("_".join(tu))
          ##注意:当有数字时 不能join()
          # tu1 = [123,"xiaobai"]
          # print("_".join(tu1))
          """列表中也能扩增元组"""
          li.extend((11,22,33,'zhu',))
          print(li)
          """元组的一级元素不可修改 """
          tu = (11,22,'alex',[(1123,55,66)],'xiong',True,234)
          print(tu[3][0][1])
          print(tu[3])
          tu[3][0] = 567
          print(tu)
          """元组tuple的一些方法"""
          tu = (11,22,'alex',[(1123,55,66)],'xiong',True,234,22,23,22)
          print(tu.count(22)) #统计指定元素在元组中出现的次数
          print(tu.index(22)) #某个值的索引
          字典(无序)
          创建字典:
          1
          2
          3
          person = {"name""mr.wu"'age'18}
          person = dict({"name""mr.wu"'age'18})

          常用操作:

          • 索引
          • 新增
          • 删除
          • 键、值、键值对
          • 循环
          • 长度
             ######dict静态方法##########
            # @staticmethod # known case
            def fromkeys(*args, **kwargs): # real signature unknown
            """ Returns a new dict with keys from iterable and values equal to value. """
            pass def get(self, k, d=None): # real signature unknown; restored from __doc__
            """ D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None. """
            pass def items(self): # real signature unknown; restored from __doc__
            """ D.items() -> a set-like object providing a view on D's items """
            pass def keys(self): # real signature unknown; restored from __doc__
            """ D.keys() -> a set-like object providing a view on D's keys """
            pass def pop(self, k, d=None): # real signature unknown; restored from __doc__
            """
            D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
            If key is not found, d is returned if given, otherwise KeyError is raised
            """
            pass def popitem(self): # real signature unknown; restored from __doc__
            """
            D.popitem() -> (k, v), remove and return some (key, value) pair as a
            2-tuple; but raise KeyError if D is empty.
            """
            pass def setdefault(self, k, d=None): # real signature unknown; restored from __doc__
            """ D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D """
            pass def update(self, E=None, **F): # known special case of dict.update
            """
            D.update([E, ]**F) -> None. Update D from dict/iterable E and F.
            If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]
            If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v
            In either case, this is followed by: for k in F: D[k] = F[k]
            """
            pass def values(self): # real signature unknown; restored from __doc__
            """ D.values() -> an object providing a view on D's values """
            pass
             ##########字典#############
            """1、数字、字符串、列表、元组、字典都能作为字典的value,还能进行嵌套"""
            info1 = {
            "zhang":123,
            1:"wang",
            "k1":[12,34,{
            "kk1":12,
            "kk2":23
            }],
            "k2":(123,456,789,[12,{"kk3":12}]),
            "k3":{
            123:"li",
            "wang":[1,2,3]
            }
            }
            print(info1)
            """2、列表、字典不能作为字典的key"""
            info2 = {
            12:'sss',
            "k1":12334,
            True:"",
            (11,22):456,
            # [12,34]:741,
            # {"k2":22,"k3":33}:852
            }
            print(info2)
            """3、字典无序 可以通过key索引方式找到指定元素"""
            info2 = {
            12:'sss',
            "k1":12334,
            True:"",
            (11,22):456,
            }
            print(info2["k1"])
            """4、字典增删"""
            info2 = {
            12:'sss',
            "k1":12334,
            True:"",
            (11,22):456,
            "k2":{
            "kk1":12,
            "kk2":"jack"
            }
            }
            del info2[12]
            del info2["k2"]["kk1"]
            print(info2)
            """5、字典for循环"""
            info = {
            12:'sss',
            "k1":12334,
            True:"",
            (11,22):456,
            "k2":{
            "kk1":12,
            "kk2":"jack"
            }
            }
            # for循环获取keys
            for k in info:
            print(k) for k in info.keys():
            print(k)
            # for循环获取values
            for v in info.values():
            print(v)
            # for循环获取keys values
            for k,v in info.items():
            print(k,v) """fromkeys() 根据序列 创建字典 并指定统一的值"""
            v1 = dict.fromkeys(['k1',122,''])
            v2 = dict.fromkeys(['k1',122,''],123)
            print(v1,v2)
            """根据key获取字典值,key不存在时,可以指定默认值(None)"""
            info = {
            12:'sss',
            "k1":12334,
            True:"",
            (11,22):456,
            "k2":{
            "kk1":12,
            "kk2":"jack"
            }
            }
            #不存在key时 指定返回404,不指定时返回None
            v = info.get('k1',404)
            print(v)
            v = info.get('k111',404)
            print(v)
            v = info.get('k111')
            print(v)
            """pop() popitem() 删除并获取值"""
            info = {
            12:'sss',
            "k1":12334,
            True:"",
            (11,22):456,
            "k2":{
            "kk1":12,
            "kk2":"jack"
            }
            }
            #pop() 指定弹出key="k1" 没有k1时返回404
            #popitem() 随机弹出一个键值对
            # v = info.pop("k1",404)
            # print(info,v)
            k , v = info.popitem()
            print(info,k,v)
            """setdefault() 设置值 已存在不设置 获取当前key对应的值 ,不存在 设置 获取当前key对应的值"""
            info = {
            12:'sss',
            "k1":12334,
            True:"",
            (11,22):456,
            "k2":{
            "kk1":12,
            "kk2":"jack"
            }
            }
            v = info.setdefault('k123','zhejiang')
            print(info,v)
            """update() 已有的覆盖 没有的添加"""
            info.update({"k1":1234,"k3":"宁波"})
            print(info)
            info.update(kkk1=1111,kkk2=2222,kkk3="hz")
            print(info)
             """day13作业"""
            list1 = [11,22,33]
            list2 = [22,33,44]
            # a、获取相同元素
            # b、获取不同元素
            for i in list1:
            for j in list2:
            if i==j:
            print(i)
            for i in list1:
            if i not in list2:
            print(i) # 9*9乘法表 ##注意print() 默认end="\n" 换行 sep = " " 默认空格
            for i in range(1,10):
            for j in range(1,i+1):
            print(i,"*",j,"=",i*j,"\t",end="")
            print("\n",end="") for i in range(1,10):
            str1 = ""
            for j in range(1,i+1):
            str1 += str(j) + "*" + str(i) + "=" + str(i*j) + '\t'
            print(str1)

python全栈开发day12的更多相关文章

  1. python全栈开发-Day12 三元表达式、函数递归、匿名函数、内置函数

    一. 三元表达式 一 .三元表达式 仅应用于: 1.条件成立返回,一个值 2.条件不成立返回 ,一个值 def max2(x,y): #普通函数定义 if x > y: return x els ...

  2. Python全栈开发【面向对象进阶】

    Python全栈开发[面向对象进阶] 本节内容: isinstance(obj,cls)和issubclass(sub,super) 反射 __setattr__,__delattr__,__geta ...

  3. Python全栈开发【面向对象】

    Python全栈开发[面向对象] 本节内容: 三大编程范式 面向对象设计与面向对象编程 类和对象 静态属性.类方法.静态方法 类组合 继承 多态 封装 三大编程范式 三大编程范式: 1.面向过程编程 ...

  4. Python全栈开发【模块】

    Python全栈开发[模块] 本节内容: 模块介绍 time random os sys json & picle shelve XML hashlib ConfigParser loggin ...

  5. Python全栈开发【基础四】

    Python全栈开发[基础四] 本节内容: 匿名函数(lambda) 函数式编程(map,filter,reduce) 文件处理 迭代器 三元表达式 列表解析与生成器表达式 生成器 匿名函数 lamb ...

  6. Python全栈开发【基础三】

    Python全栈开发[基础三]  本节内容: 函数(全局与局部变量) 递归 内置函数 函数 一.定义和使用 函数最重要的是减少代码的重用性和增强代码可读性 def 函数名(参数): ... 函数体 . ...

  7. Python全栈开发【基础二】

    Python全栈开发[基础二] 本节内容: Python 运算符(算术运算.比较运算.赋值运算.逻辑运算.成员运算) 基本数据类型(数字.布尔值.字符串.列表.元组.字典) 其他(编码,range,f ...

  8. Python全栈开发【基础一】

    Python全栈开发[第一篇] 本节内容: Python 的种类 Python 的环境 Python 入门(解释器.编码.变量.input输入.if流程控制与缩进.while循环) if流程控制与wh ...

  9. python 全栈开发之路 day1

    python 全栈开发之路 day1   本节内容 计算机发展介绍 计算机硬件组成 计算机基本原理 计算机 计算机(computer)俗称电脑,是一种用于高速计算的电子计算机器,可以进行数值计算,又可 ...

随机推荐

  1. 【网络编程】——ne-snmp开发实例1

    net-snmp扩展有多种方式,在此只介绍两种——动态库扩展,静态库扩展. 在做net-snmp开发之前,首先确定net-snmp相关的软件是否安装. rpm -qa | grep snmp net- ...

  2. visio操作

    1.上下标:选中要成为上标的文字,ctrl+shift+"=" 选中要成为下标的文字,ctrl+"="

  3. TestNg学习

    参考:https://www.yiibai.com/testng/junit-vs-testng-comparison.html#article-start 1.JUnit缺点: 最初的设计,使用于单 ...

  4. Entity Framework 5中遇到的 mysql tinyint(1) 转换为 bool 的问题 (我用的是VS2013中的EF5版本)

    数据有一个字段,用的是 tinyint 长度是1  默认值为0 , 当用vs2013中的 EF5来生成 实体模型之后,看到这个列被标识为 bool 类型   Mysql官方参考文档关于布尔类型的说明: ...

  5. Stanford Corenlp学习笔记——词性标注

    使用Stanford Corenlp对中文进行词性标注 语言为Scala,使用的jar的版本是3.6.0,而且是手动添加jar包,使用sbt添加其他版本的时候出现了各种各样的问题 添加的jar包有5个 ...

  6. 安卓开发笔记——关于开源组件PullToRefresh实现下拉刷新和上拉加载(一分钟搞定,超级简单)

    前言 以前在实现ListView下拉刷新和上拉加载数据的时候都是去继承原生的ListView重写它的一些方法,实现起来非常繁杂,需要我们自己去给ListView定制下拉刷新和上拉加载的布局文件,然后添 ...

  7. QT 中Widgets-Scene3d例子学习

    QT中自带的例子widgets-scene3d实现在基于Widget的应用程序中使用qml 3d场景的功能,我在此基础上,将basicshapes-cpp的例子加以嵌入: 相关代码如下:  C++ C ...

  8. [Laravel] 08 - Auth & Data Migration

    登录注册框架 一.加载Auth模块 Step 1, 安装Auth模块 生成相关 laravel 框架内部的代码模块: $ php artisan make:auth 自动添加了路由代码 在larave ...

  9. 13组合模式Composite

    一.什么是组合模式 Composite模式也叫组合模式,是构造型的设 计模式之一.通过递归手段来构造树形的对象结 构,并可以通过一个对象来访问整个对象树. 二.组合模式的结构 三.组合模式的角色和职责 ...

  10. linux下WEB服务器安装、配置VSFTP

    转载  http://www.oicto.com/centos-vsftp/?tdsourcetag=s_pcqq_aiomsg linux下WEB服务器安装.配置VSFTP 由 admin · 发布 ...