如何将两个列表的所有组合拆分N次?

时间:2019-01-14 02:51:07

标签: python python-2.7

请原谅问题的标题。我想不出更好的措辞了。无论如何,我有一个Python程序,该程序接受名字列表和姓氏列表。然后,将两者“拆分”为所有组合,最多可获得5个结果。该代码对其进行了更好的解释:

def getLists(firstNames, lastNames, resultCount):
    firstNameCount = len(firstNames)
    lastNameCount = len(lastNames)

    if firstNameCount >= resultCount:
        # Return the last name list as is.
        return [firstNames[i * firstNameCount / resultCount:(i + 1) * firstNameCount / resultCount] for i in xrange(resultCount)], [lastNames for _ in xrange(resultCount)]

    else:
        lastNameLists = []
        firstNameLists = [[firstNames[i % firstNameCount]] for i in xrange(resultCount)]

        for i in xrange(resultCount):
            firstName = firstNameLists[i]
            total = firstNameLists.count(firstName)
            already = total - firstNameLists[i:].count(firstName)
            lastNameLists.append(lastNames[already * lastNameCount / total:(already + 1) * lastNameCount / total])

        # Return the last names, split depending on how many results and first names.
        return firstNameLists, lastNameLists

如果名字少于最大值(5),则重复名字,直到最大值和姓氏分配在名字之间:

firstNames = ['firstName{}'.format(i) for i in xrange(1, 3)]
lastNames = ['lastName{}'.format(i) for i in xrange(1, 8)]
comboCount = len(firstNames) * len(lastNames)
resultCount = min(comboCount, 5)
firstNameLists, lastNameLists = getLists(firstNames, lastNames, resultCount)

for i in xrange(resultCount):
    print 'Result #{}:\nFirst Names: {}\nLast Names: {}'.format(i + 1, firstNameLists[i], lastNameLists[i])
    print

输出:

Result #1:
First Names: ['firstName1']
Last Names: ['lastName1', 'lastName2']

Result #2:
First Names: ['firstName2']
Last Names: ['lastName1', 'lastName2', 'lastName3']

Result #3:
First Names: ['firstName1']
Last Names: ['lastName3', 'lastName4']

Result #4:
First Names: ['firstName2']
Last Names: ['lastName4', 'lastName5', 'lastName6', 'lastName7']

Result #5:
First Names: ['firstName1']
Last Names: ['lastName5', 'lastName6', 'lastName7']

如果名字多于最大值(5),则名字将尽可能平均地分配,并为每个名字复制姓氏:

firstNames = ['firstName{}'.format(i) for i in xrange(1, 8)]
lastNames = ['lastName{}'.format(i) for i in xrange(1, 8)]
comboCount = len(firstNames) * len(lastNames)
resultCount = min(comboCount, 5)
firstNameLists, lastNameLists = getLists(firstNames, lastNames, resultCount)

for i in xrange(resultCount):
    print 'Result #{}:\nFirst Names: {}\nLast Names: {}'.format(i + 1, firstNameLists[i], lastNameLists[i])
    print

输出:

Result #1:
First Names: ['firstName1']
Last Names: ['lastName1', 'lastName2', 'lastName3', 'lastName4', 'lastName5', 'lastName6', 'lastName7']

Result #2:
First Names: ['firstName2']
Last Names: ['lastName1', 'lastName2', 'lastName3', 'lastName4', 'lastName5', 'lastName6', 'lastName7']

Result #3:
First Names: ['firstName3', 'firstName4']
Last Names: ['lastName1', 'lastName2', 'lastName3', 'lastName4', 'lastName5', 'lastName6', 'lastName7']

Result #4:
First Names: ['firstName5']
Last Names: ['lastName1', 'lastName2', 'lastName3', 'lastName4', 'lastName5', 'lastName6', 'lastName7']

Result #5:
First Names: ['firstName6', 'firstName7']
Last Names: ['lastName1', 'lastName2', 'lastName3', 'lastName4', 'lastName5', 'lastName6', 'lastName7']

如何使getLists更加高效,可读性和/或更简洁?

1 个答案:

答案 0 :(得分:0)

该示例为Python3,如果您使用Python2,请尝试进行一些可能的代码更改:

firstNames = ['firstName{}'.format(i) for i in range(1, 3)]
lastNames = ['lastName{}'.format(i) for i in range(1, 2)]

from itertools import product, islice

print(list(islice(product(firstNames, lastNames), 5)))

islice确保返回前5个结果,如果结果小于5,则仍然可以。