美文网首页自动化
Python使用过程容易混淆的坑

Python使用过程容易混淆的坑

作者: 佛系小懒 | 来源:发表于2020-02-06 15:11 被阅读0次

     区分join()及split()的使用

    join() 在字符串中加入分割字符,如','.join('12345')的结果为:‘1,2,3,4,5’

    split()将字符串通过分割字符分割成list,如: '1,2,3,4,5'.split(',')的结果为:[‘1’, ‘2’, ‘3’, ‘4’, ‘5’]

    filter()及map()的使用

    list(filter(lambda x:x>5,range(8)))结果是[6,7]

    list(map(list,x))的输出结果 [[‘a’, ‘b’], [‘c’, ‘d’]],map使其变成可迭代对象

    区别append() 及 extend()的使用

    append:list追加单个元素

    extend:list追加另一个list中的所有元素

    移除list的最后一个元素

    目标list.pop(-1)

    [1,2,5,10,3,100,9,24]中抽取>5的值构成list的2种方式

    nums=[1,2,5,10,3,100,9,24]

    newnums1=[i for i in nums if i>=5]

    newnums2=list(filter(lambda x:x>=5, nums))

    区分深拷贝与浅拷贝

    import copy

    b=copy.deepcopy(a)  #b的变化不会影响a取值

    b=copy.copy(a) #浅拷贝,拷贝的引用,b的变化会影响a的取值

    “aaa bbb ccc ddd eee”字符串中空格移除的3种方式

    s='aaa bbb ccc ddd eee'

    s1=''.join(s.split())

    s2=str(''.join(([i for i in s if i!=' '])))

    s3 = s.replace(' ','')

    区别(1)与(1,)的type

    b=(1) type(b):<class ‘int’>

    b=(1,) type(b):<class ‘tuple'>

    区别del及remove() 的使用

    del:通过index删除指定元素

    remove:通过value删除指定元素

    try ....except...else 语句,当没有异常发生时,else中的语句将会被执行:

    a=3

    for i in range(3):

        try:

            a = a - 1

            print(a)

        except Exception as e:

            print(e)

        else:

            print("正常运行")

    相关文章

      网友评论

        本文标题:Python使用过程容易混淆的坑

        本文链接:https://www.haomeiwen.com/subject/agtuxhtx.html