如何在Python中对列表中的列表进行排序,以避免重复的名称排在一起

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

How to sort a list of lists in Python without having repeated names together

问题

以下是翻译好的部分:

我有以下包含姓名和年龄的列表:

[['John', 8], ['John', 8], ['John', 8], ['Mary', 7], ['Mary', 7], ['Mike', 9], ['Mike', 9]]

如何使用Python以随机方式排序它,而且不能让相同的名字彼此相邻。
例如(已排序):

[['John', 8], ['Mary', 7], ['Mike', 9], ['John', 8], ['Mike', 9], ['Mary', 7], ['John', 8]]

我的实际情况有大约250个姓名,同名的重复次数不超过50次,这意味着总是有可能的解决方案。

英文:

I have the following list with names and ages:

[['John', 8], ['John', 8], ['John', 8], ['Mary', 7], ['Mary', 7], ['Mike', 9], ['Mike', 9]]

How to sort it, with Python, in a random way and without having same name next to each other.
For example (sorted):

[['John', 8], ['Mary', 7], ['Mike', 9], ['John', 8], ['Mike', 9], ['Mary', 7], ['John', 8]]

My real case has about 250 names and same name does not repeats more than 50 times, that means that always there is a possible solution.

答案1

得分: 3

另一种解决方法(以获取随机结果)。创建一个临时字典,其中键是名称,值是原始数据中的值列表。

随机选择一个元素(其中包含最多的值)并将其放入输出列表的第一个元素中,然后从剩余值最多的列表中随机选择一个元素并将其弹出。

from random import choice

lst = [
    ["John", 8],
    ["John", 8],
    ["John", 8],
    ["Mary", 7],
    ["Mary", 7],
    ["Mike", 9],
    ["Mike", 9],
]

def get_random_max(d):
    m = max(map(len, d.values()))
    return choice([[k, v] for k, v in d.items() if len(v) == m])

tmp = {}
for name, value in lst:
    tmp.setdefault(name, []).append(value)

k, v = get_random_max(tmp)
out = [[k, v.pop()]]

while any(tmp.values()):
    k, v = get_random_max({k: v for k, v in tmp.items() if k != out[-1][0]})
    out.append([k, v.pop()])

print(out)

打印(例如):

[['John', 8], ['Mary', 7], ['Mike', 9], ['John', 8], ['Mike', 9], ['John', 8], ['Mary', 7]]

编辑:已经合并了@KellyBundy的建议。谢谢!

英文:

Another solution (to get random results). Crate a temporary dictionary where keys are names and values ale lists of values from original data.

Select one element at random (which has most values) and put it as first element in output list, then select at pop at random from lists which have the most values left.

from random import choice

lst = [
    ["John", 8],
    ["John", 8],
    ["John", 8],
    ["Mary", 7],
    ["Mary", 7],
    ["Mike", 9],
    ["Mike", 9],
]

def get_random_max(d):
    m = max(map(len, d.values()))
    return choice([[k, v] for k, v in d.items() if len(v) == m])

tmp = {}
for name, value in lst:
    tmp.setdefault(name, []).append(value)

k, v = get_random_max(tmp)
out = [[k, v.pop()]]

while any(tmp.values()):
    k, v = get_random_max({k: v for k, v in tmp.items() if k != out[-1][0]})
    out.append([k, v.pop()])

print(out)

Prints (for example):

[['John', 8], ['Mary', 7], ['Mike', 9], ['John', 8], ['Mike', 9], ['John', 8], ['Mary', 7]]

EDIT: Incorporated the suggestions from @KellyBundy. Thanks!

答案2

得分: 1

以下是代码部分的翻译:

