查找数组中的元素列表

时间:2014-12-18 23:04:25

标签: python arrays python-2.7

我正在处理我的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

您能否告诉我如何使用变量375在数组中找到position_x的元素,以便我可以使用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'>

1 个答案:

答案 0 :(得分: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的输出,你可以试试:

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)