列表

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

  • 切片

  • 追加

  • 删除

  • 长度

  • 切片

  • 循环

      • 包含

         #######################列表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. 再战android-语音识别2(修改配置)

    可怕的半桶水一直在晃.程序中需要根据用户的选择设置语音识别的语言(目前科大讯飞支持英文.普通话.粤语),不想每次要用户去IatSetting中去改,需要能直接修改IatSetting的设置.之前移植的 ...

  2. JVM学习(4)——全面总结Java的GC算法和回收机制---转载自http://www.cnblogs.com/kubixuesheng/p/5208647.html

    俗话说,自己写的代码,6个月后也是别人的代码……复习!复习!复习!涉及到的知识点总结如下: 一些JVM的跟踪参数的设置 Java堆的分配参数 -Xmx 和 –Xms 应该保持一个什么关系,可以让系统的 ...

  3. Private表示该属性(方法)为只有本类内部可以访问(类内部可见)。

    Public表示该属性(方法)公开: (想用private还要用set和get方法供其他方法调用,这样可以保证对属性的访问方式统一,并且便于维护访问权限以及属性数据合法性) 如果没有特殊情况,属性一定 ...

  4. 【Nginx】配置及使用

    常用命令: nginx -t //可以查到配置是否正确,以及配置文件路径. 如果配置不生效 sudo killall -9 nginx 注意在配置文件中注明访问来源(例如没有写明ip,就不能通过ip直 ...

  5. dokcer使用--link 让容器相连

    在使用Docker的时候我们会常常碰到这么一种应用,就是我需要两个或多个容器,其中某些容器需要使用另外一些容器提供的服务.比如这么一种情况:我们需要一个容器来提供MySQL的数据库服务,而另外两个容器 ...

  6. python网络编程之UDP方式传输数据

    UDP --- 用户数据报协议(User Datagram Protocol),是一个无连接的简单的面向数据报的运输层协议. UDP不提供可靠性,它只是把应用程序传给IP层的数据报发送出去,但是并不能 ...

  7. sqlzoo需要知道的那些事

    1.“Bahamas 巴哈馬”中有三個 a,還有嗎?找出所有國家,其名字包括三個或以上的a. SELECT name FROM world WHERE name LIKE '%a%a%a%' 2.“I ...

  8. 【转载】eclipse常用插件在线安装地址或下载地址

    一,反编译插件: A.Jadclipse 1.打开eclipse增加站点:http://jadclipse.sf.net/update,在线安装好JDT Decompiler 3.4.0 2.http ...

  9. C#WinForm应用程序中嵌入ECharts图表

    C#WinForm应用程序中嵌入ECharts图表 程序运行效果: 下载ECharts: 官网下载ECharts :http://echarts.baidu.com/download.html 或者直 ...

  10. [原]openstack-kilo--issue(十一)Failed connect to 169.254.169.254:80; No route to host

    本博客已经添加"打赏"功能,"打赏"位置位于右边栏红色框中,感谢您赞助的咖啡. # curl http://169.254.169.254/latest/use ...