观察计数器,计算总数并计算未完成的计数

2024-09-30 16:23:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一段代码,用于观察计数器的输出,如:

a1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,
      1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,
      1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]

我希望代码能够统计总数,并告诉我遗漏了多少计数,例如,如果发生这种情况:

a1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,
      1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,12,13,14,15,16,17,18,19,20,21,22,23,24,  25, 26,  5, 6, 7, 8, 9, 10, 11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,1,2]

我会得到总共92个,但得到的反馈是8个缺失

我已经非常熟悉以下代码:

Blk_Tot = 0

CBN = 0
LBN = 0
x = 0
y = 0
z = 0
MissedBlocks = 0

for i in range(len(a1)):
    CBN = a1[i]
    if CBN - LBN <= 0:
        if LBN == 30:
            y = 30 - abs(CBN - LBN)
        elif LBN < 30:
            z = 30 - LBN
            y = 30 - abs(CBN - LBN) + z
            print(z)




        Blk_Tot = Blk_Tot + y
    else:
        x = CBN - LBN
        Blk_Tot = Blk_Tot + x
        if x > 1:
            MissedBlocks = MissedBlocks - 1 + x
    LBN = CBN


print(Blk_Tot)

print(MissedBlocks)

如果我删除1到30之间的任何一个,它都能很好地工作,但是如果我删除30,比如说29,30,1,2,它就会中断。我不希望它能够连续删除30个,并且仍然能够得到一个合适的总数

有人对如何实现这一目标有什么想法吗?我觉得我错过了一个显而易见的答案:D

对不起,我想我不清楚,a1是一个来自外部设备的计数器,它从1计数到30,然后再绕到1。每个计数实际上是消息的一部分,以表明消息已被接收;比如说12 4,我知道第三条信息不见了。我想做的是找出应该收到的总数,以及从计数中漏掉多少

更新以下帖子中的想法后,另一种方法可能是:

输入:

123456

清单[1,2,3,4,5,6]

1.检查第一个输入,查看它在列表的哪个部分,然后从那里开始(如果我们不是从零开始)

2.每次接收到输入时,检查是否与数组中的下一个值匹配

3.如果没有,那么需要多少步骤才能找到该值


Tags: 代码消息ifa1计数器abs计数print
3条回答

一般来说,如果要计算两个列表之间的差异数量,可以很容易地使用字典。另一个答案也可以,但对于稍微大一点的列表来说,它效率很低

def counter(lst):
    # create a dictionary with count of each element
    d = {}
    for i in lst:
        if d.get(i, None):
            d[i] += 1
        else:
            d[i] = 1
    return d 

def compare(d1, d2):
    # d1 and d2 are dictionaries
    ans = 0
    for i in d1.values():
        if d2.get(i, None):
            # comapares the common values in both lists
            ans += abs(d1[i]-d2[i])
            d2[i] = 0
        else:
            #for elements only in the first list
            ans += d1[i]
    for i in d2.values():
        # for elements only in the second list
        if d2[i]>0:
            ans += d2[i]
    return ans
    
l1 = [...]
l2 = [...]
print(compare(counter(l1), counter(l2)))

用于检查重复序列模式中缺少元素的新代码

现在我已经更清楚地理解了你的问题,下面是代码。此代码中的假设是列表将始终按从1到30的升序排列,然后从1开始再次重复。在1和30之间可能会缺少元素,但在1和30之间的顺序将始终是升序

如果源数据如列表a1所示,那么代码将导致8缺少元素

a1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,
      1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,
      5, 6, 7, 8, 9, 10, 11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,
      1,2]

a2 = a1.copy()

c = 1
missing = 0
while a2:
    if a2[0] == c:
        c+=1
        a2.pop(0)
    elif a2[0] > c:
        missing +=1
        c+=1
    elif a2[0] < c:
        missing += 31-c
        c = 1

    if c == 31: c=1

print (f'There are {missing} items missing in the list')

其输出将为:

There are 8 items missing in the list

如果这能解决您的问题,请告诉我

比较两个列表的早期代码

