2014-11-09 2 views
0

파이썬 numpy 모듈을 사용 해보지 않았으므로 제목에 맞는 용어를 사용했는지 확신 할 수 없지만 특정 코드에서 두 줄이 생겨 혼란 스럽습니다. 이 두 :numpy 배열 내포를 일반 파이썬 구문으로 변환

IAM[theta == 0]=1 
IAM[abs(theta) > 90 | (IAM < 0)]=0 

소스 : https://github.com/Sandia-Labs/PVLIB_Python/blob/master/pvlib/pvl_physicaliam.py#L109-111

그들은 일반 파이썬 코드로 번역 될 수 있는지가 궁금? 는 상단이 실제로 뜻 :

theta = 10 # for example 

newIAM = [] 
for item in IAM: 
    if item == 0: 
     newIAM.append(1) 
    else: 
     newIAM.append(item) 

과 :

newIAM = [] 
for item in IAM: 
    if (abs(theta) > 90) and (item < 0) 
     newIAM.append(0) 
    else: 
     newIAM.append(item) 

?

저는 python 2.7을 사용하고 있습니다. 도움 주셔서 감사합니다.

답변

3

IAM은 벡터이고, theta는 벡터 또는 스칼라 일 수 있습니다.

IAM[theta == 0]=1 

은 해당하는 세타가 0 인 IAM의 모든 값을 1로 설정합니다. 해당 절대 세타 값보다 큰 90 OR IAM가 0보다 작 으면

IAM[abs(theta) > 90 | (IAM < 0)]=0 

(이것은 함) 0 IAM의 각 값을 설정한다.

import numpy as np 
IAM = np.array([3, 2, 3, 4, 5]) 
# theta can be shorter than IAM 
theta = np.array([0, 1, 0, 1]) 
IAM[theta==0] = 1 
# when theta is a scalar only the fist value will be tested and perhaps changed 
# theta[0] is 0 => set IAM[0] to 0 
# theta[1] is not 0 => do not change IAM[1] 
# ... 
#IAM = [1 2 1 4 5] 

등가 순수 파이썬 솔루션 : 구성과 같은

from itertools import izip_longest 
IAM = [3, 2, 3, 4, 5] 
theta = [0, 1, 0, 1] 
newIAM = [] 

try: 
    for iam, t in izip_longest(IAM, theta): 
     if t == 0: 
      newIAM.append(1) 
     else: 
      newIAM.append(iam) 
except TypeError: 
    newIAM.extend(IAM) 
    if theta == 0: 
     neaIAM[0]=1 

두 번째 줄은 작동하지 않습니다. ABS (세타)보다 큰 경우, ABS (세타)의 arround 괄호없이

import numpy as np 
IAM = np.array([-1, 2, 3, -5, 1]) 
theta = np.array([1, 2, -91, 3, 4]) 
IAM[(abs(theta) > 90) | (IAM < 0)]=0 
# IAM is [0, 2, 0, 0, 1] 

는> 90가 수표 (90 | (IAM < 0)). 90 | (IAM < 0) 90로 평가되는 경우 IAM> = 0에 IAM < 0

을 게시 코드의 버그처럼 보인다 (91) 경우
관련 문제