python全栈开发day12

列表

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

  • 切片

  • 追加

  • 删除

  • 长度

  • 切片

  • 循环

      • 包含

         #######################列表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)
上一篇:翻译:使用 ASP.NET MVC 4, EF, Knockoutjs and Bootstrap 设计和开发站点 - 1


下一篇:sparkshell运行sql报错: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver