简体   繁体   English

如何根据原始列表中与设置值不同的元素对列表进行切片(或创建子列表)

[英]How to slice a list (or create sublists) based on elements in original list that differ by a set value

This is my first time asking a question here, so please don't roast me too much as I try to get the swing of things. 这是我第一次在这里提出问题,所以请不要在我试图使事情发生变化的时候把我烤得太多。

I am working on some code using Python 3. So, in my code I generate a list. 我正在使用Python 3处理一些代码。因此,在我的代码中生成了一个列表。 For the sake of specificity, suppose I am working with the following list: 为了具体起见,假设我正在使用以下列表:

a = [3344, 3354, 3364, 3364, 3374, 3374, 3384, 3384, 3394, 3394, 3404, 3404, 3414, 3414, 3424, 3434]

I would like to sort this list into the following sublists: 我想将此列表分为以下子列表:

sub1 = [3344, 3354, 3364, 3374]
sub2 = [3364, 3374, 3384, 3394]
sub3 = [3384, 3394, 3404, 3414]
sub4 = [3404, 3414, 3424, 3434]

So, I would like to sort the original list into sublists based on the elements that differ by a known value, in this case 10. 因此,我想根据以已知值(在本例中为10)不同的元素将原始列表分类为子列表。

I am unable to use index-based slicing and have not yet been able to implement any kind of lambda function to accomplish this. 我无法使用基于索引的切片,还无法实现任何形式的lambda函数来完成此操作。 I have searched through the depths of Stack Overflow and have not found anything particularly helpful. 我搜索了堆栈溢出的深度,但没有发现任何特别有用的信息。

Thank you for the help. 感谢您的帮助。

Update: 更新:

I wanted to add some more information to clear up any confusion resulting from lack of specificity in my language. 我想添加更多信息,以消除由于我的语言缺乏特异性而引起的任何混乱。 The goal is to generate sublists with four elements from the original list, with the elements of the sublist differing by a value of 10. 目标是从原始列表中生成包含四个元素的子列表,该子列表的元素相差10。

To better explain what I am trying to accomplish here, I will provide a second list and the desired output sublists. 为了更好地解释我要在此处完成的工作,我将提供第二个列表和所需的输出子列表。

b = [3384, 3404, 3344, 3394, 3374, 3414, 3354, 3404, 3384, 3424, 3364, 3414, 3394, 3364, 3434, 3374]

The desired output would look something like this: 所需的输出如下所示:

sublist1 = [3344, 3354, 3364, 3374]
sublist2 = [3364, 3374, 3384, 3394]
sublist3 = [3384, 3394, 3404, 3414]
sublist4 = [3404, 3414, 3424, 3434]

I need to be able to generate this output, as these sublists will be used as input values for different functions later on... Capturing the degenerate values is a must. 我需要能够生成此输出,因为稍后这些子列表将用作不同功能的输入值...捕获退化值是必须的。

I hope this clears things up! 我希望这可以清除一切!

You can use a nested for loop that appends an item in a to an existing sublist whose length is less than 4 and whose last item is 10 less than the item, or otherwise create a new sublist with the item: 您可以使用嵌套for循环,在追加一个项目a到一个现有的子列表,其长度小于4,其最后一个项目比项目少10,或以其他方式创建这一项目的新子表:

l = []
for i in a:
    for s in l:
        if len(l) < 4 and s[-1] + 10 == i:
            s.append(i)
            break
    else:
        l.append([i])

Given your sample input, l would become: 给定您的样本输入, l将变为:

[
    [3344, 3354, 3364, 3374],
    [3364, 3374, 3384, 3394],
    [3384, 3394, 3404, 3414],
    [3404, 3414, 3424, 3434]
]

Note that your expected output is incorrect because the items in your input a can be sorted into two sublists without breaking the rule of all adjacent items differing by 10. 请注意,您的预期输出不正确,因为您可以将输入a的项目分类为两个子列表,而不会破坏所有相邻项目相差10的规则。

Use sorted with set and a loop 使用set和loop sorted

b = [3384, 3404, 3344, 3394, 3374, 3414, 3354, 3404, 3384, 3424, 3364, 3414, 3394, 3364, 3434, 3374]

lst = sorted(set(b))
for i in range(4):
    print(lst[i*2:i*2+4])
 [3344, 3354, 3364, 3374] [3364, 3374, 3384, 3394] [3384, 3394, 3404, 3414] [3404, 3414, 3424, 3434] 

暂无
暂无

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

相关问题 如何根据python3中的元素序列在列表中创建子列表? - How to create sublists within a list based on a sequence of elements in python3? 如何根据开始和结束元素从列表创建子列表? - How to create sublists from list based on start and end elements? 如何将列表切成不同长度的子列表 - How to slice a list into sublists with different length 如何从另一个子列表列表的特定元素创建新的子列表列表 - How to create new list of sublists from specific elements of another list of sublists 如何在 Python 中创建子列表列表? - How to create a list of sublists in Python? Python - 如何根据字符串的一部分从字符串列表中创建子列表? - Python - How to create sublists from list of strings based on part of the string? 根据 Python 中的一组索引将列表拆分为子列表 - Split a list into sublists based on a set of indexes in Python 如何首先基于初始列表的单个元素将列表拆分为子列表,然后仅在 python 中将列表的连续部分拆分为子列表? - How to split a list into sublists based on single elements of the initial list first and then contiguous portions of the list simply in python? 根据元素的值将列表拆分为子列表? - Split a list into sublists based on the value of an element? Python 根据列表元素第一个数字的相等性将列表分成子列表 - Python list into sublists based on the equality of first digit of the list elements
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM