python 随堂小笔记

1、type()   获取数据类型 


2、isinstance()   

  • 传入两个参数,第一个是待确定的数据类型,第二个是指定的数据类型。

  • 最后返回一个布尔类型,true表示两参数类型一致,反之则不一致

  • 列如:
    a = "520"
    print(isinstance(a, str))  #返回true

     


3、条件表达式(三元操作符)

  • 例如:
    x, y = 4, 5
    if x < y:
        small = x
    else:
        small = y

     

  • 可以改进为:
    small = x if x < y else y

     


4、assert(断言)

  • 作用:当关键字后面的条件为假的时候,程序自动崩溃并抛出AssertionError的异常
  • 适用于:在程序中置入检查点,当需要确保程序中的某个条件一定为真才让程序正常工作
  • 例如:
    assert 3>4
    
    运行结果:
    Traceback (most recent call last):
      File "D:/******.py", line 1, in <module>
        assert 3>4
    AssertionError

     


5、向列表添加元素

①、append():添加单个元素

  • 例如:
    nameber = [1, 2, 3, 4]
    nameber.append(10)
    print(nameber)
    
    运行结果:
        [1, 2, 3, 4, 10]

     

②、extend():批量添加元素 

  • 例如:※注意:添加的参数必须是列表的形式
    nameber = [1, 2, 3, 4]
    nameber.extend([7,8,9])
    print(nameber)
    
    运行结果:
        [1, 2, 3, 4, 7, 8, 9]

     

③、insert():在指定位置插入一个元素 

  • 例如:
    nameber = [1, 2, 3, 4]
    nameber.insert(1, 10)
    print(nameber)
    
    运行结果:
        [1, 10, 2, 3, 4]

     

 ④、remove():从列表删除一个元素

  • 例如:※注意:传入的参数必须是列表里已有的元素值
    nameber = [1, 2, 3, 4]
    nameber.remove(2)
    print(nameber)
    
    运行结果:
        [1, 3, 4]

     

 ⑤、del:从列表删除一个元素

  • 例如:※注意:传入的参数是列表的下标
    nameber = [1, 2, 3, 4]
    del nameber[2]
    print(nameber)
    
    运行结果:
        [1, 2, 4]

     

⑥、 pop():从列表删除一个元素,并将删除的元素返回出来

  • 例如:※注意:pop()传入的参数是元素的下标,不传入参数默认删除最后一个元素。最后都会返回删除元素的值
    nameber = [7, 8, 9, 10, 11]
    print(nameber.pop(3))
    print(nameber)
    
    运行结果:
        10
        [7, 8, 9, 11]

     

⑦、reverse():倒序元素 

  • 例如:
    nameber = [1, 2, 3, 4, 5]
    nameber.reverse()
    print(nameber)
    
    运行结果:
        [5, 4, 3, 2, 1]

    6、在函数里修改全局变量

    count = 5
    def myfun():
        count = 10
        print(count)
    myfun()
    print(count)
    
    运行结果:    #无法修改全局变量的值
    10
    5
    
    
    count = 5
    def myfun():
        global count
        count = 10
        print(count)
    myfun()
    print(count)
    
    运行结果:    #加入了global关键字,使全局变量改变
    10
    10
    

    7、闭包

    def FaxX(x):
        def FaxY(y):
            return x*y
        return FaxY
    
    i = FaxX(8)
    print(i(5))
    
    print(FaxX(8)(5))
    
    print(FaxY(5))
    
    运行结果:
    ①:40
    ②:40
    ③:报错
    

    8、内部函数调用并修改外部函数变量的方法

    def FaxX():
        x = 5    #定义了一个非全局变量
        def FaxY():    #试图在函数内部的函数调用外部函数的变量
            x *= x    #因为全局变量与局部变量的关系问题,这里会重新申请一块名为x的变量,这个新的变量还没有赋值,所以会报错
            return x    
        return FaxY()
    
    print(FaxX())
    
    运行结果:
    报错
    
    
    解决方法①:    #这种方法相当于偷鸡,不推荐使用,推荐使用下面的方法
    def FaxX():
        x = [5]
        def FaxY():
            x[0] *= x[0]
            return x[0]
        return FaxY()
    
    print(FaxX())
    
    运行结果:
    25
    
    
    解决方法②:
    def FaxX():    #想要在内部函数调用外部函数的变量,需要添加关键字参数 nonlocal,类似于 global
        x = 5
        def FaxY():
            nonlocal x    #加一个nonlocal关键字参数,可以将非全局变量的外部函数传入内部改变值
            x *= x
            return x
        return FaxY()
    
    print(FaxX())
    
    运行结果:
    25

    9、lambda表达式:匿名函数

    def ds(x):
        return 2*x+1
    print(ds(5))
    
    f = lambda x:2*x+1    #lambda 匿名函数 不需要定义函数名;冒号左边为传入的参数,右边为表达式
    print(f(5))
    
    运行结果:
    11
    11

    10、filter函数:过滤器,接收两个参数(第一个为函数,第二个为序列)

    print(list(filter(None,[1,0,-1,False,True])))  #第一个参数为None,默认为过滤掉非True的元素
    
    '''
    代码演示:
    过滤掉偶数
    '''
    def odd(x): #返回偶数
        return x%2
    temp = range(10)    #得出从0-9的序列
    show = filter(odd,temp) #过滤掉偶数
    print(list(show))
    
    #下面为另一种更为快速的方法
    
    print(list(filter(lambda x : x%2, range(10))))
    
    运行结果:
    [1, -1, True]
    [1, 3, 5, 7, 9]
    [1, 3, 5, 7, 9]

    11、map函数:会根据提供的函数对指定序列做映射,接收两个参数(第一个为函数,第二个为一个或多个序列)

    print(list(map(lambda x : x * 2, range(10))))   #map函数会将range(10)里面的每一个元素,传到第一个参数的函数里面,在输出最后的值

    12、递归求阶乘

    def opp(x):
        if x == 1:
            return 1
        else:
            return (opp(x-1) * x)
    
    print(opp(5))
    
    运行结果:
    120

    解析图:

