我遇到了一个问题:我正在运行一个循环来处理多个文件.我的矩阵是巨大的,因此如果我不小心,我经常会失去记忆.
如果创建了任何警告,有没有办法摆脱循环?它只是继续运行循环并报告它失败了很久......令人讨厌.任何想法哦明智的stackoverflow-ers ?!
我有一个文件名列表:
names = ['aet2000','ppt2000', 'aet2001', 'ppt2001']
Run Code Online (Sandbox Code Playgroud)
虽然我找到了一些可以用来grep字符串的函数,但我还没弄清楚如何grep列表中的所有元素.
比如我想:
grep(names,'aet')
Run Code Online (Sandbox Code Playgroud)
得到:
['aet2000','aet2001']
Run Code Online (Sandbox Code Playgroud)
当然不是太难,但我是Python新手
更新 上面的问题显然不够准确.下面的所有答案都适用于示例,但不适用于我的实际数据.这是我的代码来制作文件名列表:
years = range(2000,2011)
months = ["jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"]
variables = ["cwd","ppt","aet","pet","tmn","tmx"] # *variable name* with wildcards
tifnames = list(range(0,(len(years)*len(months)*len(variables)+1) ))
i = 0
for variable in variables:
for year in years:
for month in months:
fullname = str(variable)+str(year)+str(month)+".tif"
tifnames[i] = fullname
i = i+1
Run Code Online (Sandbox Code Playgroud)
运行过滤器(lambda x:'aet'in x,tifnames)或其他答案返回:
Traceback (most recent call last):
File "<pyshell#89>", line 1, in <module>
func(tifnames,'aet')
File "<pyshell#88>", line 2, in func
return [i for …
Run Code Online (Sandbox Code Playgroud) 我正在尝试将数据分配给现有数据框,并在循环中生成名称.一个基本的例子可能是
A = data.frame(a = c(1,2,3), b=c(3,6,2))
for (i in 1:2){
name = paste("Name",i, sep="")
assign(name, c(6,3,2))
}
Run Code Online (Sandbox Code Playgroud)
现在我只需要弄清楚如何将name1和name2添加到data.frame A,同时保留其指定的名称.我确信有一个简单的答案,我现在就没有看到它.
最后我想结束
A
#a b name1 name2
#1 3 6 6
#2 6 3 3
#3 2 2 2
Run Code Online (Sandbox Code Playgroud)
但我需要以自动化的方式做到这一点.
例如,如果for循环可以适应
for (i in 1:2){
name = paste("Name",i, sep="")
assign(name, c(6,3,2)
A= cbind(A, get(paste(name,i,sep=""))) # works but doesn't maintain the column name as name1 or name2 etc
}
Run Code Online (Sandbox Code Playgroud)
但是这不保留列名
我正在尝试有效地重组大型多维数据集。假设随着时间的推移,我有许多遥感图像,其中有许多波段,坐标为 xy 像素位置,时间为图像采集时间,波段为收集的不同数据。
在我的用例中,假设 xarray 坐标长度大约为 x (3000)、y (3000)、时间 (10),带有浮点数据带 (40)。所以 100GB+ 的数据。
我一直在尝试从这个例子中工作,但我在将它翻译成这个案例时遇到了麻烦。
注意:实际数据比这个例子大得多。
import numpy as np
import dask.array as da
import xarray as xr
nrows = 100
ncols = 200
row_chunks = 50
col_chunks = 50
data = da.random.random(size=(1, nrows, ncols), chunks=(1, row_chunks, col_chunks))
def create_band(data, x, y, band_name):
return xr.DataArray(data,
dims=('band', 'y', 'x'),
coords={'band': [band_name],
'y': y,
'x': x})
def create_coords(data, left, top, celly, cellx):
nrows = data.shape[-2]
ncols = data.shape[-1]
right = …
Run Code Online (Sandbox Code Playgroud) 我希望创建总和为1的3个(非负)准随机数,并反复重复.
基本上我试图在许多试验中将某些东西分成三个随机部分.
虽然我知道
a = runif(3,0,1)
我想我可以在下一次运行时使用1-a作为最大值,但是看起来很麻烦.
但这些当然不能归结为一个.有任何想法,哦明智的stackoverflow-ers?
我正在尝试制作一个堆叠的geom_area图,但是想要用线描绘每个区域图(在第一个"红色"区域而不是蓝色区域上工作).这是我最好的尝试,但我无法弄清楚如何使线型也堆叠.想法?
df= data.frame(Time=as.numeric(strsplit('1939 1949 1959 1969 1979 1989 1999 2009 2019 2029 2039 2049 1939 1949 1959 1969 1979 1989 1999 2009 2019 2029 2039 2049', split=' ')[[1]] ),
Acres=as.numeric(strsplit('139504.2 233529.0 392105.3 502983.9 685159.9 835594.7 882945.1 1212671.4 1475211.9 1717971.7 1862505.7 1934308.0 308261.4 502460.8 834303.1 1115150.7 1430797.8 1712085.8 1973366.1 1694907.7 1480506.0 1280047.6 1164200.5 1118045.3', split=' ')[[1]] ),
WUClass= strsplit('DenseUrban DenseUrban DenseUrban DenseUrban DenseUrban DenseUrban DenseUrban DenseUrban DenseUrban DenseUrban DenseUrban DenseUrban Urban Urban Urban Urban Urban Urban Urban Urban Urban Urban Urban …
Run Code Online (Sandbox Code Playgroud) 好的,所以我正在编写一个更大的函数来调用几个函数。
问题是我需要在某些逻辑条件下突破“function_inner”而不突破“function_outer”。Break 似乎适用于循环,然后停止,它停止所有功能......有什么想法吗?
function_outer <- function(){
beta =1
function_inner <- function(beta){
if (beta==1){?break?stop}
print("Its not working")
}
return(beta)
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试做一些相当简单的事情。我想从 .py 文件导入一个函数,但使用在我的主脚本中导入的模块。例如存储以下函数
./文件/罪人.py
def sinner(x):
y = mt.sin(x)
return y
Run Code Online (Sandbox Code Playgroud)
然后我想使用数学作为 mt 运行罪人
from sinner import sinner
import math as mt
sinner(10)
Run Code Online (Sandbox Code Playgroud)
这并不奇怪会引发错误
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-3-50006877ce3e> in <module>()
2 import math as mt
3
----> 4 sinner(10)
/media/ssd/Lodging_Classifier/sinner.py in sinner(x)
1 import math as mt
----> 2
3 def sinner(x):
4 y = mt.sin(x)
5 return y
NameError: global name 'mt' is not defined
Run Code Online (Sandbox Code Playgroud)
我知道如何在 R 中处理这个问题,但在 Python 中不知道。我错过了什么?
我正在尝试使用“by”调用的输出,它很容易转换为列表......但有时列表仍然无视我
a = list('1'=c(19,3,4,5), '4'=c(3,5,3,2,1,6), '8'=c(1,3))
for (i in c(1,8,4)){
# would like to do something like this
a[["i"]] # calling list elements by name rather than #
}
#ideally the output would be something like this
>19,3,4,5
>1,3
>3,5,3,2,1,6
Run Code Online (Sandbox Code Playgroud) 我想使用字符串执行逻辑操作(是的,我想这样做)
a = data.frame(x=c(1,2,3,4),y=c(11,12,13,14))
logical_text = "a$x!=2 & a$y!=14"
a
> a
x y
1 1 11
2 2 12
3 3 13
4 4 14
Run Code Online (Sandbox Code Playgroud)
我希望使用如下字符串
a[logical_text,]
> a[logical_text,]
x y
NA NA NA
Run Code Online (Sandbox Code Playgroud)
为了得到相同的结果:
a[a$x!=2 & a$y!=14,]
> a[a$x!=2 & a$y!=14,]
x y
1 1 11
3 3 13
Run Code Online (Sandbox Code Playgroud) 我在'inner'中有如下arcpy(arcmap)列出的文件名.
inner = [u'aet1941jan.asc', u'aet2004jun.asc', u'aet1981nov.asc', u'aet1985feb.asc', u'aet1974sep.asc', u'aet1900sep.asc', u'aet1994apr.asc', u'aet1970nov.asc']
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种方法来只提取1990年以后的栅格.如何构建一个逻辑表达式,从列表中删除所有旧栅格的所有元素?
这样输出就是一个列表:
out = [u'aet2004jun.asc', u'aet1994apr.asc']
Run Code Online (Sandbox Code Playgroud)