筛选三个列表并编写最终列表

时间:2014-07-17 07:59:21

标签: python if-statement for-loop

我有3个列表,1个2-D和2个1-D这个问题。二维列表'间隔'由[时间,点1,点2],1-D列表'点'组成。由[点]和1-D列表'时间'组成。由[时间]组成。

在每个"时间",[point]中的多个项目可以在[point1,point2]范围内。从列表'间隔'," time = 1"," points" 2,3和4位于范围" point1" to" point2"。在"时间= 4",有4组[point1,point2],各种项目都在这些范围内。

我正在尝试检查来自"点"位于[point1,point2]范围内,并根据它们是否存在于范围内来指定1或0。然后在相同的"时间"检查是否有多组[point1,point2],如果有,确保所有出现的"指向"在[point1,point2]范围内捕获并复制到最终列表中。因此,最终列表应该是一个32 * 3的列表,捕获所有出现的" point"在[point1,point2]范围内#34;时间"。最后一点我错了。任何有关以下代码的帮助都将受到高度赞赏。谢谢!

import math
import random
import numpy as np

intervals = [[1,1,5],[2,1,3],[2,2,4],[3,2,4],[3,3,6],[3,6,8],[4,5,9],[4,3,10],[4,6,8],[4,5,10]] 
points = [1,2,3,4,5,6,7,8] 
times = [1,2,3,4] 
count = []
new_array = []
final_array = []


for t in times:
    for j in range(len(intervals)):
        if intervals[j][0] == t:
            for i in range(len(points)):
                print('t=', t, 'j=', j, 'i=', i, intervals[j][0])   
                if points[i] >= intervals[j][1] and points[i] < intervals[j][2]:
                    count.append([t, 1, points[i]])
                else:
                    count.append([t, 0, points[i]])

len_count = len(count) - 4

count = np.array(count)

for k in range(4,len_count,1):
    y = k-4
    z = k+4
    print(y,z)      
    if count[y][0] == count[z][0] and count[y][1] != count[z][1]:
        if count[y][1] == 0:
            count[y][1] = 1
        else:
            count[z][1] = 1  
    new_array.append([count[y][0], count[y][1], count[y][2]])

new_array = np.array(new_array)

np.savetxt('new_array.txt', new_array)

len_new_count = len(new_array)-8


for t in times:
    for k in range(8,len_new_count,1):
        r = k-8
        s = k+8
        print(r,s)
        if new_array[k][0] == t:
            if new_array[k][0] == new_array[r][0]:
                new_array[r][:] = new_array[k][:]
                k = k+8
                final_array.append([new_array[k][0], new_array[k][1], new_array[k][2]])

输出缺少&#34;时间&#34; 1和2打印数据&#34;时间&#34; 3和&#34; 4&#34;两次。

谢谢!

0 个答案:

没有答案