2014-12-18 188 views
-2

我正在處理我的python腳本以獲取數組中的元素列表。我使用變量position_x = elem.getX()來獲取像素大小的元素列表。查找陣列中的元素列表

所以,當我試試這個:

for elem in self.program_buttons: 
    position_x = elem.getX() #Get positionX 
    #print the list of elements with 375 
    print position_x 

我會得到的結果是這樣的:

18:32:12 T:4060 NOTICE: 375 
18:32:12 T:4060 NOTICE: 724 
18:32:12 T:4060 NOTICE: 1073 
18:32:12 T:4060 NOTICE: 1261 
18:32:12 T:4060 NOTICE: 1449 
18:32:12 T:4060 NOTICE: 1798 
18:32:12 T:4060 NOTICE: 2147 
18:32:12 T:4060 NOTICE: 2496 
18:32:12 T:4060 NOTICE: 2845 
18:32:12 T:4060 NOTICE: 3194 
18:32:12 T:4060 NOTICE: 3543 
18:32:12 T:4060 NOTICE: 4241 
18:32:12 T:4060 NOTICE: 4590 
18:32:12 T:4060 NOTICE: 4939 
18:32:12 T:4060 NOTICE: 5289 
18:32:12 T:4060 NOTICE: 5638 
18:32:12 T:4060 NOTICE: 5987 
18:32:12 T:4060 NOTICE: 6336 
18:32:12 T:4060 NOTICE: 6685 
18:32:12 T:4060 NOTICE: 7034 
18:32:12 T:4060 NOTICE: 7383 
18:32:12 T:4060 NOTICE: 7732 
18:32:12 T:4060 NOTICE: 9456 
18:32:12 T:4060 NOTICE: 10154 
18:32:12 T:4060 NOTICE: 10503 
18:32:12 T:4060 NOTICE: 10852 
18:32:12 T:4060 NOTICE: 11201 
18:32:12 T:4060 NOTICE: 11550 
18:32:12 T:4060 NOTICE: 11900 
18:32:12 T:4060 NOTICE: 12249 
18:32:12 T:4060 NOTICE: 12598 
18:32:12 T:4060 NOTICE: 12947 
18:32:12 T:4060 NOTICE: 13296 
18:32:12 T:4060 NOTICE: 13645 
18:32:12 T:4060 NOTICE: 13994 
18:32:12 T:4060 NOTICE: 14343 
18:32:12 T:4060 NOTICE: 14692 
18:32:12 T:4060 NOTICE: 15041 
18:32:12 T:4060 NOTICE: 15390 
18:32:12 T:4060 NOTICE: 15739 
18:32:12 T:4060 NOTICE: 16088 
18:32:12 T:4060 NOTICE: 16437 
18:32:12 T:4060 NOTICE: 18161 
18:32:12 T:4060 NOTICE: 18860 
18:32:12 T:4060 NOTICE: 19209 
18:32:12 T:4060 NOTICE: 19558 
18:32:12 T:4060 NOTICE: 19907 
18:32:12 T:4060 NOTICE: 20256 
18:32:12 T:4060 NOTICE: 20605 
18:32:12 T:4060 NOTICE: 20954 
18:32:12 T:4060 NOTICE: 21652 
18:32:12 T:4060 NOTICE: 22001 
18:32:12 T:4060 NOTICE: 22350 
18:32:12 T:4060 NOTICE: 22699 
18:32:12 T:4060 NOTICE: 23048 
18:32:12 T:4060 NOTICE: 23397 
18:32:12 T:4060 NOTICE: 23746 
18:32:12 T:4060 NOTICE: 24095 
18:32:12 T:4060 NOTICE: 24444 
18:32:12 T:4060 NOTICE: 24794 
18:32:12 T:4060 NOTICE: 25143 
18:32:12 T:4060 NOTICE: 26867 
18:32:12 T:4060 NOTICE: 27565 
18:32:12 T:4060 NOTICE: 27914 
18:32:12 T:4060 NOTICE: 28263 
18:32:12 T:4060 NOTICE: 28612 
18:32:12 T:4060 NOTICE: 28961 
18:32:12 T:4060 NOTICE: 29310 
18:32:12 T:4060 NOTICE: 29659 
18:32:12 T:4060 NOTICE: 375 
18:32:12 T:4060 NOTICE: 1073 
18:32:12 T:4060 NOTICE: 1261 
18:32:12 T:4060 NOTICE: 1449 
18:32:12 T:4060 NOTICE: 2147 
18:32:12 T:4060 NOTICE: 3871 
18:32:12 T:4060 NOTICE: 4220 
18:32:12 T:4060 NOTICE: 4918 
18:32:12 T:4060 NOTICE: 5616 
18:32:12 T:4060 NOTICE: 5965 
18:32:12 T:4060 NOTICE: 8374 
18:32:12 T:4060 NOTICE: 8723 
18:32:12 T:4060 NOTICE: 9072 
18:32:12 T:4060 NOTICE: 9421 
18:32:12 T:4060 NOTICE: 9770 
18:32:12 T:4060 NOTICE: 10468 
18:32:12 T:4060 NOTICE: 12192 
18:32:12 T:4060 NOTICE: 13916 
18:32:12 T:4060 NOTICE: 14614 
18:32:12 T:4060 NOTICE: 14963 
18:32:12 T:4060 NOTICE: 15661 
18:32:12 T:4060 NOTICE: 16011 
18:32:12 T:4060 NOTICE: 16709 
18:32:12 T:4060 NOTICE: 17407 
18:32:12 T:4060 NOTICE: 19473 
18:32:12 T:4060 NOTICE: 19822 
18:32:12 T:4060 NOTICE: 21546 
18:32:12 T:4060 NOTICE: 22244 
18:32:12 T:4060 NOTICE: 22593 
18:32:12 T:4060 NOTICE: 23291 
18:32:12 T:4060 NOTICE: 23989 
18:32:12 T:4060 NOTICE: 24338 
18:32:12 T:4060 NOTICE: 26747 
18:32:12 T:4060 NOTICE: 27096 
18:32:12 T:4060 NOTICE: 27445 
18:32:12 T:4060 NOTICE: 27794 
18:32:12 T:4060 NOTICE: 28143 
18:32:12 T:4060 NOTICE: 28841 
18:32:12 T:4060 NOTICE: 30565 
18:32:12 T:4060 NOTICE: 32289 
18:32:12 T:4060 NOTICE: 32987 
18:32:12 T:4060 NOTICE: 33336 
18:32:12 T:4060 NOTICE: 34035 
18:32:12 T:4060 NOTICE: 34384 
18:32:12 T:4060 NOTICE: 35082 
18:32:12 T:4060 NOTICE: 35780 
18:32:12 T:4060 NOTICE: 37504 
18:32:12 T:4060 NOTICE: 37853 
18:32:12 T:4060 NOTICE: 38202 
18:32:12 T:4060 NOTICE: 38551 
18:32:12 T:4060 NOTICE: 38900 
18:32:12 T:4060 NOTICE: 39249 
18:32:12 T:4060 NOTICE: 39598 
18:32:12 T:4060 NOTICE: 39947 
18:32:12 T:4060 NOTICE: 40296 
18:32:12 T:4060 NOTICE: 40995 
18:32:12 T:4060 NOTICE: 41693 
18:32:12 T:4060 NOTICE: 42042 
18:32:12 T:4060 NOTICE: 44450 
18:32:12 T:4060 NOTICE: 44799 
18:32:12 T:4060 NOTICE: 45148 
18:32:12 T:4060 NOTICE: 45497 
18:32:12 T:4060 NOTICE: 45846 
18:32:12 T:4060 NOTICE: 46544 
18:32:12 T:4060 NOTICE: 48268 
18:32:12 T:4060 NOTICE: 49992 
18:32:12 T:4060 NOTICE: 50691 
18:32:12 T:4060 NOTICE: 51040 
18:32:12 T:4060 NOTICE: 51738 
18:32:12 T:4060 NOTICE: 375 
...etc 

