我想用Python读取文本文件.我的清单必须是这样的:
mylist = [(-34.968398, -6.487265), (-34.969448, -6.488250),
(-34.967364, -6.492370), (-34.965735, -6.582322)]
Run Code Online (Sandbox Code Playgroud)
我的文本文件是:
-34.968398,-6.487265
-34.969448,-6.488250
-34.967364,-6.492370
-34.965735,-6.582322
Run Code Online (Sandbox Code Playgroud)
我的Python代码:
f = open('t3.txt', 'r')
l = f.readlines()
print l
Run Code Online (Sandbox Code Playgroud)
我的结果:
['-34.968398 -6.487265\n', '-34.969448 -6.488250\n',
'-34.967364 -6.492370\n', '-34.965735 -6.582322\n']
Run Code Online (Sandbox Code Playgroud) 我正在尝试从下面的 Fortran 代码读取二进制文件输出,但结果与输出文件不同。
Fortran 77 代码:
program test
implicit none
integer i,j,k,l
real*4 pcp(2,3,4)
open(10, file='pcp.bin', form='unformatted')
l = 0
do i=1,2
do j=1,2
do k=1,2
print*,k+l*2
pcp(i,j,k)=k+l*2
l = l + 1
enddo
enddo
enddo
do k=1,4
write(10)pcp(:,:,k)
enddo
close(10)
stop
end
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用下面的 Python 代码:
from scipy.io import FortranFile
f = FortranFile('pcp.bin', 'r')
a = f.read_reals(dtype=float)
print(a)
Run Code Online (Sandbox Code Playgroud) 我试着运行这个脚本:
hostname = '192.168.3.4'
port = 22
username = 'username'
password = 'mypassword'
y = "2012"
m = "02"
d = "27"
if __name__ == "__main__":
s = paramiko.SSHClient()
s.load_system_host_keys()
s.connect(hostname, port, username, password)
command = 'ls /home/user/images/cappi/03000/y/m/d'
s.close
Run Code Online (Sandbox Code Playgroud)
现在的问题是:我怎样才能把这些变量y,m,d到变量command?
有一个numpy方法在区间中每三个元素做一个总和?例如:
import numpy as np
mydata = np.array([4, 2, 3, 8, -6, 10])
Run Code Online (Sandbox Code Playgroud)
我想得到这个结果:
np.array([9, 13, 5, 12])
Run Code Online (Sandbox Code Playgroud) 我想将2维掩模(NxM数组)应用于3维数组(KxNxM数组).我怎样才能做到这一点?
2d = lat x lon
3d =时间x lat x lon
import numpy as np
a = np.array(
[[[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8]],
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],
[[18, 19, 20],
[21, 22, 23],
[24, 25, 26]]])
b = np.array(
[[ 0, 1, 0],
[ 1, 0, 1],
[ 0, 1, 1]])
c = np.ma.array(a, mask=b) # this behavior is wanted
Run Code Online (Sandbox Code Playgroud) 我想以Pythonic的方式编写下面的代码,在两个轴上应用均值.最好的方法是什么?
import numpy as np
m = np.random.rand(30, 10, 10)
m_mean = np.zeros((30, 1))
for j in range(30):
m_mean[j, 0] = m[j, :, :].mean()
Run Code Online (Sandbox Code Playgroud) 我想从 numpy 数组的特定轴中的当前元素中减去下一个元素。但是,我知道如何使用大量循环来做到这一点。我的问题是:如何以最有效的方式做到这一点?也许使用numpy?
我的 Python 代码如下:
import numpy as np
np.random.seed(0)
myarr = np.random.rand(20, 7, 11, 151, 161)
newarr = np.full((20, 6, 11, 151, 161), np.nan)
for iyr in range(20):
for iwk in range(6):
for imb in range(11):
for ilat in range(151):
for ilon in range(161):
newarr[iyr, iwk, imb, ilat, ilon] = myarr[iyr, iwk + 1, imb, ilat, ilon] - myarr[iyr, iwk, imb, ilat, ilon]
Run Code Online (Sandbox Code Playgroud)