import hashlib
import json
import random
mylist = [['John', 8], ['John', 8], ['John', 8], ['Mary', 7], ['Mary', 7], ['Mike', 9], ['Mike', 9], ['John', 8], ['John', 8], ['Lucas', 17], ['Lucas', 17], ['Lucas', 17], ['Beatriz', 14], ['Beatriz', 14], ['Gabriela', 14], ['Gabriela', 14], ['John', 8], ['Mary', 7], ['Mary', 7], ['Mike', 9], ['Mike', 9], ['Beatriz', 14], ['Beatriz', 14], ['Gabriela', 14], ['Gabriela', 14], ['John', 8], ['Mary', 7], ['Mary', 7], ['Mike', 9], ['Mike', 9], ['Mary', 7], ['Mary', 7], ['Mike', 9], ['Mike', 9], ['John', 8], ['John', 8], ['Lucas', 17], ['Marcus', 8], ['Marcus', 8], ['Marcus', 8], ['Maricotas', 7], ['Maricotas', 7], ['Merlin', 9], ['Merlin', 9], ['Marcus', 8], ['Marcus', 8], ['Beto', 17], ['Beto', 17], ['Beto', 17], ['Beatriz', 14], ['Beatriz', 14], ['Gabriela', 14], ['Gabriela', 14], ['Marcus', 8], ['Maricotas', 7], ['Maricotas', 7], ['Merlin', 9], ['Merlin', 9], ['Beatriz', 14], ['Beatriz', 14], ['Gabriela', 14], ['Gabriela', 14], ['Marcus', 8], ['Maricotas', 7], ['Maricotas', 7], ['Merlin', 9], ['Merlin', 9], ['Maricotas', 7], ['Maricotas', 7], ['Merlin', 9], ['Merlin', 9], ['Marcus', 8], ['Marcus', 8], ['Beto', 17], ['John', 8], ['John', 8], ['John', 8], ['Mary', 7], ['Mary', 7], ['Mike', 9], ['Mike', 9], ['John', 8], ['John', 8], ['Lucas', 17], ['Lucas', 17], ['Lucas', 17], ['Beatriz', 14], ['Beatriz', 14], ['Gabriela', 14], ['Gabriela', 14], ['John', 8], ['Mary', 7], ['Mary', 7], ['Mike', 9], ['Mike', 9], ['Beatriz', 14], ['Beatriz', 14], ['Gabriela', 14], ['Gabriela', 14], ['John', 8], ['Mary', 7], ['Mary', 7], ['Mike', 9], ['Mike', 9], ['Mary', 7], ['Mary', 7], ['Mike', 9], ['Mike', 9], ['John', 8], ['John', 8], ['Lucas', 17], ['Marcus', 8], ['Marcus', 8], ['Marcus', 8], ['Maricotas', 7], ['Maricotas', 7], ['Merlin', 9], ['Merlin', 9], ['Marcus', 8], ['Marcus', 8], ['Beto', 17], ['Beto', 17], ['Beto', 17], ['Beatriz', 14], ['Beatriz', 14], ['Gabriela', 14], ['Gabriela', 14], ['Marcus', 8], ['Maricotas', 7], ['Maricotas', 7], ['Merlin', 9], ['Merlin', 9], ['Beatriz', 14], ['Beatriz', 14], ['Gabriela', 14], ['Gabriela', 14], ['Marcus', 8], ['Maricotas', 7], ['Maricotas', 7], ['Merlin', 9], ['Merlin', 9], ['Maricotas', 7], ['Maricotas', 7], ['Merlin', 9], ['Merlin', 9], ['Marcus', 8], ['Marcus', 8], ['Beto', 17]]
mylen = len(mylist)
print('Old list has: ' + str(mylen))
print(mylist)
data_md5 = hashlib.md5(json.dumps(mylist, sort_keys=True).encode('utf-8')).hexdigest()
print(data_md5)
previtem = ['', '']
endlist = []
while mylen > 1:
    myrand = random.randrange(0, mylen - 1)
    selected = mylist[myrand]
    if selected != previtem:
        tmpitem = mylist.pop(myrand)
        mylen = len(mylist)
        endlist.append(selected)
        previtem = selected
print('-----')
print('--Looking where to insert the last item--')
for i in range(1, len(endlist)):
    if (endlist[i - 1] != mylist) and (endlist[i] != mylist):
        break
endlist.insert(i, mylist)
print('New list has: ' + str(len(endlist)))
print(endlist)
data_md5 = hashlib.md5(json.dumps(endlist, sort_keys=True).encode('utf-8')).hexdigest()
print(data_md5)

请注意,代码中的特殊字符和HTML实体已经被解析并恢复为原始字符。如果您有任何进一步的问题或需要更多帮助,请告诉我。

英文:

Simple example with 296 names and without complicated code.
Be aware that this code will run forever if you provide unresolvable list, ie. if you lied when you said the list does not contain lots of repetitions of the same names.
For your needs, it should work.

Thank you.

