在Python中,当相同的值出现时,将列表拆分为子列表的操作如下:

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

Split list in python when same values occurs into a list of sublists

问题

使用Python,我需要将my_list = ['1', '2', '2', '3', '3', '3', '4', '4', '5']拆分成一个避免相同值的子列表列表。正确的输出是[['1', '2', '3', '4', '5'], ['2', '3', '4'], ['3']]。

英文:

Using python, I need to split my_list = ['1','2','2','3','3','3','4','4','5'] into a list with sublists that avoid the same value. Correct output = [['1','2','3','4','5'],['2','3','4'],['3']]

答案1

得分: 3

这个代码的功能是将一个包含重复元素的列表 my_list 转换为一个包含子列表的列表 output,每个子列表包含独一无二的元素。以下是翻译后的代码部分和输出:

  1. 也许不是最高效的方法但仍然有效
  2. my_list = ['1', '2', '2', '3', '3', '3', '4', '4', '5']
  3. output = []
  4. for e in my_list:
  5. for f in output:
  6. if not e in f:
  7. f.append(e)
  8. break
  9. else:
  10. output.append([e])
  11. print(output)
  12. **输出**
  13. [['1', '2', '3', '4', '5'], ['2', '3', '4'], ['3']]
英文:

Probably not the most efficient approach but effective nonetheless:

  1. my_list = ['1','2','2','3','3','3','4','4','5']
  2. output = []
  3. for e in my_list:
  4. for f in output:
  5. if not e in f:
  6. f.append(e)
  7. break
  8. else:
  9. output.append([e])
  10. print(output)

Output:

  1. [['1', '2', '3', '4', '5'], ['2', '3', '4'], ['3']]

答案2

得分: 2

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

  1. 我假设您正在对每个唯一元素及其出现次数进行索引并将结果列表排序以更好地满足您的期望输出
  2. uniques = list(set(my_list))
  3. uniques.sort()
  4. unique_counts = {unique:my_list.count(unique) for unique in uniques}
  5. new_list = []
  6. for _ in range(max(unique_counts.values())):
  7. new_list.append([])
  8. for unique, count in unique_counts.items():
  9. for i in range(count):
  10. new_list[i].append(unique)
  11. new_list的输出为
  12. [['1', '2', '3', '4', '5'], ['2', '3', '4'], ['3']]
英文:

I assumed you are indexing every unique element with its occurrence and also sorted the result list to better suit your desired output.

  1. uniques = list(set(my_list))
  2. uniques.sort()
  3. unique_counts = {unique:my_list.count(unique) for unique in uniques}
  4. new_list = []
  5. for _ in range(max(unique_counts.values())):
  6. new_list.append([])
  7. for unique,count in unique_counts.items():
  8. for i in range(count):
  9. new_list[i].append(unique)

The output for new_list is

  1. [['1', '2', '3', '4', '5'], ['2', '3', '4'], ['3']]

答案3

得分: 2

使用 collections.Counter 来识别所需子列表的最大数量,然后根据它们的频率在子列表上分配连续的唯一键:

  1. from collections import Counter
  2. my_list = ['1','2','2','3','3','3','4','4','5']
  3. cnts = Counter(my_list)
  4. res = [[] for i in range(cnts.most_common(1).pop()[1])]
  5. for k in cnts.keys():
  6. for j in range(cnts[k]):
  7. res[j].append(k)
  8. print(res)

输出结果:

  1. [['1', '2', '3', '4', '5'], ['2', '3', '4'], ['3']]
英文:

By using collections.Counter for recognizing the maximum number of the needed sublists and then distributing consecutive unique keys on sublists according to their frequencies:

  1. from collections import Counter
  2. my_list = ['1','2','2','3','3','3','4','4','5']
  3. cnts = Counter(my_list)
  4. res = [[] for i in range(cnts.most_common(1).pop()[1])]
  5. for k in cnts.keys():
  6. for j in range(cnts[k]):
  7. res[j].append(k)
  8. print(res)

  1. [['1', '2', '3', '4', '5'], ['2', '3', '4'], ['3']]

答案4

得分: 0

这是一个基于列表推导的方法,用于获取唯一值和它们的计数。

  1. my_list = ['1','2','2','3','3','3','4','4','5']
  2. unique = [val for i, val in enumerate(my_list) if val not in my_list[0:i]]
  3. counts = [my_list.count(val) for val in unique]
  4. output = [[val for val, ct in zip(unique, counts) if ct > i] for i in range(max(counts))]

希望这对你有帮助。

英文:

Here's a way to do it based on getting unique values and counts using list comprehension.

  1. my_list = ['1','2','2','3','3','3','4','4','5']
  2. unique = [val for i,val in enumerate(my_list) if val not in my_list[0:i]]
  3. counts = [my_list.count(val) for val in unique]
  4. output = [[val for val,ct in zip(unique, counts) if ct > i] for i in range(max(counts))]

huangapple
  • 本文由 发表于 2023年2月18日 00:27:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75486868.html
匿名

发表评论

匿名网友

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

确定