2016-04-30 38 views
0

我有一个数组,看起来像这样:Python 2.7版:在2D Python的阵列评估对

myarray = array([[Me, False], [Me, True], [Partner, False], [Me, True], [Me, False],[Me, True],[Partner, True]]) 

我想要做的就是每次启动计数器[我,真]显示,使该计数器增加,直到数组中出现[Partner,True]或[Partner,False]。每当此计数器遇到[Partner,True]或[Partner,False]时,都应该重置。

所以,如果permcount = 0和tempcount = 0,

在上述例子中,因为[Me中,真]正好出现之前[合作伙伴,FALSE],tempcount将递增到1,然后得到加时permcount [合作伙伴,FALSE]遇到和复位为0。

现在permcount = 1和tempcount = 0

当[Me中,真]旁边出现,tempcount会遇到[合作伙伴,真]和复位之前达到3到0和permcount = 4.

我的代码如下所示:

tempcount = 0 
permcount = [] 
flag = True 
initiated = False 

for x,y in myarray: 
    if x == "Me" and y == True: 
     if flag == True: 
      tempcount = tempcount + 1 
      initiated == True 
     else: 
      tempcount = 0 
      tempcount = tempcount + 1 
      flag == True 
    if x == "Me" and y == False and flag == True and initiated == True: 
     tempcount = tempcount + 1 
    if x == "Partner" and y == True and flag == True and initiated == True: 
     permcount.append(tempcount) 
     flag == False 
    if x == "Partner" and y == False and flag == True and initiated == True:  
     permcount.append(tempcount) 
     flag == False 

出于某种原因,这似乎并没有评估和追加tempcount在所有的permcount仍处于结束时清空。我做错了什么建议?

编辑:的确,这不是一个Numpy数组...我通过一个pandas dataframe.values命令实现了这个列表清单。还为稍后将看到此内容的任何用户更正了一些拼写错误。感谢你们!

-Mano

+1

我在这里看不到任何'numpy'数组的使用;这很好,因为'np.append'不像列表追加那样工作。此外,对于像“我”和“真”这样的值,列表或列表的列表优于阵列。至于问题 - 您需要打印一些中间结果。 – hpaulj

+0

您能提供一些预期输出吗? – Eric

+2

'initiate == True '应该是'启动=真',并且同样为'Flag == True' – Eric

回答

1

首先我数组改为

alist = [["Me", False], ["Me", True],... 

如果没有进一步的变化,我得到3 tempcount[]permcount

改变这些错位的===,这意味着将那些改变flagintiated值(而不是对它们进行测试),我得到

3 
[1, 3] 

== True般的表情可以简化(虽然它不会改变结果)

for x,y in alist: 
    if x == "Me" and y: 
     if flag: 
      tempcount += 1 
      initiated = True 
     else: 
      tempcount = 1 
      flag = True 
    if x == "Me" and not y and flag and initiated: 
     tempcount += 1 
    if x == "Partner" and y and flag and initiated: 
     permcount.append(tempcount) 
     flag = False 
    if x == "Partner" and not y and flag and initiated:  
     permcount.append(tempcount) 
     flag = False 
+0

啊!该死的我没有意识到==和=之间的区别,非常感谢你,像一个魅力一样!Mano – manofone