简体   繁体   English

python:将元素组合在一起

[英]python : group elements together in list

I'm currently working with itertools to create and return a list whose elements are lists that contain the consecutive runs of equal elements of the original list. 我目前正在使用itertools创建并返回一个列表,其列表的元素是包含原始列表的相等元素的连续运行的列表。

import itertools
it = [1, 1, 5, 5, 5, 'test', 'test', 5]

new = len(it)
for a in range(new):
  return [list(k) for a, k in itertools.groupby(it)] 

For the above example the result is: 对于上面的例子,结果是:

[[1, 1], [5, 5, 5], ['test', 'test'], [5]]

Can I achieve this without using itertools ? 我可以不使用itertools实现这一目标吗?

To be honest a simple for loop could make this work, you don't even have to import itertools . 说实话,一个简单的for循环可以使这个工作,你甚至不必导入itertools

The simplest way to do this is by using this: 最简单的方法是使用这个:

it = [1, 1, 5, 5, 5, 'test', 'test', 5]
result = []
for (i, x) in enumerate(it):
  if i < 1 or type(x) != type(it[i - 1]) or x != it[i - 1]:
    result.append([x])
  else:
    result[-1].append(x)
print(result)

Or, in function form: 或者,在功能形式:

def type_chunk(it):
  result = []
  for (i, x) in enumerate(it):
    if i < 1 or type(x) != type(it[i - 1]) or x != it[i - 1]:
      result.append([x])
    else:
      result[-1].append(x)
  return result

You would then use the function like this: 然后你会使用这样的函数:

print(type_chunk([1, 1, 5, 5, 5, 'test', 'test', 5]))

You could even skip the type checking and only look for equal values: 你甚至可以跳过类型检查,只查找相同的值:

def type_chunk(it):
  result = []
  for (i, x) in enumerate(it):
    if i < 1 or x != it[i - 1]:
      result.append([x])
    else:
      result[-1].append(x)
  return result

Good luck. 祝好运。

You can pair adjacent items by zipping the list with itself but with a padding of float('nan') since it can't be equal to any object, and then iterate through the zipped pairs to append items to last sub-list of the output list, and add a new sub-list when the adjacent items are different: 您可以通过将列表与自身相互压缩来配对相邻项目,但是使用float('nan')填充,因为它不能等于任何对象,然后遍历压缩对以将项目附加到最后的子列表中输出列表,并在相邻项目不同时添加新的子列表:

output = []
for a, b in zip([float('nan')] + it, it):
    if a != b:
        output.append([])
    output[-1].append(b)

output becomes: output变为:

[[1, 1], [5, 5, 5], ['test', 'test'], [5]]

You could have a look at the function in itertools to see how they are doing it. 您可以查看itertools中的函数,了解它们是如何进行的。

Here is one way which shows the logic clearly (can be further reduced): 这是一种清楚地显示逻辑的方法(可以进一步减少):

def i_am_itertool():
    it = [1, 1, 5, 5, 5, 'test', 'test', 5]
    ret = []

    temp = []
    last = it[0]
    for e in it:
        if e == last:
            temp.append(e)
        else:
            ret.append(temp)  # Add previous group
            temp = [e]  # Start next group
            last = e
    ret.append(temp)  # Add final group
    return ret

print(i_am_itertool())

Output: 输出:

 [[1, 1], [5, 5, 5], ['test', 'test'], [5]]

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

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