13.斐波那契数列的迭代实现 

  • 案列如下 (使用迭代和递归)

    #递归方法(递归的层数比较多不建议使用,非常占用CPU)
    def opp(x):
        if x == 1:
            return 1
        elif x == 2:
            return 1
        else:
            return (opp(x-1)+ opp(x-2))
    print(opp(12))
    
    #迭代方法(不管数值的大小还是层数的多少,都可以很快出结果)
    a = 0
    b = 1
    for i in range(12):
        a,b = b,a+b
    print(a)
    
    运行结果:(上面两种结果都一样)
    144

    14.递归-汉诺塔

    def hannuota(n, a, b, c):
        if n == 1:
            print(a, '->', c)
        else:
            hannuota(n-1, a, c, b)#将前n-1个盘子从a移动到b上
            print(a,  '->', c) #将最底下的最后一个盘子从a移动到c上
            hannuota(n - 1, b, a, c)#将b上的n-1个盘子移动到c上
    n = 3
    hannuota(n, "a", "b", "c")
    
    运行结果:
    a -> c
    a -> b
    c -> b
    a -> c
    b -> a
    b -> c
    a -> c

    15.字典(映射)

  • dict(创建字典,其中之一)

dict3 = dict((('F',70),('i',105),('s',115),('h',104),('c',67)))
print(dict3)

运行结果:#dict只能传入一个值,传入的是映射
{'F': 70, 'i': 105, 's': 115, 'h': 104, 'c': 67}



dict3 = dict(F = 70, s = 115) #也可以传入关键字参数,出来的结果排序可能会变化
print(dict3)
运行结果:
{'F': 70, 's': 115}


#通过键改变值
dict3['F'] = 10
print(dict3)

运行结果:
{'F': 10, 's': 115}


#直接用键值添加进字典
dict3['h'] = 104
print(dict3)

运行结果:
{'F': 70, 's': 115, 'h': 104}
  • fromkeys():创建并返回一个新的字典,有两个参数,第一个是字典的键值,第二个为可选参数是对应键的值

dict1 = {}
print(dict1.fromkeys((1, 2, 3)))
print(dict1.fromkeys((1, 2, 3), 'number'))
print(dict1)

运行结果:
{1: None, 2: None, 3: None}
{1: 'number', 2: 'number', 3: 'number'}
{}

#缺陷:第二个参数会将其值赋值给每一个传入的键,即无法单独赋予不同的值,也无法修改字典的值
  •  keys():字典键的引用

    dict1 = {}
    dict1 = dict1.fromkeys(range(4), '赞')
    for eachKey in dict1.keys():
        print(eachKey)
    
    运行结果:    #打印出字典的键名
    0
    1
    2
    3
    

     

  • values():字典值的引用

dict1 = {}
dict1 = dict1.fromkeys(range(4), '赞')
for eachKey in dict1.values():
    print(eachKey)

