2014-11-09 2 views
0

numpy 파이썬 모듈 구문에 대한 질문이 하나 더 있습니다. 정규 파이썬 2.7 구문으로 변환하고 싶습니다.numpy 흐름 제어 구문을 일반 파이썬 구문으로 변환

if any((var.theta < 0) | (var.theta >= 90)): 
    print('Input incident angles <0 or >=90 detected For input angles with absolute value greater than 90, the ' + 'modifier is set to 0. For input angles between -90 and 0, the ' + 'angle is changed to its absolute value and evaluated.') 
    var.theta[(var.theta < 0) | (var.theta >= 90)]=abs((var.theta < 0) | (var.theta >= 90)) 

소스 코드 :

newTheta = [] 
for item in var.theta: 
    if (item < 0) and (item >= 90): 
     print('Input incident angles <0 or >=90 detected For input angles with absolute value greater than 90, the ' + 'modifier is set to 0. For input angles between -90 and 0, the ' + 'angle is changed to its absolute value and evaluated.') 
     newItem = abs(item) 
     newTheta.append(newItem) 

: 우리는 NumPy와 배열이 차원이다 척하면 https://github.com/Sandia-Labs/PVLIB_Python/blob/master/pvlib/pvl_physicaliam.py#L93-95

는, 일반 파이썬 2.7 구문은 다음과 같이 보일 것입니다 여기에 NumPy와 버전은 무엇입니까? ? 답장을 보내 주셔서 감사합니다. 즉

답변

2

if any((var.theta < 0) | (var.theta >= 90)) 표현은 무엇을 의미하는지 :

무엇 당신의 흐름 "var.theta에있는 항목 중 하나가, 제로보다 또는가 90보다 크거나 같은 할 ... 경우" 제어 명령문의 내용은 "var.theta의 항목이 0 보다 크거나 같으면 ..."입니다.

평범한 파이썬 예제에서 실제로 또는을 의미한다면, 그렇습니다. 다른 방법으로하지만, 당신은 지능형리스트를 사용할 수 있습니다

newTheta = [abs(item) for item in var.theta if (item < 0 or item >= 90)]

목록 이해는 간단한 작업을 위해 쉽게 읽을 수있는, 컴팩트하고, 인터프리터 최적화하기에 용이하다.

+0

MobiusKlein 답장을 보내 주셔서 감사합니다. 그래서 numpy의'|'는'or'를 대신합니다. '와'는 어때? 'and'에 대한 대체 구문은 무엇입니까? – marco

+1

Numpy는 바이너리와 연산자 '&'를 오버로드하여 벡터화 된 논리 AND를 처리합니다. 코드는'cond_1 & cond_2'와 같을 것이며, 평범한 파이썬의 경우에는 and 키워드 만 있으면됩니다. – mobiusklein

+0

mobiusklein 감사합니다. – marco

관련 문제