Python编程学习圈 2020-06-10

1、大概意思就是：

a = [5,3,1,5,4]b = [5,3]d = {}for i in b:d[i] = 0res =l = len(a)for i in range(l):if a[i] in d:res.append(i)print(res)

2、如图，输入为这样，输出为那样，写吧……

d = {"A":"-1","A-1":"A","A-2":"A","A-3":"A","A-2-1":"A-2","A-2-2":"A-2","A-2-3":"A-2"}res =def func(node):array.append(node[0])if node[1] == "-1":returnfunc((node[1],d[node[1]]))for i in d.items:array =func(i)string = "/".join(array[::-1])res.append("/"+string)for j in res:print(j)

3、这个题太经典了。最短路径和。

array = [[1,8,5,2],[4,1,7,3],[3,6,2,9]]x = len(array)y = len(array[0])dp = [[0 for i in range(y)] for j in range(x)]# 遍历顺序是每行内的每列。所以遍历array中第一行只执行到if，dp中第一行就确定了，然后再确定dp第二行。# 要注意两个边界条件for i in range(x):for j in range(y):if i == 0:dp[i][j] = array[i][j]elif j == 0:dp[i][j] = array[i][j] + min(dp[i-1][j], dp[i-1][j+1])elif j == y-1:dp[i][j] = array[i][j] + min(dp[i-1][j-1],dp[i-1][j])else:dp[i][j] = array[i][j] + min(dp[i-1][j-1],dp[i-1][j],dp[i-1][j+1])# [[1, 8, 5, 2],# [5, 2, 9, 5],# [5, 8, 4, 14]]print(min(dp[-1]))# 4