假设我随机生成一个列表。
mylist = []
for i in range(0,10):
s = np.random.choice([-1,0,1])
mylist.append(s)
现在for x in range(0,100)
。我根据一些规则修改了元素,然后使用counts = Counter(mylist)
for x in range(0,100):
#some rules
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的值> 1.5(+1的数量),并且当+1时为高-1状态。 -1的值> 1.5 *(+ 1的个数)。 (或其他任意任意因式,而不是1或1.5,为2、3等)
答案 0 :(得分:0)
import random
mylist = []
new_list = list(mylist)
transitions = []
for x in range(0,100):
mylist.append(random.choice([1, 0, -1])) # some rules, which change mylist
is_trans1 = new_list.count(1) > new_list.count(-1) and mylist.count(1) < mylist.count(-1)
is_trans2 = new_list.count(1) < new_list.count(-1) and mylist.count(1) > mylist.count(-1)
if is_trans1 or is_trans2:
transitions.append(x)
new_list = list(mylist) # save mylist for the next iteration
答案 1 :(得分:0)
如果您只对列表中最常见的元素感兴趣,那么Counter
具有内置的most_common()
函数,您可以查询:
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)]
答案 2 :(得分:0)
此答案假设您仅尝试计算一个转换,而不尝试绘制值(问题的最后一段)。
如果仅处理{-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)
在这里我假设,如果sum不与零相交,就不会有过渡。
类似的方法适用于前提因素案例(如果我的想法正确的话):
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