Python 计算变量更改值的次数

Python 计算变量更改值的次数,python,numpy,Python,Numpy,假设我随机生成一个列表 mylist = [] for i in range(0,10): s = np.random.choice([-1,0,1]) mylist.append(s) 现在用于范围(0100)内的x。我根据一些规则修改元素,然后使用counts=Counter(mylist) 在开始时,列表可能处于-1的数量大于+1的数量的状态,然后从+1的数量大于-1的数量的状态过渡 有没有办法计算这样的转换次数?除此之外,在列表中进行此类转换时,是否可以

假设我随机生成一个列表

mylist = []
for i in range(0,10):
        s = np.random.choice([-1,0,1])
        mylist.append(s)
现在
用于范围(0100)内的x
。我根据一些规则修改元素,然后使用
counts=Counter(mylist)

在开始时,列表可能处于-1的数量大于+1的数量的状态,然后从+1的数量大于-1的数量的状态过渡

有没有办法计算这样的转换次数?除此之外,在列表中进行此类转换时,是否可以记录x的值

到目前为止,我已经试着将这些数据与

p = counts[1]
m = counts[-1]  
plt.plot(x, p,m)
plt.show()
然后寻找交叉点。是否有更好的方法来实现这一点,或者python和/或numpy中是否有一些内置功能。此外,如果我能在+1数量大于1.5(数量大于+1)时得分过渡到高+1状态,以及在-1数量大于1.5*(数量大于+1)时得分过渡到高-1状态,那就太好了。(或其他任意预因子,2、3等,而不是1或1.5)

导入随机
mylist=[]
新建列表=列表(mylist)
转换=[]
对于范围(0100)内的x:
mylist.append(random.choice([1,0,-1]))#一些更改mylist的规则
is_trans1=new_list.count(1)>new_list.count(-1)和mylist.count(1)mylist.count(-1)
如果是_trans1或是_trans2:
transitions.append(x)
新建列表=列表(mylist)#为下一次迭代保存mylist

如果您只对列表中最常见的元素感兴趣,那么
计数器
有一个内置的
最常见()
函数,您可以查询:

counts = Counter()
for x in range(0, 100):
    # some rules
    s = np.random.choice([-1, 0, 1])
    mylist.append(s)
    if mylist.count(1) > mylist.count(-1) and mylist.count(1) > mylist.count(0):
        counts[1] += 1
    elif mylist.count(-1) > mylist.count(1) and mylist.count(-1) > mylist.count(0):
        counts[-1] += 1
    else:
        counts[0] += 1
    print(counts.most_common())
它会自动将最常见的元素(及其计数)提升为返回列表中的第一个元素。以下是过渡点附近的一些相邻回报:

[(-1, 28), (0, 27), (1, 26)]
[(-1, 28), (0, 27), (1, 27)]
[(1, 28), (-1, 28), (0, 27)]   # Transition here as -1 and +1 both have a count of 28
[(1, 29), (-1, 28), (0, 27)]

这个答案假设您只尝试计算一个转换,而不尝试绘制值(问题的最后一段)

如果只处理
{-1,0,1}
集,也许可以从列表中求和一个值。这比将它们添加到计数器并为您提供转换信息要快一点

当然,不必每次对完整列表求和,只要在发生更改时增加/减少逻辑块中的计数器就可以了

more_ones = True if sum(my_list) > 0 else False
transition_counter = 0
transitions_history = []
for x in range(0, 100): 
    # some rules, which change mylist
    # you can place a transition counter logic right here
    current_sum = sum(my_list)  # one more time, counting inside the logic is faster
    if current_sum < 0 and more_ones:
        transition_counter  += 1
        more_ones = False
        transitions_history.append(x)
    elif current_sum > 0 and not more_ones:  
        transition_counter  += 1
        more_ones = True
        transitions_history.append(x)
[(-1, 28), (0, 27), (1, 26)]
[(-1, 28), (0, 27), (1, 27)]
[(1, 28), (-1, 28), (0, 27)]   # Transition here as -1 and +1 both have a count of 28
[(1, 29), (-1, 28), (0, 27)]
more_ones = True if sum(my_list) > 0 else False
transition_counter = 0
transitions_history = []
for x in range(0, 100): 
    # some rules, which change mylist
    # you can place a transition counter logic right here
    current_sum = sum(my_list)  # one more time, counting inside the logic is faster
    if current_sum < 0 and more_ones:
        transition_counter  += 1
        more_ones = False
        transitions_history.append(x)
    elif current_sum > 0 and not more_ones:  
        transition_counter  += 1
        more_ones = True
        transitions_history.append(x)
my_list = [0, 1, -1, -1]  # sample data

factor = 1.5  # some factor
pos_count = my_list.count(1)
neg_count = my_list.count(-1)
more_ones = True if pos_count > factor * neg_count else False  # unclear part - don't know what you plan to do with zeroes
transition_counter = 0
transitions_history = []
for x in range(0, 100):
    # I provide a test logic
    ### CROP HERE ###
    try:
        if x < 3:
            my_list.remove(-1)  # better to decrement/increment pos. and neg. counts here
        elif x >= 3 and x < 5:
            my_list.append(1)
        elif x >= 5 and x < 10:
            my_list.append(-1)
        else:
            my_list.append(1)
    except ValueError:
        pass
    ### CROP HERE ###
    pos_count = my_list.count(1)
    neg_count = my_list.count(-1)
    if neg_count > pos_count * factor and more_ones:
        transition_counter  += 1
        more_ones = False
        # you couldn't store list and need a copy, otherwise it will change values by pointer
        transitions_history.append((x, 'pos_to_neg', [i for i in my_list]))
    elif pos_count > neg_count * factor and not more_ones:
        transition_counter += 1
        more_ones = True
        transitions_history.append((x, 'neg_to_pos', [i for i in my_list]))
transitions_history
Out:
    [(1, 'neg_to_pos', [0, 1]),
     (9, 'pos_to_neg', [0, 1, 1, 1, -1, -1, -1, -1, -1]),
     (14, 'neg_to_pos', [0, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1])]
transition_counter
Out:
3