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

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

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,每个子列表包含独一无二的元素。以下是翻译后的代码部分和输出:

也许不是最高效的方法但仍然有效

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

**输出**

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

Probably not the most efficient approach but effective nonetheless:

my_list = ['1','2','2','3','3','3','4','4','5']
output = []

for e in my_list:
    for f in output:
        if not e in f:
            f.append(e)
            break
    else:
        output.append([e])

print(output)

Output:

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

答案2

得分: 2

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

我假设您正在对每个唯一元素及其出现次数进行索引并将结果列表排序以更好地满足您的期望输出

uniques = list(set(my_list))
uniques.sort()

unique_counts = {unique:my_list.count(unique) for unique in uniques}
new_list = []
for _ in range(max(unique_counts.values())):
    new_list.append([])
for unique, count in unique_counts.items():
    for i in range(count):
        new_list[i].append(unique)

new_list的输出为

[['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.

uniques = list(set(my_list))
uniques.sort()

unique_counts = {unique:my_list.count(unique) for unique in uniques}
new_list = []
for _ in range(max(unique_counts.values())):
    new_list.append([])
for unique,count in unique_counts.items():
    for i in range(count):
        new_list[i].append(unique)

The output for new_list is

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

答案3

得分: 2

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

from collections import Counter

my_list = ['1','2','2','3','3','3','4','4','5']
cnts = Counter(my_list)
res = [[] for i in range(cnts.most_common(1).pop()[1])]

for k in cnts.keys():
    for j in range(cnts[k]):
        res[j].append(k)
print(res)

输出结果:

[['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:

from collections import Counter

my_list = ['1','2','2','3','3','3','4','4','5']
cnts = Counter(my_list)
res = [[] for i in range(cnts.most_common(1).pop()[1])]

for k in cnts.keys():
    for j in range(cnts[k]):
        res[j].append(k)
print(res)

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

答案4

得分: 0

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

my_list = ['1','2','2','3','3','3','4','4','5']

unique = [val for i, val in enumerate(my_list) if val not in my_list[0:i]]
counts = [my_list.count(val) for val in unique]

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.

my_list = ['1','2','2','3','3','3','4','4','5']

unique = [val for i,val in enumerate(my_list) if val not in my_list[0:i]]
counts = [my_list.count(val) for val in unique]

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:

确定