파이썬은 당신이 즉 경우, zero-indexed
인 list
같은 :
l = [1, 4, 7, 3, 6]
그리고 당신은 while loop
(A for-loop
이 마음을 더 잘 할 수 없지만 않을 것), 당신은해야 할 것입니다 그것을 통해 iterate
원하는 index
loop
while
는 list
의 length
보다 작 - 그래서 index
실제로 O를 length
결코 f list
, 전에 최대 1
까지.
과 같이 보일 것이다 위의 list
이상 iterating
의 코드 : 당신의 output
을 줄 것이다
i = 0
while i < len(l):
print(l[i])
i += 1
을 :
1
4
7
3
6
동일한 논리가 적용되는 당신의 image
- 결국 결국은 2-dimensional
list
입니다.
코드의 less than or equal
(<=
) 비교기를 less thans
(<
)으로 수정해야한다는 의미입니다. 그런 다음 코드가 원하는 방식으로 작동해야합니다. 내가 처음에 언급 한 바와 같이 더 파이썬을 덜 라인을 필요로하고있다되므로
from PIL import Image
pix = newimage2.load()
print(newimage2.size)
print(" ")
whitevalues = 0
x = 0
while x < newimage2.width:
y = 0
while y < newimage2.height:
print(pix[x,y])
if pix[x,y] == (255,255,255):
whitevalues += 1
y += 1
x += 1
print(whitevalues)
그러나,하는 for-loop
이 응용 프로그램에 대한 더 나은 것 :
그래서이 코드가 수정 될 것입니다. 그래서 여기가 유용하게 사용할 수있는
for-loop
에 대한 코드는 다음과 같습니다
from PIL import Image
pix = newimage2.load()
print(newimage2.size)
print(" ")
whitevalues = 0
for row in newimage2
for col in row:
print(col)
if col == (255,255,255):
whitevalues += 1
print(whitevalues)
당신이 매우 파이썬으로 원하는 경우 또는, 당신은 list-comprehension
에서이 작업을 수행 할 수 있습니다 :
whitevalues = sum([1 for r in pix for c in r if c == 1])