Python列表附加:每次迭代有多个附加

时间:2018-10-19 11:15:14

标签: python python-3.x pandas list append

我有一个数据框,其中包含许多行row(60000的顺序),其中包含三个字段:

  1. shift:96个字符的字符串,指示位置或'r'(无位置;例如'rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrraaabbbbbbbrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr'
  2. 开始:指定的起点(已索引1个)
  3. 停止:指定的停止点(已索引1个)

现在,我想同时为位置'a''b'映射。此映射显示在哪个时间点上,哪个row包含ab。因此,我们有:

mapping['a'] = [[]]*96 # list of length 96 with, initially, and empty list for the row-indexes. `
mapping['b'] = [[]]*96 # list of length 96 with, initially, and empty list for the row-indexes. `

for index, row in pd_shifts.iterrows():
    for t in range(row['start']-1,row['stop']):
        loc = row['shift'][t] # either 'a' or 'b'
        if loc != 'r': # 'r' can be ignored.
            mapping[loc][t].append(index)

我使用上面的for循环在时刻loc上找到t,并将其附加到mapping[loc][t]上。似乎很容易。但是,每个索引在字符串中添加了ab的次数。输出片段:
1535,1535,1536,1536,1536,1536,1536,1537,1537,

这是怎么回事? 为什么每个索引附加a的次数是连续的?

尝试

我检查了每行是否具有唯一索引,并且每行仅迭代一次。
而且,每个起点和终点(及其之间的点)都被访问一次(“ print(t)”)。

0 个答案:

没有答案
相关问题