Home > Blockchain >  How to get combinations of list of names sorted in an alphabetical order?
How to get combinations of list of names sorted in an alphabetical order?

Time:08-03

I have a list of names as:

names_list = ['Acosta Jose','Bailey Tyler','Barbosa Roberto','Calabro James','Carasone Allison','Adams Zane','Brencher Tim','Chacon Ramon','Arce Manuel']

I would like to get a combinations of names per each alphabet letter.

For example: A letter names are: 'Acosta Jose','Adams Zane','Arce Manuel'

Here required combinations are:

Acosta Jose Adams Zane
Acosta Jose Arce Manuel
Adams Zane Arce Manuel

Final Required output for given name list is:

[['Acosta Jose, Adams Zane',
  'Acosta Jose,Arce Manuel',
  'Adams Zane, Arce Manuel'],
 ['Bailey Tyler, Barbosa RobertoBailey Tyler, Brencher TimBarbosa Roberto, '
  'Brencher Tim'],
 ['Calabro James, Carasone AllisonCalabro James, Chacon RamonCarasone Allison '
  ',Chacon Ramon']]

CodePudding user response:

The output shown in the question is rather confusing. However, the description seems to indicate that this is what's required:

from itertools import combinations

names_list = ['Acosta Jose','Bailey Tyler','Barbosa Roberto','Calabro James','Carasone Allison','Adams Zane','Brencher Tim','Chacon Ramon','Arce Manuel']

ds = {}

for name in sorted(names_list):
    ds.setdefault(name[0], []).append(name)

output = ([[f'{a}, {b}' for a, b in combinations(e, 2)] for e in ds.values()])

print(output)

Output:

[['Acosta Jose, Adams Zane', 'Acosta Jose, Arce Manuel', 'Adams Zane, Arce Manuel'], ['Bailey Tyler, Barbosa Roberto', 'Bailey Tyler, Brencher Tim', 'Barbosa Roberto, Brencher Tim'], ['Calabro James, Carasone Allison', 'Calabro James, Chacon Ramon', 'Carasone Allison, Chacon Ramon']]

CodePudding user response:

Try:

import itertools
named_list = ['Acosta Jose','Adams Zane','Arce Manuel']
print(sorted(list(map(', '.join, itertools.permutations(named_list, 2)))))

Result:

['Acosta Jose, Adams Zane',
 'Acosta Jose, Arce Manuel',
 'Adams Zane, Acosta Jose',
 'Adams Zane, Arce Manuel',
 'Arce Manuel, Acosta Jose',
 'Arce Manuel, Adams Zane']

CodePudding user response:

This is my take on it:

import string, itertools

names_list = ['Acosta Jose','Bailey Tyler','Barbosa Roberto','Calabro James','Carasone Allison','Adams Zane','Brencher Tim','Chacon Ramon','Arce Manuel']
names_sorted = []
result = []

for a in string.ascii_uppercase:
    temp = []
    for name in names_list:
        if name.startswith(a):
            temp.append(name)
    if temp:
        names_sorted.append(temp)

for name in names_sorted:
    result.append(sorted(list(map(', '.join, itertools.combinations(name, 2)))))

print(result)

Result:

[
    ['Acosta Jose, Adams Zane', 'Acosta Jose, Arce Manuel', 'Adams Zane, Arce Manuel'],
    ['Bailey Tyler, Barbosa Roberto', 'Bailey Tyler, Brencher Tim', 'Barbosa Roberto, Brencher Tim'],
    ['Calabro James, Carasone Allison', 'Calabro James, Chacon Ramon', 'Carasone Allison, Chacon Ramon']
]

Edit: changed permutations to combinations as per Stuarts comment.

CodePudding user response:

print(sorted(names_list))

You can use sorted function for order by alphabetically.

CodePudding user response:

References:

Code:

import itertools
import collections

names_list = ['Acosta Jose','Bailey Tyler','Barbosa Roberto','Calabro James','Carasone Allison','Adams Zane','Brencher Tim','Chacon Ramon','Arce Manuel']

twonames_list = sorted([', '.join(twonames) for twonames in itertools.permutations(names_list, 2)])

twonames_list_dict = collections.defaultdict(list)
for twonames in twonames_list:
    twonames_list_dict[twonames[0]].append(twonames)

twonames_list_list = [v for k, v in twonames_list_dict.items()]

Result:

[['Acosta Jose, Adams Zane',
  'Acosta Jose, Arce Manuel',
  'Acosta Jose, Bailey Tyler',
  'Acosta Jose, Barbosa Roberto',
  'Acosta Jose, Brencher Tim',
  'Acosta Jose, Calabro James',
  'Acosta Jose, Carasone Allison',
  'Acosta Jose, Chacon Ramon',
  'Adams Zane, Acosta Jose',
  'Adams Zane, Arce Manuel',
  'Adams Zane, Bailey Tyler',
  'Adams Zane, Barbosa Roberto',
  'Adams Zane, Brencher Tim',
  'Adams Zane, Calabro James',
  'Adams Zane, Carasone Allison',
  'Adams Zane, Chacon Ramon',
  'Arce Manuel, Acosta Jose',
  'Arce Manuel, Adams Zane',
  'Arce Manuel, Bailey Tyler',
  'Arce Manuel, Barbosa Roberto',
  'Arce Manuel, Brencher Tim',
  'Arce Manuel, Calabro James',
  'Arce Manuel, Carasone Allison',
  'Arce Manuel, Chacon Ramon'],
 ['Bailey Tyler, Acosta Jose',
  'Bailey Tyler, Adams Zane',
  'Bailey Tyler, Arce Manuel',
  'Bailey Tyler, Barbosa Roberto',
  'Bailey Tyler, Brencher Tim',
  'Bailey Tyler, Calabro James',
  'Bailey Tyler, Carasone Allison',
  'Bailey Tyler, Chacon Ramon',
...
  • Related