简体   繁体   English

如何将列表分成 4 个一组,其中第一个元素大于最后一个?

[英]How to split a list into groups of 4 where first elements are greater than the last?

I have this list of integers that I want to split into groups of 4 elements by condition:我有这个整数列表,我想按条件将它们分成 4 个元素的组:

result = [0, 4, 10, 6, 15, 9, 18, 35, 40, -30, -90, 99]

The first item of each group should be greater than the fourth following item.每组的第一项应大于以下第四项。 Output should be like this:输出应该是这样的:

[[10, 6, 15, 9], [18, 35, 40, -30], [35, 40, -30, -90]]

but how do I apply that conditions in this code?但是我如何在这段代码中应用这些条件?

split_no = 4
list(split_list(result, split_no))

Currently my output looks like this:目前我的输出是这样的:

[[0, 4, 10, 6], [15, 9, 18, 35], [40, -30, -90, 99]]

You could do the following您可以执行以下操作

arr1 = [0, 4, 10, 6, 15, 9, 18, 35, 40, -30, -90, 99]
arr2=[arr1[x:x+4] for x in range(0, len(arr1)) if len(arr1[x:x+4])==4]
for each in arr2[:]:
    if each[0]<=each[3]:
        arr2.remove(each)

Now, if you try printing out arr2;现在,如果您尝试打印出 arr2;

print(arr2)

you get:你得到:

[[10, 6, 15, 9], [18, 35, 40, -30], [35, 40, -30, -90]]

First, a list of all the sub-list of length 4 is created, using Python's list comprehension :首先,使用Python 的 list comprehension创建长度为 4 的所有子列表的列表

input_list = [0, 4, 10, 6, 15, 9, 18, 35, 40, -30, -90, 99]

all_sublists = [input_list[i:i+4] for i in range(len(input_list)-3)]

print(all_sublists)
# [[0, 4, 10, 6], [4, 10, 6, 15], [10, 6, 15, 9], [6, 15, 9, 18], [15, 9, 18, 35], [9, 18, 35, 40], [18, 35, 40, -30], [35, 40, -30, -90], [40, -30, -90, 99]]

Then, the sub-lists are filtered using the wanted conditions:然后,使用所需条件过滤子列表:

output = [sublist for sublist in all_sublists if sublist[0]>sublist[-1]]

print(output)
# [[10, 6, 15, 9], [18, 35, 40, -30], [35, 40, -30, -90]]

The one-line solution could be:单行解决方案可能是:

output = [input_list[i:i+4] for i in range(len(input_list)-3)
          if input_list[i]>input_list[i+3]]

print(output)
# [[10, 6, 15, 9], [18, 35, 40, -30], [35, 40, -30, -90]]

Answers from xdze2 and gireesh4manu have a drawback of keeping all subsequences of length 4 in memory.来自xdze2gireesh4manu 的答案有一个缺点,就是将所有长度为 4 的子序列保留在内存中。 We can avoid that by using a rolling window iterator .我们可以通过使用滚动窗口迭代器来避免这种情况。

For example, taking one from this answer and improving it a bit:例如,从这个答案中取出一个并稍微改进一下:

from itertools import islice, tee

def windows(iterable, size): 
    iterators = tee(iterable, size) 
    iterators = [islice(iterator, i, None) for i, iterator in enumerate(iterators)]  
    yield from map(list, zip(*iterators))

def is_valid(seq):
    return seq[0] > seq[-1]

result = [0, 4, 10, 6, 15, 9, 18, 35, 40, -30, -90, 99]
print(list(filter(is_valid, windows(result, 4))))

will give you:会给你:

[[10, 6, 15, 9], [18, 35, 40, -30], [35, 40, -30, -90]]

暂无
暂无

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

相关问题 分割第二个元素大于阈值的元组列表 - Split list of tuples where second elment is greater than threshold 在python的列表中查找大于0的第一个和最后一个值 - Find first and last value greater than 0 in a list in python 查找列表中值大于常量的第一个元素 - Find first element in list where value is greater than a constant 如果列表中的元素数大于 2,则将列表前半部分中的 n 个元素与列表另一半中的元素组合 - Combine n elements in first half of list with elements in other half of a list, if number of elements in a list is greater than 2 如何将列表分为相等的子列表,最后一个子列表由列表的第一个可能的元素组成 - How to split list into equal sublists with the last sublist made up from the first possible elements of the list 列表的大小如何大于生成该列表元素的生成器? - How does a is the size of a list greater than the generator that yields elements of that list? 如果列表中的最后n个元素大于一个数字,则返回列表中的剩余项 - Return remaining items in list, if last n elements in list greater than a number Python-如何生成大小大于列表元素个数的排列 - Python - How to generate permutations of size greater than the number of list elements 在 Pandas 中:如何检查列表元素是否大于数据框列值 - In Pandas : How to check a list elements is Greater than a Dataframe Columns Values 如果它们的索引 mod 4 大于 1,如何将列表的元素变成它们的负数? - How to turn elements of a list into their negative counterpart if their index mod 4 is greater than 1?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM