【问题标题】:I need to save element of list in seperated lists我需要将列表元素保存在单独的列表中
【发布时间】:2021-08-27 14:19:38
【问题描述】:

我有这个清单:

[29, 64, 65, 66, 128, 129, 130, 166, 167, 168, 184, 185, 186, 215, 216, 217, 237, 238, 239, 349, 350, 351, 443, 483, 484, 485, 495, 496, 497, 526, 527, 528, 542, 543, 544, 564, 565, 566]

我想将它们分开,如果元素与下一个元素之间的差异不同于 1,则代码会将下一个元素保存在另一个 lit 中

list1=[29]
liste2=[64, 65, 66]
liste3=[128, 129, 130]
liste3=[166, 167, 168]

直到最后

【问题讨论】:

  • 这个列表总是排序的吗?
  • @Epsi95 如果不是,您只需在开始之前对其进行排序。我能想到的唯一其他选择涉及列表列表并检查每个单独的列表以插入一个元素,即 O(n^2).
  • 我认为 O(nlong) + O(n) ~ O(nlogn) 是可能的,O(nlogn) 来自排序。我会遍历排序数组并检查差异 1 并继续进行分组。 @wjandrea
  • @Epsi95 这正是我的想法。请参阅this answer 了解 O(n) 实现,假设输入已排序。对列表进行排序(如果需要)是 O(n log n)

标签: python


【解决方案1】:

使用 numpy 可以在一行中完成:

import numpy as np
lst = [29, 64, 65, 66, 128, 129, 130, 166, 167, 168, 184, 185, 186, 215, 216, 217, 237, 238, 239, 349, 350, 351, 443, 483, 484, 485, 495, 496, 497, 526, 527, 528, 542, 543, 544, 564, 565, 566]
[x.tolist() for x in np.split(lst, np.where(np.diff(lst) > 1)[0]+1)]

输出:

[[29],
 [64, 65, 66],
 [128, 129, 130],
 [166, 167, 168],
 [184, 185, 186],
 [215, 216, 217],
 [237, 238, 239],
 [349, 350, 351],
 [443],
 [483, 484, 485],
 [495, 496, 497],
 [526, 527, 528],
 [542, 543, 544],
 [564, 565, 566]]

编辑 1:将每个列表存储到单独的变量中(不推荐

sub_lists = [x.tolist() for x in np.split(lst, np.where(np.diff(lst) > 1)[0]+1)]
for i in range(1, len(sub_lists)+1):
    globals()['list_%s' % i] = sub_lists[i-1]

输出:

print(list_1)
>> [29]

print(list_2)
>> [64, 65, 66]

注意: 不建议使用单个变量for multiple reasons,尤其是在变量数量可以根据条件爆炸的场景。

【讨论】:

  • 您好,我只是想补充您给出的答案。由于他想要的输出是将每个列表存储到一个变量中,我相信你可以使用exec() 这样做(虽然强烈不推荐,但应该给你莎拉想要的结果)for key, i in enumerate(new_list): exec(f'liste{key+1} = i') new_list 是使用代码创建的列表以上由@heretolearn 提供!干杯
  • @Owenn,感谢您的建议。我更新了代码以创建动态变量并根据拆分后的子列表数量保存列表值。
  • 我建议 OP 建议不要使用单个变量名称。有关说明,请参阅 this answer。无论哪种方式,仅将变量放在全局命名空间中可能是一个坏主意,例如,如果此代码最终将进入一个函数,那么您将使用应该是本地的 var 污染全局变量,但 locals() 是不可分配的。
  • 是的,我同意拥有单个变量的观点,特别是当我们不知道子列表的数量时,主列表可以打破它。将注释添加到我的答案中。谢谢
【解决方案2】:

我相信这应该是您正在寻找的:

sort_me = [29, 64, 65, 66, 128, 129, 130, 166, 167, 168, 184, 185, 186, 215, 216, 217, 237, 238, 239, 349, 350, 351, 443, 483, 484, 485, 495, 496, 497, 526, 527, 528, 542, 543, 544, 564, 565, 566]

sorted_lists = list()

last_num = sort_me[0]  # get the  first index of the list to be the last number that has been evaluated
current_list = [sort_me[0]]  # get first index fo list prepared for for loop

# iterate over all numbers in the list
for i in range(1, len(sort_me)):
    num = sort_me[i]  # get the next number
    if num == last_num + 1:  # check if it meets the criteria to be put into list
        current_list.append(num)  # add number to the list
    else:
        sorted_lists.append(current_list)  # add list to list of lists (say that 10 times fast)
        current_list = [num]  # start new list
    last_num = num  # save last num checked as this iteration's number

if len(current_list) > 0:
    sorted_lists.append(current_list)

# print them to show that the lists are correct (chnage this bit to do what you want with them)
for ls in sorted_lists:
    print(ls)

EDIT 添加了错过最后一个列表的行,现在应该可以正常工作了。

【讨论】:

  • 我无法获取最后一个列表,我不知道为什么?
  • @Sarah 我错过了在循环之后添加列表。上面的代码现在可以工作了,应该可以解决您的问题,而无需导入任何额外的包。
猜你喜欢
  • 2016-11-13
  • 2019-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-23
  • 1970-01-01
相关资源
最近更新 更多