Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python删除五月输出中的inf值_Python - Fatal编程技术网

如何使用python删除五月输出中的inf值

如何使用python删除五月输出中的inf值,python,Python,我有以下代码: Import matplotlib.pyplot as plt import pandas as pd import math import numpy as np import numpy as geek import itertools from itertools import permutations from itertools import combinations import collections from math import sin from math

我有以下代码:

Import matplotlib.pyplot as plt
import pandas as pd
import math
import numpy as np
import numpy as geek
import itertools 
from itertools import permutations
from itertools import combinations
import collections
from math import sin
from math import asin
from math import pi



a= 1
b= 1
c= 1
alpha=90
beta=90
gamma=90
w=0.5 #width of your peak
basisatom =1;
p=1;
lamda=0.71073

x = [i for i in range(-1, 1, 1)]
y = [i for i in range(-1, 1, 1)]
z = [i for i in range(-1, 1, 1)]

all_list = [x,y,z]

res = np.array(list(itertools.product(*all_list)))

res1 = res.transpose()

a1= res1[0]
b1 = res1[1]
c1  = res1[2]
test_list = [a1,b1,c1]

for h, k, l in zip(*test_list):
    a1 =[h, k, l]
    aa1 = abs(np.array((a1)))
    ax =aa1[0]
    ay =aa1[1]
    az =aa1[2]
    
    V = (a*b*c)*geek.sqrt(1 - (math.cos(alpha))**2 - (math.cos(beta))**2 - (math.cos(gamma))**2 +2*((math.cos(alpha))*(math.cos(beta))*(math.cos(gamma))))
    Spp =( b**2)*(c**2)*((math.sin(alpha))**2)
    Sww = (a**2)*(c**2)*((math.sin(beta))**2)
    Snn = (a**2)*(b**2)*((math.sin(gamma))**2);
    Spw = (c**2)*b*a*((math.cos(alpha))*math.cos(beta)-math.cos(gamma))
    Swn = ((a**2)*b*c*(math.cos(beta))*(math.cos(gamma))-math.cos(alpha))
    Spn = (b**2)*a*c*((math.cos(gamma))*math.cos(alpha) - math.cos(beta))
    d = (geek.sqrt(((V**2)/(((Spp*(ax**2)) + Sww*(ay**2) + Snn*(az**2) + 2*(Spw*(ax*ay) + 2*(Swn*(ay*az)) + 2*Spn*(ax*az)))))))
    za = lamda/(2*d)
    print(d)  
    
d = d.replace([np.inf, -np.inf], np.nan)
输出为:

0.156546553048738001 0.2742138334664101 0.22787361381000193 0.5219919198195865 0.22787361381000193 0.5219919198195865 0.5219919198195865 inf

我想删除数组中的inf值。我尝试了以下代码:

Import matplotlib.pyplot as plt
import pandas as pd
import math
import numpy as np
import numpy as geek
import itertools 
from itertools import permutations
from itertools import combinations
import collections
from math import sin
from math import asin
from math import pi



a= 1
b= 1
c= 1
alpha=90
beta=90
gamma=90
w=0.5 #width of your peak
basisatom =1;
p=1;
lamda=0.71073

x = [i for i in range(-1, 1, 1)]
y = [i for i in range(-1, 1, 1)]
z = [i for i in range(-1, 1, 1)]

all_list = [x,y,z]

res = np.array(list(itertools.product(*all_list)))

res1 = res.transpose()

a1= res1[0]
b1 = res1[1]
c1  = res1[2]
test_list = [a1,b1,c1]

for h, k, l in zip(*test_list):
    a1 =[h, k, l]
    aa1 = abs(np.array((a1)))
    ax =aa1[0]
    ay =aa1[1]
    az =aa1[2]
    
    V = (a*b*c)*geek.sqrt(1 - (math.cos(alpha))**2 - (math.cos(beta))**2 - (math.cos(gamma))**2 +2*((math.cos(alpha))*(math.cos(beta))*(math.cos(gamma))))
    Spp =( b**2)*(c**2)*((math.sin(alpha))**2)
    Sww = (a**2)*(c**2)*((math.sin(beta))**2)
    Snn = (a**2)*(b**2)*((math.sin(gamma))**2);
    Spw = (c**2)*b*a*((math.cos(alpha))*math.cos(beta)-math.cos(gamma))
    Swn = ((a**2)*b*c*(math.cos(beta))*(math.cos(gamma))-math.cos(alpha))
    Spn = (b**2)*a*c*((math.cos(gamma))*math.cos(alpha) - math.cos(beta))
    d = (geek.sqrt(((V**2)/(((Spp*(ax**2)) + Sww*(ay**2) + Snn*(az**2) + 2*(Spw*(ax*ay) + 2*(Swn*(ay*az)) + 2*Spn*(ax*az)))))))
    za = lamda/(2*d)
    print(d)  
    
d = d.replace([np.inf, -np.inf], np.nan)
但是,出现了一个错误

AttributeError:'numpy.float64'对象没有属性'replace'


是否有其他方法删除inf值?谢谢

您的变量
d
是一个float64,因此不能使用方法
replace()
。您可以在循环的
末尾使用if条件:

    if d!=np.inf:
        za = lamda/(2*d)
        print(d)

要从数组中删除
np.inf
,您可以使用如下所示的简单列表理解:

import numpy as np
a = [0.15654653048738001, np.inf, 0.2742138334664101, 0.22787361381000193, 0.5219919198195865, 0.22787361381000193, 0.5219919198195865, 0.5219919198195865, np.inf]
filtered = [item for item in a if item!=np.inf]
print(filtered)
这将过滤np.inf的所有外观,输出为:

[0.15654653048738001, 0.2742138334664101, 0.22787361381000193, 0.5219919198195865, 0.22787361381000193, 0.5219919198195865, 0.5219919198195865]

您可以添加您添加的代码以删除inf值和错误内容吗?请检查更新的问题。
import
的第一行写在
import
(大写)