私信  •  关注

Ibrahim

Ibrahim 最近创建的主题
Ibrahim 最近回复了
3 年前
回复了 Ibrahim 创建的主题 » 在python中返回最多n项的斐波那契数列[重复]

这个 rth 斐波那契的术语是 r-1th 术语+ r-2th 术语,其中r>0 所以你可以从一个初始列表开始 [0,1] 然后使用for循环遍历具有最大限制的自然数 n 和最小极限 1 然后使用列表中的索引来获取 r-1th 术语和 r-2 然后附加 rth 在名单上。我会这样做的。

intialise the list with elements 0,1
use a for loop here to loop through the natural numbers
get the r-1th from the list
get the r-2th from the list
add both
append the result you got, in the list you initialised
3 年前
回复了 Ibrahim 创建的主题 » Python-计算从object1(x和y)到object2(x和y)的角度

可以使用简单的三角法,首先计算object1和object2之间的水平距离,即 x_2 - x_1 然后计算垂直距离b/w object1和object2,即 y_2 - y_1 .水平距离是我们的基础,垂直距离是我们的垂线。 perpendicular/base = tan(θ) 现在倒过来 atan(perpendicular/base) = θ

在python中实现这一点的方法很简单,您可以使用 math.atan 为了达到你的目的 math.atan((y2-y1)/(x2-x1))

3 年前
回复了 Ibrahim 创建的主题 » Python阅读了一系列妄想中的妄想的第一个条目

您需要使用lookahead和lookback正则表达式来执行以下操作

s = "[('entryA', 'typeA'), ('entryB', 'typeB'), ('entryC', 'typeC'), ('entryD', 'typeD')]"
result = re.findall("(?<=\(').*?(?=',)", s)

print("\"entries\":",result)

展望未来: (?=EXPR) 查看元素正前方的内容。
回顾: (?<=EXPR) 查看元素的正后方。

这是因为 copy_matrix 这里没有局部变量,所以它引用 nonlocal 第6行出现变量ie copy_matrix = {} ,而 cur_max 它被定义为第24行的局部变量 cur_max = max(cur_max, copy_matrix[(r, c)]) ,它引用的是非局部变量 copy_matirx , rows , columns 但它引用的是局部变量 cur_max 因为它是在函数中定义的,并且在赋值之前被引用。你可能想做的是

def dfs(r, c, prev):
    nonlocal cur_max
    if (r < 0 or c < 0 or
        r == rows or c == columns or
        matrix[r][c] <= prev):
        return 0
    
    if (r, c) in copy_matrix:
        return copy_matrix[(r, c)]
    
    max_length = 0
    max_length = max(max_length, 1 + dfs(r + 1, c, matrix[r][c]))
    max_length = max(max_length, 1 + dfs(r - 1, c, matrix[r][c]))
    max_length = max(max_length, 1 + dfs(r, c + 1, matrix[r][c]))
    max_length = max(max_length, 1 + dfs(r, c - 1, matrix[r][c]))
    copy_matrix[(r, c)] = max_length
    cur_max = max(cur_max, copy_matrix[(r, c)])
    
    return max_length

read more