运行结果:    #打印出字典的值
赞
赞
赞
赞
  •  items():打印字典的键值对

    dict1 = {}
    dict1 = dict1.fromkeys(range(4), '赞')
    for eachKey in dict1.items():
        print(eachKey)
    

     

  •  clear():清空字典

    dict1 = {}
    dict1 = dict1.fromkeys(range(4), '赞')
    print(dict1)
    dict1.clear()
    print(dict1)
    
    运行结果:
    {0: '赞', 1: '赞', 2: '赞', 3: '赞'}
    {}

     

  • copy():浅拷贝

    a = {1:'one', 2:'two', 3:'three'}
    b = a.copy()
    c = a
    print(a,id(a))
    print(b,id(b))
    print(c,id(c))
    
    运行结果:    #从下面的结果可以看出赋值是贴了一样的标签在相同的数据上,但是浅拷贝是把数据赋值到另一个位置;再次对c赋值可以改变a的值,但是无法改变b的值
    {1: 'one', 2: 'two', 3: 'three'} 1746192076872
    {1: 'one', 2: 'two', 3: 'three'} 1746192076944
    {1: 'one', 2: 'two', 3: 'three'} 1746192076872

     

  • pop():给定键弹出对应的值

    a = {1:'one', 2:'two', 3:'three', 4:'four'}
    print(a.pop(3))
    print(a)
    
    运行结果:
    three
    {1: 'one', 2: 'two', 4: 'four'}

     

  • popitem():给定键返回一个像

    a = {1:'one', 2:'two', 3:'three', 4:'four'}
    print(a.popitem())  #随机从字典里面弹出一个键值对
    print(a)
    
    运行结果:
    (4, 'four')
    {1: 'one', 2: 'two', 3: 'three'}

     

  • setdefault():用法与get类似,但是如果字典里没有查找的这个键值会自动添加

    a = {3:'three', 4:'four'}
    print(a)
    a.setdefault('小白')
    print(a)
    a.setdefault(5, "five")
    print(a)
    
    运行结果:
    {3: 'three', 4: 'four'}
    {3: 'three', 4: 'four', '小白': None}
    {3: 'three', 4: 'four', '小白': None, 5: 'five'}

      

  • update():利用一个字典或映射关系去更新另一个字典

    a = {3:'three', 4:'four'}
    b = {'小白':"狗"}
    a.update(b)
    print(a)
    
    运行结果:
    {3: 'three', 4: 'four', '小白': '狗'}

16.集合:里面所有的元素都具有唯一性,自动剔除重复的元素,集合也是无序的,不能索引集合中的某一个元素,案例如下:

#第一种创建集合的方法
set1 = {1,2,3,4,5,4,3,2,1}
print(set1)

运行结果:
{1, 2, 3, 4, 5}

#第二种创建集合的方法
set1 = set([1,2,3,2,1])
print(set1)

运行结果:
{1, 2, 3}

#试图索引集合里面的数据
set1 = set([1,2,3,2,1])
print(set1[2])

运行结果:
报错
  • 集合部分内置函数:

    #add():添加一个元素
    
    set1 = set([1,2,3,2,1])
    print(set1)
    set1.add(4)
    print(set1)
    
    运行结果:
    {1, 2, 3}
    {1, 2, 3, 4}
    
    
    
    #remove():移除一个元素
    
    set1 = set([1,2,3,2,1])
    print(set1)
    set1.remove(1)
    print(set1)
    
    运行结果:
    {1, 2, 3}
    {2, 3}

    16.不可变的集合(frozenset):不能增加或删除元素的集合

    set1 = frozenset([1,2,3,2,1])
    print(set1)
    set1.remove(1)
    print(set1)
    set1.add(6)
    print(set1)
    
    运行结果:
    frozenset({1, 2, 3})
    报错
    报错

    17.替换字符串(replace):

  • str.replace(old, new[, max])
  • old -- 将被替换的子字符串。
  • new -- 新字符串,用于替换old子字符串。
  • max -- 可选字符串, 替换不超过 max 次
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
GeoPandas是一个开源的Python库,旨在简化地理空间数据的处理和分析。它结合了Pandas和Shapely的能力,为Python用户提供了一个强大而灵活的工具来处理地理空间数据。以下是关于GeoPandas的详细介绍: 一、GeoPandas的基本概念 1. 定义 GeoPandas是建立在Pandas和Shapely之上的一个Python库,用于处理和分析地理空间数据。 它扩展了Pandas的DataFrame和Series数据结构,允许在其中存储和操作地理空间几何图形。 2. 核心数据结构 GeoDataFrame:GeoPandas的核心数据结构,是Pandas DataFrame的扩展。它包含一个或多个列,其中至少一列是几何列(geometry column),用于存储地理空间几何图形(如点、线、多边形等)。 GeoSeries:GeoPandas中的另一个重要数据结构,类似于Pandas的Series,但用于存储几何图形序列。 二、GeoPandas的功能特性 1. 读取和写入多种地理空间数据格式 GeoPandas支持读取和写入多种常见的地理空间数据格式,包括Shapefile、GeoJSON、PostGIS、KML等。这使得用户可以轻松地从各种数据源中加载地理空间数据,并将处理后的数据保存为所需的格式。 2. 地理空间几何图形的创建、编辑和分析 GeoPandas允许用户创建、编辑和分析地理空间几何图形,包括点、线、多边形等。它提供了丰富的空间操作函数,如缓冲区分析、交集、并集、差集等,使得用户可以方便地进行地理空间数据分析。 3. 数据可视化 GeoPandas内置了数据可视化功能,可以绘制地理空间数据的地图。用户可以使用matplotlib等库来进一步定制地图的样式和布局。 4. 空间连接和空间索引 GeoPandas支持空间连接操作,可以将两个GeoDataFrame按照空间关系(如相交、包含等)进行连接。此外,它还支持空间索引,可以提高地理空间数据查询的效率。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值