不能使用set,因为这些项是重复的。因此,您需要sort它们,并找出每个元素在两个列表中的次数。差额将为您提供缺少的计数。您可以在a1中有一个元素,但在a2中没有,反之亦然。因此,找出丢失项目的绝对计数将给出结果

在下一次更新中,我将使用更好的变量更新响应

我是这样做的:

带注释的代码:

a1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,
      1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,
      1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]

a2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,
      1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,
      5, 6, 7, 8, 9, 10, 11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,1,2]

#step 1: Find out which list is longer. We will use that as the master list
if len(a1) > len(a2):
    master_list = a1.copy()
    second_list = a2.copy()
else:
    master_list = a2.copy()
    second_list = a1.copy()

#step 2: We must sort both master and second list
#        so we can compare against each other
master_list.sort()
second_list.sort()

#set the counter to zero
missing = 0

#iterate through the master list and find all values in master against second list
#for each iteration, remove the value in master[0] from both master and second list
#when you have iterated through the full list, you will get an empty master_list
#this will help you to use while statement to iterate until master_list is empty
while master_list:
    #pick the first element of master list to search for
    x = master_list[0]
    #count the number of times master_list[0] is found in both master and second list
    a_count = master_list.count(x)
    b_count = second_list.count(x)

    #absolute difference of both gives you how many are missing from each other
    #master may have 4 occurrences and second may have 2 occurrences. abs diff is 2
    #master may have 2 occurrences and second may have 5 occurrences. abs diff is 3
    missing += abs(a_count - b_count)

    #now remove all occurrences of master_list[0] from both master and second list
    master_list = [i for i in master_list if i != x]
    second_list = [i for i in second_list if i != x]

#iterate until master_list is empty

#you may end up with a few items in second_list that are not found in master list
#add them to the missing items list
#thats your absolute total of all missing items between lists a1 and a2

#if you want to know the difference between the bigger list and shorter one,
#then don't add the missing items from second list
missing += len(second_list)

#now print the count of missig elements between the two lists
print ('Total number of missing elements are:', missing)

此操作的输出为:

Total number of missing elements are: 7

如果您想找出缺少哪些元素,那么需要再添加几行代码

在上面的示例中,元素27,28,29,30, 4, 5a2中缺失,元素31a1中缺失。所以缺失元素的总数是7

无注释代码:

a1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,
      1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,
      1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]

a2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,
      1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,
      5, 6, 7, 8, 9, 10, 11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,1,2]

if len(a1) > len(a2):
    master_list = a1.copy()
    second_list = a2.copy()
else:
    master_list = a2.copy()
    second_list = a1.copy()

master_list.sort()
second_list.sort()

missing = 0

while master_list:
    x = master_list[0]

    a_count = master_list.count(x)
    b_count = second_list.count(x)

    missing += abs(a_count - b_count)

    master_list = [i for i in master_list if i != x]
    second_list = [i for i in second_list if i != x]

missing += len(second_list)
print ('Total number of missing elements are:', missing)

如果你超过了30线,你不需要跟踪

只需与理想序列进行比较,并计算缺失的数字

不知道最后是否缺少零件

如果一个块中缺少30多个零件,则不知道

from itertools import cycle

def idealSeqGen():
  for i in cycle(range(1,31)):
    yield(i)

def receivedSeqGen():
  a1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,
                    5, 6, 7, 8, 9, 10, 11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,
        1,2]
  for i in a1:
    yield(i)

receivedSeq = receivedSeqGen()
idealSeq = idealSeqGen()

missing = 0
ideal = next(idealSeq)
try:
  while True:
    received = next(receivedSeq)
    while received != ideal:
      missing += 1
      ideal = next(idealSeq)
    ideal = next(idealSeq)
except StopIteration:
  pass

print (f'There are {missing} items missing')

编辑

循环部分可以简单一点

missing = 0
try:
  while True:
    ideal = next(idealSeq)
    received = next(receivedSeq)
    while received != ideal:
      missing += 1
      ideal = next(idealSeq)
except StopIteration:
  pass

print (f'There are {missing} items missing')

相关问题 更多 >