小编NON*_*her的帖子

生成器理解究竟是如何工作的?

生成器理解有什么作用?它是如何工作的?我找不到关于它的教程.

python

84
推荐指数
4
解决办法
5万
查看次数

如何停止While循环?

我写了while loop一个函数,但不知道如何阻止它.当它不满足其最终条件时,循环就永远不会发生.我怎么能阻止它?

def determine_period(universe_array):
    period=0
    tmp=universe_array
    while True:
        tmp=apply_rules(tmp)#aplly_rules is a another function
        period+=1
        if numpy.array_equal(tmp,universe_array) is True:
            break    #i want the loop to stop and return 0 if the 
                     #period is bigger than 12
        if period>12:  #i wrote this line to stop it..but seems it 
                       #doesnt work....help..
            return 0
        else:   
            return period
Run Code Online (Sandbox Code Playgroud)

python while-loop

12
推荐指数
2
解决办法
16万
查看次数

如何在NumPy中为矩阵添加保护环?

使用NumPy,矩阵A有n行和m列,我想在矩阵A上添加一个保护环.保护环全为零.

我该怎么办?使用重塑?但该元素不足以构成n + 1 m + 1矩阵.

还是等等?

提前致谢

我的意思是一个额外的单元环,总是包含0个环绕矩阵A.基本上有一个矩阵B有n + 2个m + 2列,其中第一行和第一列以及最后一行和列都是零,其余部分是与矩阵A相同

python numpy

2
推荐指数
2
解决办法
911
查看次数

如何使用NumPy(Python)截断矩阵

只是一个简单的问题,如果我有一个矩阵有n行和m列,我怎么能切断矩阵的4个边并返回一个新的矩阵?(新矩阵将具有n-2行m-2列).

提前致谢

python numpy

2
推荐指数
2
解决办法
1万
查看次数

关于python变量范围的一个小问题

我是python的初学者,有一个问题,对我来说很困惑.如果我先定义一个函数但在函数内我必须使用一个在下面另一个函数中定义的变量,我可以这样做吗?或者我如何将另一个函数的返回值导入函数?例如:

def hello(x,y):
    good=hi(iy,ix)
    "then do somethings,and use the parameter'good'."
    return something

def hi(iy,ix):
    "code"
    return good
Run Code Online (Sandbox Code Playgroud)

python variables scope

1
推荐指数
1
解决办法
770
查看次数

如何确定一个函数的周期

如果我有一个函数A,它可以在给定矩阵上应用某个规则来生成另一个矩阵,我将其称为原始矩阵的下一个状态,该函数也可以通过给定时间N确定矩阵的最终状态(对原点应用规则,并再次对原始矩阵的下一个状态应用规则,并应用规则应用规则... N次).

因此,假设对于给定的矩阵,将规则应用于其上5次,并且最终矩阵与原始矩阵相同,并且我们称该矩阵的周期为5.

而且我有另一个函数B,我怎样才能使functionB能够在functionA的相同规则下确定给定函数的周期,并返回句点?我只是不知道如何开始制作它...谢谢预先.

def functionA(origin_matrix,N_times):
   #apply rule on the origin_matrix to generate another matrix which is the next sate of it.
   #apply rule on origin_matrix for N_times
   return the_final_matrix

def functionB(origin_matrix):
   #determine the period of the the origin_matrix.
   return period
Run Code Online (Sandbox Code Playgroud)

python math

1
推荐指数
1
解决办法
4195
查看次数

Python实现康威生命游戏的问题

我目前正在研究康威的生命游戏,并且已经陷入困境.我的代码不起作用.

当我在GUI中运行我的代码时,它说:

 
[[0 0 0 0]
 [0 1 1 0]
 [0 1 0 0]
 [0 0 0 0]]

Traceback (most recent call last):
  File "C:\Users\Documents\Physics\Python\MainProject\conway.py", line 53, in 
    b= apply_rules(a)
  File "C:\Users\Documents\Physics\Python\MainProject\conway.py", line 14, in apply_rules
    neighbours=number_neighbours(universe_array,iy,ix)
  File "C:\Users\Documents\Physics\Python\MainProject\conway.py", line 36, in number_neighbours
    neighbours+=1
UnboundLocalError: local variable 'neighbours' referenced before assignment

这是我的代码:

'''If a cell is dead at time T with exactly three live neighbours, the cell will be alive at T+1
If a cell is alive at …
Run Code Online (Sandbox Code Playgroud)

python

0
推荐指数
1
解决办法
1542
查看次数

标签 统计

python ×7

numpy ×2

math ×1

scope ×1

variables ×1

while-loop ×1