【问题标题】:separating a list into two separate list in python在python中将一个列表分成两个单独的列表
【发布时间】:2021-01-01 11:41:15
【问题描述】:

这是我的清单

lis = [['For the first', '$7.56'], ['For the next', '$16.69'],
['For the first', '$18.34'], ['For the next', '$43.47']]

这里总会有2个"for the first"和N次"for the next" 例如;-

lis = [['For the first', '$7.56'], ['For the next', '$16.69'],['for the next','$3.4'],['for the next','$2'],['For the first', '$18.34'], ['For the next', '$43.47'],[for the next,'$34']

我想将列表分成 2 个。

1:从第一个“为第一个”到第二个“为第一个”(不包括)
2:从第二个“第一个”(包括)到最后一个元素

delivery=[]
supply=[]

for j in range(1,len(lis)):
    
    if(lis[j][0].lower()=='for the first'):
        
        break
    else:
         delivery.append(lis[j][1])

print(delivery)

【问题讨论】:

  • 请更新您的问题,说明问题所在。
  • 显示预期输出...

标签: python python-3.x list loops for-loop


【解决方案1】:

如果可以保证列表中有两个项目,索引为 0 的 'for the first',会这样做:

lst = [['For the first', '$7.56'], ['For the next', '$16.69'],
['For the first', '$18.34'], ['For the next', '$43.47']]

lst_first = []
for i, item in enumerate(lst):
    if item[0].lower() == 'for the first':
        next_occurence = next(j for j, item in enumerate(lst[i+1:], 1) if item[0].lower()=='for the first')
        lst_first = lst[i+1:next_occurence]
        lst_other = lst[next_occurence:]
        break

产生:

print(lst_first)  # [['For the next', '$16.69']]
print(lst_other)  # [['For the first', '$18.34'], ['For the next', '$43.47']]

说明

它的工作方式是它使用一个简单的for 循环,就像在您的代码中一样查找'for the first' 的第一个实例,然后next 查找第二个实例。使用next 进行的第二次搜索不会搜索整个列表,而是搜索从第一个实例之后的元素开始的部分。当找到这两个实例时,结果列表将组装为第一个实例的切片。之后,我们将break 退出for 循环。

附言

您是否需要lower() 电话,我将由您决定。请注意,如果您的输入始终一致,则它是多余的。

【讨论】:

  • 谢谢你成功了,你能解释一下你做了什么
  • 我更新了答案,对它的措辞做了一些解释。从你的代码来看,你现在应该可以得到它。但是,我建议您阅读一下如何在 Python 中循环列表(您不必像以前那样使用索引)以及 next 的工作原理。
  • 在 1st_first 列表中还有什么方法可以附加 perm_list[0]
  • 有一种方法可以做任何事情。你只需要具体说明你想要什么。我不确定perm_list 是什么,但您可以尝试使用切片,例如(lst_first = lst[i:next_occurence],而不是lst_first = lst[i+1:next_occurence])。删除 +1 将使其具有包容性。
【解决方案2】:

您需要计算您看到"For the first" 的次数,并根据具体情况将值保存在第一个或第二个列表中:

def split_list(values):
    first_half, second_half = [], []
    first_counter = 0

    for name, amount in values:
        if name == "For the first":
            first_counter += 1
        if first_counter == 1:
            first_half.append([name, amount])
        elif first_counter == 2:
            second_half.append([name, amount])

    return first_half, second_half

使用 dict,您可以简化这一点,key 是您看到该模式的次数,而该值就是列表中的值。

第一次看到后返回,第二次看到后返回

def split_list(values):
    result = defaultdict(list)
    first_counter = 0
    for name, amount in values:
        if name == "For the first":
            first_counter += 1
        result[first_counter].append([name, amount])
    return result.get(1, []), result.get(2, [])

Code Demo


如果您希望新列表仅包含美元金额,请将 .append([name, amount]) 替换为 .append(amount)

【讨论】:

  • 如果我正确理解了 OP 的要求,则此代码不会产生预期的结果。
  • 谢谢你的时间,我得到了我想要的东西
猜你喜欢
  • 2018-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-05
  • 2013-04-15
  • 1970-01-01
  • 2021-08-06
  • 2023-02-03
相关资源
最近更新 更多