简体   繁体   English

获取列表中所有重复元素的一半

[英]get half of all repeating elements in a list

My goal is to create a list with half of the individual items repeating.我的目标是创建一个列表,其中重复了一半的单个项目。 In the list below I have given an example.在下面的列表中,我给出了一个例子。 In this list, except for the first element, each marked element is repeated 2n times, therefore all elements are even and divisible by 2. I can't create a list comprehension that divides by 2 the number of times each element is repeated.在这个列表中,除了第一个元素,每个标记的元素都重复了 2n 次,因此所有元素都是偶数并且可以被 2 整除。我无法创建一个列表推导式来除以每个元素重复的次数。 For example the element: [0, 255, 0] is repeated 6 times in the list;例如元素: [0, 255, 0]在列表中重复了 6 次; I expect in my new list to be repeated only 3 times.我希望在我的新列表中只重复 3 次。

Input list:输入列表:

[[255, 255, 255], [255, 0, 0], [255, 0, 0], [0, 255, 0], [0, 255, 0],
 [0, 255, 0], [0, 255, 0], [0, 255, 0], [0, 255, 0]]

Expected output list:预期 output 列表:

[[255, 255, 255], [255, 0, 0], [0, 255, 0], [0, 255, 0], [0, 255, 0]]

You could use the Counter class form collections to compute the number of repetition then a comprehension to repeat the distinct values half the number of times they occurred:您可以使用计数器 class 表格 collections 来计算重复次数,然后理解重复不同值的次数的一半:

from collections import Counter

L = [[255, 255, 255], [255, 0, 0], [255, 0, 0], [0, 255, 0], 
    [0, 255, 0], [0, 255, 0], [0, 255, 0], [0, 255, 0], [0, 255, 0]]

R = [ t for t,count in Counter(map(tuple,L)).items() 
        for _ in range(count//2 or 1) ]

print(R)    
[(255, 255, 255), (255, 0, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0)]

Note that i'm converting the sublists into tuple because the Counter dictionary needs a hashable key value.请注意,我正在将子列表转换为元组,因为 Counter 字典需要一个可散列的键值。 You can convert them back into lists in the comprehension if you need the output to be a list of lists.如果您需要 output 成为列表的列表,您可以将它们转换回理解中的列表。

For consecutive repetitions:对于连续重复:

If your repetitions are always consecutive, you can use a more efficient approach by filtering/including every other repetition (at even relative occurence):如果您的重复始终是连续的,则可以通过过滤/包括所有其他重复(甚至相对发生)来使用更有效的方法:

Using a simple loop:使用一个简单的循环:

include  = True
result   = L[:1]
for s in L[1:]:
    include = True if s != result[-1] else not include
    if include:
        result.append(s)

print(result)
[[255, 255, 255], [255, 0, 0], [0, 255, 0], [0, 255, 0], [0, 255, 0]]

or the groupby/islice functions from itertools:或 itertools 中的 groupby/islice 函数:

from itertools import groupby,islice

result = [ s for _,g in groupby(L) for s in islice(g,0,None,2) ]

print(result)
[[255, 255, 255], [255, 0, 0], [0, 255, 0], [0, 255, 0], [0, 255, 0]]    
 

You can use answer from shubham koli than create a new list as follow您可以使用shubham koli的回答,而不是创建一个新列表,如下所示

list = [
  [255, 255, 255],
  [255, 0, 0], [255, 0, 0],
  [0, 255, 0], [0, 255, 0], [0, 255, 0], [0, 255, 0], [0, 255, 0], [0, 255, 0]
]


temp_list = []
for i in list:
    if i not in temp_list:
        temp_list.append(i)

result_list = []
for i in range(0, len(temp_list)):
  if i == 0:
    result_list.append(temp_list[i])
  else:
    n = list.count(temp_list[i])
    for j in range(n // 2):
      result_list.append(temp_list[i])

print(result_list)

which produces产生

[[255, 255, 255], [255, 0, 0], [0, 255, 0], [0, 255, 0], [0, 255, 0]]

as requested.按照要求。 Number of repreated items is store in n and is obtained using .count() from a list.重复项目的数量存储在n中,并使用列表中的.count()获得。 Later n // 2 will give half of its value in integer.稍后n // 2将在 integer 中给出其值的一半。

  res_lst = [item for k, item in enumerate(list) if k == 0 or k % 2 != 0]

    print(res_lst)

    res_lst = [[255, 255, 255], [255, 0, 0], [0, 255, 0], [0, 255, 0], [0, 255, 0]]

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

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