如何在列表中根据条件创建子元素?

huangapple go评论67阅读模式
英文:

How to create subelements with conditions in list?

问题

Here's the translated code part you requested:

我有一个像这样的列表

a = [3, 2, 1, 2, 3, 1, 3, 1, 2]

我试图获取一个新的列表其中的元素在以下条件下分隔成子列表

1-) 将序列从1到N在这种情况下N为3分隔成子列表

2-) 即使在1到N之间有缺失的数字也将序列从1到N分隔成子列表

3-) 将其余元素分隔成子列表

预期的输出如下

b = [[3], [2], [1, 2, 3], [1, 3], [1, 2]]

我的当前尝试如下但是它错误地关联了元素感谢任何帮助

b = [None]*len(a)
for i in range(len(a)):
    if 1 not in a[i][0]:
        b[i] = [a[i]]

If you have any further questions or need assistance with this code, please let me know.

英文:

I have a list like this

a = [3, 2, 1, 2, 3, 1, 3, 1, 2]

I'm trying to get a new list with elements separated in sublist for following conditions:

1-) Separate in sublists the sequences from 1 to N (N in this case is 3)

2-) Separate in sublists the sequences from 1 to N even there are missing numbers between 1 and N

3-) Separate in sublists the rest elements

The expected output would be like this.

b = [[3], [2], [1, 2, 3], [1, 3], [1, 2]]

My current attempt is below but is associating incorrectly the elements. Thanks for any help

b = [None]*len(a)
for i in range(len(a)):
    if 1 not in a[i][0]:
        b[i] = [a[i]] 

答案1

得分: 1

以下是翻译好的代码部分:

理解简单的逻辑方式

    # 你的列表
    a = [3, 2, 1, 2, 3, 1, 3, 1, 2]
    
    # 你的列表的列表
    finalList = [] 
    
    # 临时列表
    tempList = []
    size = len(a)
    
    # 逻辑
    for i in range(len(a)):
        if (i+1 < size) and (a[i] < a[i+1]):
            tempList.append(a[i])
        else:
            tempList.append(a[i])
            finalList.append(tempList[:]) # 没有引用。
            tempList.clear()
    
    print(finalList)

注意:我已经将代码翻译成中文,但不包括“理解简单的逻辑方式”这句话,因为这句话本身不是代码的一部分。

英文:

To understand the logic in a simple way

#YOUR LIST
a = [3, 2, 1, 2, 3, 1, 3, 1, 2]

#YOUR LIST OF LISTS
finalList = [] 

#TEMPORAY LIST
tempList = []
size = len(a)

#LOGIC
for i in range(len(a)):
    if (i+1 &lt; size) and  (a[i] &lt; a[i+1]):
        tempList.append(a[i])
    else:
        tempList.append(a[i])
        finalList.append(tempList[:]) #NO REFERENCE.
        tempList.clear()

print(finalList)

答案2

得分: 0

你可以在输出中的当前项目不大于输出的最后一个子列表的最后一个项目时,添加一个新的子列表:

a = [3, 2, 1, 2, 3, 1, 3, 1, 2]
n = 3

output = []
for i in a:
    if not output or not 1 <= output[-1][-1] < i <= n:
        output.append([])
    output[-1].append(i)

output 变成了:

[[3], [2], [1, 2, 3], [1, 3], [1, 2]]

演示:https://replit.com/@blhsing/FavoriteLargeSite

英文:

You can add a new sub-list to the output when the current item is not greater than the last item of the last sub-list of the output:

a = [3, 2, 1, 2, 3, 1, 3, 1, 2]
n = 3

output = []
for i in a:
    if not output or not 1 &lt;= output[-1][-1] &lt; i &lt;= n:
        output.append([])
    output[-1].append(i)

output becomes:

[[3], [2], [1, 2, 3], [1, 3], [1, 2]]

Demo: https://replit.com/@blhsing/FavoriteLargeSite

huangapple
  • 本文由 发表于 2023年5月11日 09:08:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76223497.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定