【问题标题】:How to remove items that are not in all the other lists如何删除不在所有其他列表中的项目
【发布时间】:2017-10-24 09:16:35
【问题描述】:

在 python 3 中:

我有一些列表。一组列表是时间戳,另一组是对应于该时间的度量。我有5很多这样的。因此,我们将列表称为:Time1、Time2、Time3、Time4、Time5 和 M1、M2、M3、M4、M5。这个列表应该都是相同的大小,但不幸的是它们不是。我想遍历所有列表,确保每个时间都在每个列表中,如果不是,则从列表中删除该时间戳并删除相应的测量值。所以我最终所有列表的长度都相等,并且 Time1-5 具有完全相同的时间戳。

最pythonic和最快的方法是什么?

例如:

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

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]

处理后:

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

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

Time3 = [1, 2, 4, 5, 6] 和 M3 = [6, 18, 91, 10, 7]

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

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

【问题讨论】:

  • 请分享一些具有预期输出的示例数据。
  • 我现在已经编辑了问题。谢谢。

标签: python-3.x


【解决方案1】:

我假设 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 不更新。你可以安排他们。

【讨论】:

    【解决方案2】:

    你可以这样做:

    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)]
    

    【讨论】:

      【解决方案3】:
      • 如果您更改 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]
      

      【讨论】:

      • 请接受作为答案,如果您对答案满意,请点赞。
      【解决方案4】:

      鉴于已经提供了 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]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-10-09
        • 1970-01-01
        • 2014-09-15
        • 1970-01-01
        • 1970-01-01
        • 2021-03-12
        • 2020-06-08
        • 2020-03-17
        相关资源
        最近更新 更多