zoukankan      html  css  js  c++  java
  • python笔试题1-21

    1、如何实现对python列表去重并保持原先顺序

    答:再创建一个列表,依次循环原列表元素并判断,若当前元素不在新建列表中,则添加即可

    2、现在有两个元组(('a'),('b')),(('c'),('d')),请使用python中匿名函数,生成列表[{'a':'b'},{'c':'d'}]

    答:

    #答案一
    test = lambda t1,t2 :[{i:j} for i,j in zip(t1,t2)]
    print(test(t1,t2))
    #答案二
    print(list(map(lambda t:{t[0]:t[1]},zip(t1,t2))))
    #还可以这样写
    print([{i:j} for i,j in zip(t1,t2)])

    3、请给出二分查找的python示例代码

    答:

      

    def recursion_search(data_source, find_n):
    mid = len(data_source) // 2
    if len(data_source) >= 1:
    if find_n > data_source[mid]:
    recursion_search(data_source[mid + 1:], find_n)
    elif find_n < data_source[mid]:
    recursion_search(data_source[:mid], find_n)
    else:
    print(data_source[mid])
    else:
    print("not found !")

    4、在python字符串格式化中,%与.format的主要区别

    答:%中如果传数字,必须指定类型,而format不用

    5、*args,**kwargs在什么情况下会用到,请举例**kwargs应用

    答:*args,**kwargs一般用于函数传多个值,

      **kwargs示例def x(**kwargs):

              return kwargs

            x(a=1)

    6、x = 'foo'

      y = 2

      print(x+y)

      A.foo B.foo foo C.foo 2 D. an exception throw

    7、

    kvps = {'1':1,'2':2}
    theCopy = kvps
    kvps['1'] = 5
    sum = kvps['1'] + theCopy['1']
    print(sum)

    A.1 B.2 C.7 D.10

    8、sys.path.append('/root/mods')

      A.改变python启动路径 B.改变python当前工作目录  C.添加一个新的python模块搜索路径  D.从mods中移除所有文件夹

    9、

    country_counter = {}

    def addone(country):
    if country in country_counter:
    country_counter[country] += 1
    else:
    country_counter[country] = 1
    addone('China')
    addone('Japan')
    addone('china')
    A.0 B.1 C.2 D.3 E.4

    10、

    names1 = ['Amir','Barry','Chales','Dao']
    names2 = names1 #['Alice','Barry','Chales','Dao']
    names3 = names1[:]
    names2[0] = 'Alice'
    names3[1] = 'Bob'#['Alice','Bob','Chales','Dao']
    sum = 0
    for ls in (names1,names2,names3):
    if ls[0] == 'Alice':
    sum += 1
    if ls[1] == 'Bob':
    sum += 10
    print(sum)

    A.11 B.12 C.21 D.22 E.33

    11、

    d = lambda p:p*2
    t = lambda p:p*3
    x= 2
    x = d(x)
    x = t(x)
    x =d(x)
    print(x)
    答:x = 24

    12、

    x = True
    y = False
    z = False
    if x or y and z:
    print('yes')
    else:
    print('no')
    答:yes (and优先级大于or)

    13、

    python中如何实现touple与list转换

    答:touple(list)->touple     list(touple)->list

    14、

    请写出一段代码实现删除一个列表里重复的元素

    答:

    nl = []
    for i in l:
    if i in nl:
    continue
    nl.append(i)

    15、如何得到列表list的交集与差集

      答:list=l1&l2

        list=l1-l2

    16、以下代码会输出什么

    def ex(val,list=[]):
    list.append(val)
    return list
    list1 = ex(10)
    list2 = ex(123,[])
    list3 = ex('a')
    print(list1,list2,list3)
    答:[10, 'a'] [123] [10, 'a']

    17、python中如何书写可变参数和关键字参数

      答:可变参数要写在关键字参数后面

    18、什么是lambda表达式

      答:lambda函数是一个可以接收任意多个参数(包括可选参数)并且返回单个表达式值的函数

    19、re的match与search有什么区别

      答:match会从头开始匹配,头部不一样则失败,search会全文匹配

    20、1 or 2 与 1 and 2 分别输出什么,为什么

      答:输出1,2   

    #python 中的and从左到右计算表达式,若所有值均为真,则返回最后一个值,若存在假,返回第一个假值

    21、1 < (2 == 2)与1 < 2 == 2 输出什么,为什么

      答:flase,True原因同上

     

     
  • 相关阅读:
    SAP库存账龄分析报表
    elasticsearch 同义词配置搜索
    elasticsearch 上下文
    git 修改源路径 修改项目地址
    intellij IDEA 无限试用
    Kubernetes 安装Redis集群
    helm安装ingress
    安装Helm
    Kubernetes Rook + Ceph
    GIT 将工作区恢复到上次提交的内容 commit your changes or stash them before you can merge Your local changes to the following files would be overwritten by merge
  • 原文地址:https://www.cnblogs.com/fenglin0826/p/8442480.html
Copyright © 2011-2022 走看看