zoukankan      html  css  js  c++  java
  • 复习

    1. 在Python中遍历字典的时候能不能对字典本身做涉及键(key)的操作
    2.问: 字符串格式化:%和format 有什么区别?

    Python新版本推荐使用format.
    Python2.6 新加入的format语法支持.
    3.6加入了一个 f-strings新特性
    3问结果

    def foo(arg,li=[]):
        li.append(arg)
    return li

    list1 = foo(21)
    list2 = foo(21,[1,])
    list3 = foo(28)

    print(list1)
    print(list2)
    print(list3)

      [21, 28]
      [1, 21]
      [21, 28]

    ***可变对象不能作为函数参数
     return li.append(arg)
    ***append操作没有返回值
    # 打乱列表的顺序
    ***random.shuffle(list)
    去除多余的嵌套列表 
    list1 = [11, [22, 3], [4, ], [55, 66], 8, [9, [7, [12, [34, [26]]]]]]
    # 去除多余嵌套的列表,得到[11, 22, 3, 4, 55, 66, 8]

    # 小剥皮

    def func(x):
    return [a for b in x for a in func(b)] if isinstance(x, list) else [x]

    def f(x):
    ret = []
    for b in x:
    if isinstance(b, list):
    for a in f(b):
    ret.append(a)
    else:
    ret.append(b)
    return ret
    #
    list2 = [11, 22, [33, 44], [55, [66, 77]], [88, [99, [100, [200, [300]]]]]]
    # ret = f(list2)
    # print(ret)
    ret2 = func(list2)
    print(ret2)
  • 相关阅读:
    Jedis API操作Redis数据库
    Go开发环境安装与环境变量配置
    Java中使用md5进行hash运算
    oracle创建表空间、用户
    CentOS安装MySQL
    Go语言之快速排序
    软件包管理rpm和yum
    第十一节:configParse模块
    redis数据库
    tcpdump命令
  • 原文地址:https://www.cnblogs.com/l-jie-n/p/9595357.html
Copyright © 2011-2022 走看看