如何删除不在所有其他列表中的项目

时间:2017-10-24 09:16:35

标签: python-3.x

在python 3中:

我有一些列表。一组列表是时间戳,另一组是与该时间对应的度量。我有5批这些。因此,让我们调用列表:Time1,Time2,Time3,Time4,Time5和M1,M2,M3,M4,M5。这个列表应该都是相同的大小,但不幸的是它们不是。我想循环遍历所有列表,确保每个时间都在每个列表中,如果不是,则从列表中删除该时间戳并删除相应的测量值。所以我最终得到的所有列表的长度相等,而Time1-5的所有时间戳都相同。

最蟒蛇最快的方式是什么?

例如:

原始时间戳和相应的测量值:

时间1 = [1,2,3,4,5,6]和M1 = [5,6,7,8,9,10]

时间2 = [1,2,3,4,5,6,7]和M2 = [6,11,8,9,10,4,7]

时间3 = [1,2,4,5,6]和M3 = [6,18,91,10,7]

时间4 = [1,2,3,4,5,6]和M4 = [50,16,72,18,9,10]

时间5 = [1,2,3,4,5,6]和M5 = [24,32,11,2,9,1]

处理完毕后:

时间1 = [1,2,4,5,6]和M1 = [5,6,8,9,10]

时间2 = [1,2,4,5,6]和M2 = [6,11,9,10,4]

时间3 = [1,2,4,5,6]和M3 = [6,18,91,10,7]

时间4 = [1,2,4,5,6]和M4 = [50,16,18,9,10]

时间5 = [1,2,4,5,6]和M5 = [24,32,2,9,1]

4 个答案:

答案 0 :(得分:0)

我假设time1,...,time5,M1,...,M5已定义。

我确信这不是最快捷的方式,但有效:

times = [time1, time2, time3, time4, time5]
Ms = [M1, M2, M3, M4, M5]

def intersection(l):
    l = [set(item) for item in l]
    return set.intersection(*l)

def func():
    if len(times) != len(Ms):
        print("!!! List numbers are not same !!!")
        return None

    time_intersection = intersection(times)

    for i, time in enumerate(times):
        for j, time_stamp in enumerate(time):
            if time_stamp not in time_intersection:
                Ms[i][j] = -1
        #Update
        Ms[i] = [x for x in Ms[i] if x != -1]       
        times[i] = list(time_intersection)  

func()  
print("times:\t" + str(times))
print("Ms:\t" + str(Ms))

输出:

times:  [[1, 2, 4, 5, 6], [1, 2, 4, 5, 6], [1, 2, 4, 5, 6], [1, 2, 4, 5, 6], [1, 2, 4, 5, 6]]
Ms:     [[5, 6, 8, 9, 10], [6, 11, 9, 10, 4], [6, 18, 91, 10, 7], [50, 16, 18, 9, 10], [24, 32, 2, 9, 1]]

timesMs列表已更新。 time1,...,time5,M1,...,M5不会更新。你可以安排它们。

答案 1 :(得分:0)

您可以这样做:

Times = [Time1, Time2, Time3, Time4, Time5]
WantedTime = list(set(Time1).intersection(*Times[1:]))
def TimeToM(Time,M):
    idx = [i for i, x in enumerate(Time) if x in WantedTime]
    return [y for i, y in enumerate(M) if i in idx]
M1 = TimeToM(Time1, M1)
...
Time1, Time2, Time3, Time4, Time5 = [WantedTime for _ in range(len(Times)]

答案 2 :(得分:0)

  • 如果您更改print声明
  • ,它也适用于2.7+
  • 删除最后一个打印语句,您将得到您想要的内容
  • 如果您有更多变量Time, M
  • ,则自动生效
  • 如果您有更多变量,则更改范围
  • 少代码

代码:

Time1 = [1, 2, 3, 4, 5, 6];M1 = [5, 6, 7, 8, 9, 10];Time2 = [1, 2, 3, 4, 5, 6, 7];M2 = [6, 11, 8, 9, 10, 4, 7];Time3 = [1, 2, 4, 5, 6];M3 = [6, 18, 91, 10, 7];Time4 = [1, 2, 3, 4, 5, 6];M4 =[50, 16, 72, 18, 9, 10];Time5 = [1, 2, 3, 4, 5, 6];M5 = [24, 32, 11, 2, 9, 1]

common_Time = set(Time1)
for i in range(2,6):
     common_Time = common_Time & set(eval('Time'+str(i)))

for i in range(1,6):
     for index, value in enumerate(set(eval('Time'+str(i)))-common_Time):
         tmp_key=eval('Time'+str(i)).index(value)-index
         del eval('M'+str(i))[tmp_key]
     locals()['Time'+str(i)] = list(common_Time)
     print("Time{indx} = {time} and M{indx} = {m}".format(indx=str(i), time=str(list(common_Time)),m =str(locals()['M'+str(i)])))

输出:

Time1 = [1, 2, 4, 5, 6] and M1 = [5, 6, 8, 9, 10]
Time2 = [1, 2, 4, 5, 6] and M2 = [6, 11, 9, 10, 4]
Time3 = [1, 2, 4, 5, 6] and M3 = [6, 18, 91, 10, 7]
Time4 = [1, 2, 4, 5, 6] and M4 = [50, 16, 18, 9, 10]
Time5 = [1, 2, 4, 5, 6] and M5 = [24, 32, 2, 9, 1]

答案 3 :(得分:0)

鉴于已经提供了t1,t2 ... t5和m1,m2 ... m5,这里有一个使用列表理解的可能解决方案(太多)

# Create a nested list of time and measurement
t = [t1,t2,t3,t4,t5]
m=[m1,m2,m3,m4,m5]

# Find intersection of all values time
common = set(t[0]).intersection(*t)

# Create a dictionary by mapping elements of respective time and 
# measurement [t1:m1, t2:m2 and so on]
tm = [dict(zip(keys, vals)) for keys, vals in zip(t,m)]

# Get keys which are in common
t = [[k for k in d.keys() if k in common] for d in tm]

# Get values which respective keys in t
m = [[d[k] for k in d.keys() if k in common]for d in tm]