简体   繁体   English

填写列表中的缺失值

[英]Fill missing values in lists

I have a list which consists of 0's and 1's. 我有一个包含0和1的列表。 The list should ideally look like this 0,1,0,1,0,1,0,1,0,1,0,1,0,1..... 理想情况下,列表应该看起来像是0,1,0,1,0,1,0,1,0,1,0,1,0,1 .....

But due to some error in logging, my list looks like this: 0,1,0,1,1,1,0,1,0,0,0,1,0,1.... As one can clearly there are some missed 0's and 1's in middle. 但是由于记录错误,我的列表如下所示:0,1,0,1,1,1,0,1,0,0,0,1,0,1 ....在中间遗漏了0和1。 How can I fix this list to add those 0's and 1's in between the missing elements so as to get to the desired list values. 如何修复此列表,以便在缺失的元素之间添加那些0和1,以便获得所需的列表值。

Here is the code used by me, this does the task for me but it is not the most pythonic way of writing scripts. 这是我使用的代码,它为我完成了任务,但这不是编写脚本的最Python方式。 So how can I improve on this script? 那么如何改进此脚本?

l1 = [0,1,0,1,1,1,0,1,0,0,0,1,0,1]
indices = []
for i in range(1,len(l1)):
    if l1[i]!=l1[i-1]:
        continue
    else:
        if l1[i]==0:
            val=1
        else:
            val=0
        l1.insert(i, val)

EDIT 编辑

As asked in the comments, Let me explain why is this important rather than generating 1's and 0's. 正如评论中的要求,让我解释一下为什么这么重要而不是生成1和0。 I have TTL pulse coming ie a series of HIGH(1) and LOW(0) coming in and simultaneously time for each of these TTL pulse is logged on 2 machines with different clocks. 我有TTL脉冲即将到来,即一系列HIGH(1)和LOW(0)进入,并且同时将这些TTL脉冲中的每一个的时间记录在2个具有不同时钟的机器上。

Now while machine I is extremely stable and logging each sequence of HIGH(1) and low(1) accurately, the other machine ends up missing a couple of them and as a result I don't have time information for those. 现在,虽然我的机器非常稳定,并且准确记录了HIGH(1)和low(1)的每个序列,但另一台机器最终却丢失了其中的几个,因此我没有时间信息。

All I wanted was to merge the missing TTL pulse on one machine wrt to the other machine. 我只想将一台机器上丢失的TTL脉冲合并到另一台机器上。 This will now allow me to align time on both of them or log None for not received pulse. 现在,这将使我可以同时调整它们的时间或将“ None” (未接收到的脉冲)记录为“ None ”。

Reason for doing this rather than correcting the logging thing (as asked in comments) is that this is an old collected data. 这样做而不是更正日志记录内容(如注释中所述)的原因是,这是一个旧的收集数据。 We have now fixed the logging issue. 现在,我们已修复了日志记录问题。

why would you have a list of 0,1,0,1,0,1 ? 为什么会有0,1,0,1,0,1的清单? there is no good reason i can think of. 我没有想到的充分理由。 oh well thats beyond the scope of this question i guess... 哦,那超出了这个问题的范围,我想...

list(itertools.islice(itertools.cycle([0,1]),expected_length))

You can try something like this: 您可以尝试如下操作:

from itertools import chain

l1 = [0,1,0,1,1,1,0,1,0,0,0,1,0,1]

c = max(l1.count(0), l1.count(1))
print list(chain(*zip([0]*c,[1]*c)))

Output: 输出:

[0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1]

Just multiply a new list. 只需乘以一个新列表即可。

>>> l1 = [0,1,0,1,1,1,0,1,0,0,0,1,0,1]
>>> l1
[0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1]
>>> [0,1] * (len(l1)//2)
[0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1]

If the list has an odd number of elements, add the necessary 0 : 如果列表中元素的数量为奇数,请添加必要的0

>>> l2 = [0,1,0,1,1,1,0,1,0,0,0,1,0,1,0]
>>> l2_ = [0,1] * (len(l1)//2)
>>> if len(l2)%2: l2_.append(0)
...
>>> l2
[0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0]
>>> l2_
[0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM