python爬虫(3)

上一次的代码结果如下:

当然会有一点点不一样是正常的表现,因为这个图本身使用随机数rand函数做的,用其他两种随机函数出来的结果也不会完全相同。

继上节这次带来的是数组的重塑和转置

1、一维数组的重塑

在NumPy模块中的reshape()函数可以在不改变数组元素内容和个数的情况下重塑数组形状

代码示例如下:

import numpy as np

c = np.array([1,2,3,2,3,4,4,5,6,9,8,4])

o =c.reshape(3,4)

p =c.reshape(4,3)

print(o)

print(p)

其中这里reshape(行数,列数)

结果如下:

2、多维数组的重塑

这里reshape也可以改变多维数组的形状

import numpy as np

c = np.array([1,2,3,2,3,4,4,5,6,9,8,4])

o =c.reshape(3,4)

p =o.reshape(4,3)

print(o)

print(p)

这里是将上个例子进行改变可以发现还是可以改变的

结果如下:

其中将多维数组改编为一维数组也是有办法的

flatten()和ravel()函数

示例如下:

import numpy as np

c = np.array([1,2,3,2,3,4,4,5,6,9,8,4])

o =c.reshape(3,4)

p =o.reshape(4,3)

print(o.flatten())

print(p.ravel())

结果如下:

数组的转置:

T属性:就是将数组的行和列进行交换

import numpy as np

c = np.array([1,2,3,2,3,4,4,5,6,9,8,4])

o =c.reshape(3,4)

print(o)

print(o.T)

结果如下:

当然有一个与T属性效果相同的函数叫做transpose()

例子如下:

import numpy as np

c = np.array([1,2,3,2,3,4,4,5,6,9,8,4])

o =c.reshape(3,4)

print(o)

c1=np.transpose(o)

print(c1)

结果如下:

为大家带来一串代码:

def lcs(str1, str2, dp):

    len1 = len(str1)

    len2 = len(str2)

    for i in range(1, len1+1):                          

        for j in range(1, len2+1):

            if str1[i-1] == str2[j-1]:                  

                dp[i][j] = dp[i-1][j-1] + 1

            else:

                dp[i][j] = max(dp[i-1][j], dp[i][j-1])

    return dp[len1][len2]                              

def getlcs(str1, str2, dp):

    i = len(str1)

    j = len(str2)

    res = " "

    while(i != 0 and j != 0):                          

        if(str1[i-1] == str2[j-1]):

            res += str1[i-1]

            i -= 1

            j -= 1

        else:

            if(dp[i][j] == dp[i-1][j]):                

                i -= 1

            else:

                j -= 1

    return res[::-1]                                    

str1 = "bdcaba"

str2 = "abcbda"

lenA = len(str1)

lenB = len(str2)

dp = [[0 for i in range(lenA+1)] for j in range(lenB+1)]  

length = lcs(str1, str2, dp)

print("最长公共子序列长度为:", length)

print("dp表为:", dp)

res = getlcs(str1, str2, dp)

print("最长公共子序列为:", res)

相关推荐

  1. python爬虫

    2024-03-13 01:10:02       65 阅读
  2. python爬虫

    2024-03-13 01:10:02       64 阅读
  3. python爬虫

    2024-03-13 01:10:02       42 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-13 01:10:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-13 01:10:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-13 01:10:02       82 阅读
  4. Python语言-面向对象

    2024-03-13 01:10:02       91 阅读

热门阅读

  1. Activiti工作流引擎:流程实例名称模糊查询

    2024-03-13 01:10:02       45 阅读
  2. react hook:useMemo

    2024-03-13 01:10:02       44 阅读
  3. linux设置开机启动慎用nohup

    2024-03-13 01:10:02       43 阅读
  4. PyTorch学习笔记(三)

    2024-03-13 01:10:02       39 阅读
  5. 程序员如何选择职业赛道?

    2024-03-13 01:10:02       53 阅读
  6. Armadillo:矩阵类、向量类、Cube类和泛型类

    2024-03-13 01:10:02       33 阅读
  7. Linux的目录结构(介绍主要的)

    2024-03-13 01:10:02       39 阅读
  8. Android主界面多Tab实现方式

    2024-03-13 01:10:02       44 阅读
  9. android so载入过程

    2024-03-13 01:10:02       39 阅读
  10. Vue3:ref和reactive实现响应式数据

    2024-03-13 01:10:02       45 阅读
  11. LeetCode--代码详解 146.LRU缓存

    2024-03-13 01:10:02       49 阅读
  12. Lwip之TCP服务端示例记录(1对1)

    2024-03-13 01:10:02       37 阅读
  13. swagger-ui页面设置接口请求头head参数

    2024-03-13 01:10:02       44 阅读