import hashlib
import json
import random
mylist=[['John', 8], ['John', 8], ['John', 8], ['Mary', 7], ['Mary', 7], ['Mike', 9], ['Mike', 9],['John', 8], ['John', 8], ['Lucas', 17], ['Lucas', 17], ['Lucas', 17], ['Beatriz', 14], ['Beatriz', 14], ['Gabriela', 14], ['Gabriela', 14], ['John', 8], ['Mary', 7], ['Mary', 7], ['Mike', 9], ['Mike', 9], ['Beatriz', 14], ['Beatriz', 14], ['Gabriela', 14], ['Gabriela', 14], ['John', 8], ['Mary', 7], ['Mary', 7], ['Mike', 9], ['Mike', 9], ['Mary', 7], ['Mary', 7], ['Mike', 9], ['Mike', 9],['John', 8], ['John', 8], ['Lucas', 17],['Marcus', 8], ['Marcus', 8], ['Marcus', 8], ['Maricotas', 7], ['Maricotas', 7], ['Merlin', 9], ['Merlin', 9],['Marcus', 8], ['Marcus', 8], ['Beto', 17], ['Beto', 17], ['Beto', 17], ['Beatriz', 14], ['Beatriz', 14], ['Gabriela', 14], ['Gabriela', 14], ['Marcus', 8], ['Maricotas', 7], ['Maricotas', 7], ['Merlin', 9], ['Merlin', 9], ['Beatriz', 14], ['Beatriz', 14], ['Gabriela', 14], ['Gabriela', 14], ['Marcus', 8], ['Maricotas', 7], ['Maricotas', 7], ['Merlin', 9], ['Merlin', 9], ['Maricotas', 7], ['Maricotas', 7], ['Merlin', 9], ['Merlin', 9],['Marcus', 8], ['Marcus', 8], ['Beto', 17], ['John', 8], ['John', 8], ['John', 8], ['Mary', 7], ['Mary', 7], ['Mike', 9], ['Mike', 9],['John', 8], ['John', 8], ['Lucas', 17], ['Lucas', 17], ['Lucas', 17], ['Beatriz', 14], ['Beatriz', 14], ['Gabriela', 14], ['Gabriela', 14], ['John', 8], ['Mary', 7], ['Mary', 7], ['Mike', 9], ['Mike', 9], ['Beatriz', 14], ['Beatriz', 14], ['Gabriela', 14], ['Gabriela', 14], ['John', 8], ['Mary', 7], ['Mary', 7], ['Mike', 9], ['Mike', 9], ['Mary', 7], ['Mary', 7], ['Mike', 9], ['Mike', 9],['John', 8], ['John', 8], ['Lucas', 17],['Marcus', 8], ['Marcus', 8], ['Marcus', 8], ['Maricotas', 7], ['Maricotas', 7], ['Merlin', 9], ['Merlin', 9],['Marcus', 8], ['Marcus', 8], ['Beto', 17], ['Beto', 17], ['Beto', 17], ['Beatriz', 14], ['Beatriz', 14], ['Gabriela', 14], ['Gabriela', 14], ['Marcus', 8], ['Maricotas', 7], ['Maricotas', 7], ['Merlin', 9], ['Merlin', 9], ['Beatriz', 14], ['Beatriz', 14], ['Gabriela', 14], ['Gabriela', 14], ['Marcus', 8], ['Maricotas', 7], ['Maricotas', 7], ['Merlin', 9], ['Merlin', 9], ['Maricotas', 7], ['Maricotas', 7], ['Merlin', 9], ['Merlin', 9],['Marcus', 8], ['Marcus', 8], ['Beto', 17], ['John', 8], ['John', 8], ['John', 8], ['Mary', 7], ['Mary', 7], ['Mike', 9], ['Mike', 9],['John', 8], ['John', 8], ['Lucas', 17], ['Lucas', 17], ['Lucas', 17], ['Beatriz', 14], ['Beatriz', 14], ['Gabriela', 14], ['Gabriela', 14], ['John', 8], ['Mary', 7], ['Mary', 7], ['Mike', 9], ['Mike', 9], ['Beatriz', 14], ['Beatriz', 14], ['Gabriela', 14], ['Gabriela', 14], ['John', 8], ['Mary', 7], ['Mary', 7], ['Mike', 9], ['Mike', 9], ['Mary', 7], ['Mary', 7], ['Mike', 9], ['Mike', 9],['John', 8], ['John', 8], ['Lucas', 17],['Marcus', 8], ['Marcus', 8], ['Marcus', 8], ['Maricotas', 7], ['Maricotas', 7], ['Merlin', 9], ['Merlin', 9],['Marcus', 8], ['Marcus', 8], ['Beto', 17], ['Beto', 17], ['Beto', 17], ['Beatriz', 14], ['Beatriz', 14], ['Gabriela', 14], ['Gabriela', 14], ['Marcus', 8], ['Maricotas', 7], ['Maricotas', 7], ['Merlin', 9], ['Merlin', 9], ['Beatriz', 14], ['Beatriz', 14], ['Gabriela', 14], ['Gabriela', 14], ['Marcus', 8], ['Maricotas', 7], ['Maricotas', 7], ['Merlin', 9], ['Merlin', 9], ['Maricotas', 7], ['Maricotas', 7], ['Merlin', 9], ['Merlin', 9],['Marcus', 8], ['Marcus', 8], ['Beto', 17], ['John', 8], ['John', 8], ['John', 8], ['Mary', 7], ['Mary', 7], ['Mike', 9], ['Mike', 9],['John', 8], ['John', 8], ['Lucas', 17], ['Lucas', 17], ['Lucas', 17], ['Beatriz', 14], ['Beatriz', 14], ['Gabriela', 14], ['Gabriela', 14], ['John', 8], ['Mary', 7], ['Mary', 7], ['Mike', 9], ['Mike', 9], ['Beatriz', 14], ['Beatriz', 14], ['Gabriela', 14], ['Gabriela', 14], ['John', 8], ['Mary', 7], ['Mary', 7], ['Mike', 9], ['Mike', 9], ['Mary', 7], ['Mary', 7], ['Mike', 9], ['Mike', 9],['John', 8], ['John', 8], ['Lucas', 17],['Marcus', 8], ['Marcus', 8], ['Marcus', 8], ['Maricotas', 7], ['Maricotas', 7], ['Merlin', 9], ['Merlin', 9],['Marcus', 8], ['Marcus', 8], ['Beto', 17], ['Beto', 17], ['Beto', 17], ['Beatriz', 14], ['Beatriz', 14], ['Gabriela', 14], ['Gabriela', 14], ['Marcus', 8], ['Maricotas', 7], ['Maricotas', 7], ['Merlin', 9], ['Merlin', 9], ['Beatriz', 14], ['Beatriz', 14], ['Gabriela', 14], ['Gabriela', 14], ['Marcus', 8], ['Maricotas', 7], ['Maricotas', 7], ['Merlin', 9], ['Merlin', 9], ['Maricotas', 7], ['Maricotas', 7], ['Merlin', 9], ['Merlin', 9],['Marcus', 8], ['Marcus', 8], ['Beto', 17]]
mylen=len(mylist)
print('Old list has:' + str(mylen))
print(mylist)
data_md5 = hashlib.md5(json.dumps(mylist, sort_keys=True).encode('utf-8')).hexdigest()
print(data_md5)
previtem=['','']
endlist=[]
while mylen > 1:
myrand=random.randrange(0, mylen-1)
selected=mylist[myrand]
if selected!=previtem:
tmpitem=mylist.pop(myrand)
mylen=len(mylist)
endlist.append(selected)
previtem=selected
print('-----')
print('--Looking where to insert last item--')
for i in range(1,len(endlist)):
if (endlist[i-1]!=mylist) and (endlist[i]!=mylist):
break
endlist.insert(i, mylist)
print('New list has:' + str(len(endlist)))
print(endlist)
data_md5 = hashlib.md5(json.dumps(endlist, sort_keys=True).encode('utf-8')).hexdigest()
print(data_md5)

如何在Python中对列表中的列表进行排序,以避免重复的名称排在一起

答案3

得分: 0

谢谢您提供的所有答案和建议。

不幸的是,我无法使任何提出的解决方案适用于我的实际数据,所以我不得不开发一种解决方案,您可以在以下链接中查看并评论:

https://stackoverflow.com/questions/75534975/python-sorting-a-matrix-of-data-list-of-lists-to-have-equal-names-at-the-bi

英文:

Thank you for all the answers and suggestions.

Unfortunately I could not adapt any of the proposed solutions to work with my real data, and I had to develop a solution, that you can see and comment here:

https://stackoverflow.com/questions/75534975/python-sorting-a-matrix-of-data-list-of-lists-to-have-equal-names-at-the-bi

huangapple
  • 本文由 发表于 2023年2月14日 07:42:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75442192.html
匿名

发表评论

匿名网友

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

确定