• 技术文章 >Python技术 >Python基础教程

    两道简单却实用的python面试题

    silencementsilencement2019-07-20 13:28:12原创2651

    题目一:python中String类型和unicode什么关系

    整理答案:string是字节串,而unicode是一个统一的字符集,utf-8是它的一种存储实现形式,string可为utf-8编码,也可编码为GBK等各种编码格式

    题目二:不用set集合方法,去除列表中的重复元素

    方法一:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    List=['b','b','d','b','c','a','a'] 

    print "the list is:" ,  List 

    if List: 

            List.sort() 

            last = List[-1] 

            for i in range(len(List)-2, -1, -1): 

                    if last==List[i]: 

                            del List[i] 

                    else: 

                            last=List[i] 

    print "after deleting the repeated element the list is : " , List

    方法二:使用列表综合

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    l1 = ['b','c','d','b','c','a','a'] 

    l2 = [] 

    [l2.append(i) for i in l1 if not i in l2] 

    print l2 

    题目三:实现斐波那契(Fibonacci)数列

    方法一:递归

    def fibonacci2(n): 

        if n == 1 or n == 2: 

            return 1 

        else: 

            return fibonacci2(n-1) + fibonacci2(n-2)

    方法二:迭代

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    def fibonacci(n): 

        if n == 1 or n == 2: 

            return 1 

        

        nPre = 1 

        nLast = 1 

        nResult = 0 

        i = 2 

        while i < n: 

            nResult = nPre + nLast 

            nPre = nLast 

            nLast = nResult 

            i += 1 

        

        return nResult 

        

    print fibonacci(5)

    专题推荐:面试题
    上一篇:教你如何用Python生成随机数字和随机字符串 下一篇:给Python初学者的一些技巧

    相关文章推荐

    • 解决python3 json数据包含中文的读写问题• 详解Python中%r和%s的区别及用法• 一张图让你学会Python

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网