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

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

How to create subelements with conditions in list?

问题

Here's the translated code part you requested:

  1. 我有一个像这样的列表
  2. a = [3, 2, 1, 2, 3, 1, 3, 1, 2]
  3. 我试图获取一个新的列表其中的元素在以下条件下分隔成子列表
  4. 1-) 将序列从1N在这种情况下N3分隔成子列表
  5. 2-) 即使在1N之间有缺失的数字也将序列从1N分隔成子列表
  6. 3-) 将其余元素分隔成子列表
  7. 预期的输出如下
  8. b = [[3], [2], [1, 2, 3], [1, 3], [1, 2]]
  9. 我的当前尝试如下但是它错误地关联了元素感谢任何帮助
  10. b = [None]*len(a)
  11. for i in range(len(a)):
  12. if 1 not in a[i][0]:
  13. 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

  1. 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.

  1. 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

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

答案1

得分: 1

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

  1. 理解简单的逻辑方式
  2. # 你的列表
  3. a = [3, 2, 1, 2, 3, 1, 3, 1, 2]
  4. # 你的列表的列表
  5. finalList = []
  6. # 临时列表
  7. tempList = []
  8. size = len(a)
  9. # 逻辑
  10. for i in range(len(a)):
  11. if (i+1 < size) and (a[i] < a[i+1]):
  12. tempList.append(a[i])
  13. else:
  14. tempList.append(a[i])
  15. finalList.append(tempList[:]) # 没有引用。
  16. tempList.clear()
  17. print(finalList)

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

英文:

To understand the logic in a simple way

  1. #YOUR LIST
  2. a = [3, 2, 1, 2, 3, 1, 3, 1, 2]
  3. #YOUR LIST OF LISTS
  4. finalList = []
  5. #TEMPORAY LIST
  6. tempList = []
  7. size = len(a)
  8. #LOGIC
  9. for i in range(len(a)):
  10. if (i+1 &lt; size) and (a[i] &lt; a[i+1]):
  11. tempList.append(a[i])
  12. else:
  13. tempList.append(a[i])
  14. finalList.append(tempList[:]) #NO REFERENCE.
  15. tempList.clear()
  16. print(finalList)

答案2

得分: 0

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

  1. a = [3, 2, 1, 2, 3, 1, 3, 1, 2]
  2. n = 3
  3. output = []
  4. for i in a:
  5. if not output or not 1 <= output[-1][-1] < i <= n:
  6. output.append([])
  7. output[-1].append(i)

output 变成了:

  1. [[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:

  1. a = [3, 2, 1, 2, 3, 1, 3, 1, 2]
  2. n = 3
  3. output = []
  4. for i in a:
  5. if not output or not 1 &lt;= output[-1][-1] &lt; i &lt;= n:
  6. output.append([])
  7. output[-1].append(i)

output becomes:

  1. [[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:

确定