這裏是我想要實現:

375 
375 
375 
375 
375 
375 
375 

能請告訴我如何使用變量position_x在陣列中找到375的元素我可以用375列印元素列表?

編輯:

這裏是print type(position_x)結果:

00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
00:22:49 T:6656 NOTICE: <type 'int'> 
+0

評論不適合廣泛的討論;這個對話已經[轉移到聊天](http://chat.stackoverflow.com/rooms/67282/discussion-on-question-by-rob-find-a-list-of-elements-in-the-arrays) 。 – Taryn 2014-12-19 01:26:51

回答

1

這似乎是你在說什麼。

elems = [elem for elem in self.program_buttons if elem.getX() == 375] 
for elem in elems: 
    # the same code you have before -- the above line filters it 

大概其可讀性:

for elem in self.program_buttons: 
    position_x = elem.getX() 
    if position_x == 375: 
     # use the same code you have to print it -- this is your filter 
    else: 
     continue 
    # these last two lines do literally nothing and can be excluded 
    # I just find them more readable!! 

基本上你根據該通知數量是否375篩選結果。我的第一個代碼給你一個列表elems,其中包含通知其通過過濾器的每個元素。代碼的第二位不會產生這樣的列表,只需打印通過的代碼行即可。

如果你從字面上只希望375 S的輸出,你可以嘗試:

elems = [elem for elem in self.program_buttons if elem.getX() == 375] 
for elem in elems: 
    print(375) 

for elem in self.program_buttons: 
    position_x = elem.getX() 
    if position_x == 375: 
     print(375) 
+0

非常感謝您的幫助。我有一個錯誤:'AttributeError:'int'object has no attribute'endswith'',如果position_x.endswith(「375」):',錯誤在這行上跳躍。你知道我爲什麼得到一個錯誤嗎? – Rob 2014-12-18 23:24:21

+0

@Rob看起來像'position_x'不是一個字符串,它是一個int。你似乎非常確信這是一個字符串,所以我不知道如何幫助你。 – 2014-12-18 23:27:25

+0

哦,對不起,這是我的錯誤。我認爲這是一個字符串。我沒有意識到這是一個整數。你知道如何用'375'獲得整數嗎? – Rob 2014-12-18 23:32:07