-2

I have list of strings shown below. I want to combine each string with every other string to give unique combination.

listTags = [ "TOT" , "WBA", "BUR", "SOU", "HUL", "SUN", "STK", "SWA", "ARS", "CHE", "MUN", "WHU", "WAT", "MID"
  "WHU", "CRY", "LEI", "EVE", "MCI", "LIV"].

So for above list it would be #TOTWBA, #TOTBUR, #TOTSOU...etc. How can I do this in python?

SparkAndShine
  • 17,001
  • 22
  • 90
  • 134
shanky
  • 751
  • 1
  • 16
  • 46

4 Answers4

3
import itertools

listTags = [ "TOT" , "WBA", "BUR", "SOU", "HUL", "SUN", "STK", "SWA", "ARS", "CHE", "MUN", "WHU", "WAT", "MID", "WHU", "CRY", "LEI", "EVE", "MCI", "LIV"]

combinations = ['#' + x + y for x, y in itertools.combinations(listTags, 2)]

print(combinations)

Output

['#TOTWBA', '#TOTBUR', '#TOTSOU', '#TOTHUL', '#TOTSUN', '#TOTSTK', '#TOTSWA', '#TOTARS', '#TOTCHE', '#TOTMUN', '#TOTWHU', '#TOTWAT', '#TOTMID', '#TOTWHU', '#TOTCRY', '#TOTLEI', '#TOTEVE', '#TOTMCI', '#TOTLIV', '#WBABUR', '#WBASOU', '#WBAHUL', '#WBASUN', '#WBASTK', '#WBASWA', '#WBAARS', '#WBACHE', '#WBAMUN', '#WBAWHU', '#WBAWAT', '#WBAMID', '#WBAWHU', '#WBACRY', '#WBALEI', '#WBAEVE', '#WBAMCI', '#WBALIV', '#BURSOU', '#BURHUL', '#BURSUN', '#BURSTK', '#BURSWA', '#BURARS', '#BURCHE', '#BURMUN', '#BURWHU', '#BURWAT', '#BURMID', '#BURWHU', '#BURCRY', '#BURLEI', '#BUREVE', '#BURMCI', '#BURLIV', '#SOUHUL', '#SOUSUN', '#SOUSTK', '#SOUSWA', '#SOUARS', '#SOUCHE', '#SOUMUN', '#SOUWHU', '#SOUWAT', '#SOUMID', '#SOUWHU', '#SOUCRY', '#SOULEI', '#SOUEVE', '#SOUMCI', '#SOULIV', '#HULSUN', '#HULSTK', '#HULSWA', '#HULARS', '#HULCHE', '#HULMUN', '#HULWHU', '#HULWAT', '#HULMID', '#HULWHU', '#HULCRY', '#HULLEI', '#HULEVE', '#HULMCI', '#HULLIV', '#SUNSTK', '#SUNSWA', '#SUNARS', '#SUNCHE', '#SUNMUN', '#SUNWHU', '#SUNWAT', '#SUNMID', '#SUNWHU', '#SUNCRY', '#SUNLEI', '#SUNEVE', '#SUNMCI', '#SUNLIV', '#STKSWA', '#STKARS', '#STKCHE', '#STKMUN', '#STKWHU', '#STKWAT', '#STKMID', '#STKWHU', '#STKCRY', '#STKLEI', '#STKEVE', '#STKMCI', '#STKLIV', '#SWAARS', '#SWACHE', '#SWAMUN', '#SWAWHU', '#SWAWAT', '#SWAMID', '#SWAWHU', '#SWACRY', '#SWALEI', '#SWAEVE', '#SWAMCI', '#SWALIV', '#ARSCHE', '#ARSMUN', '#ARSWHU', '#ARSWAT', '#ARSMID', '#ARSWHU', '#ARSCRY', '#ARSLEI', '#ARSEVE', '#ARSMCI', '#ARSLIV', '#CHEMUN', '#CHEWHU', '#CHEWAT', '#CHEMID', '#CHEWHU', '#CHECRY', '#CHELEI', '#CHEEVE', '#CHEMCI', '#CHELIV', '#MUNWHU', '#MUNWAT', '#MUNMID', '#MUNWHU', '#MUNCRY', '#MUNLEI', '#MUNEVE', '#MUNMCI', '#MUNLIV', '#WHUWAT', '#WHUMID', '#WHUWHU', '#WHUCRY', '#WHULEI', '#WHUEVE', '#WHUMCI', '#WHULIV', '#WATMID', '#WATWHU', '#WATCRY', '#WATLEI', '#WATEVE', '#WATMCI', '#WATLIV', '#MIDWHU', '#MIDCRY', '#MIDLEI', '#MIDEVE', '#MIDMCI', '#MIDLIV', '#WHUCRY', '#WHULEI', '#WHUEVE', '#WHUMCI', '#WHULIV', '#CRYLEI', '#CRYEVE', '#CRYMCI', '#CRYLIV', '#LEIEVE', '#LEIMCI', '#LEILIV', '#EVEMCI', '#EVELIV', '#MCILIV']

This answer relies on the use of itertools.combinations:

itertools.combinations(iterable, r)

Return r length subsequences of elements from the input iterable.

Combinations are emitted in lexicographic sort order. So, if the input iterable is sorted, the combination tuples will be produced in sorted order.

Elements are treated as unique based on their position, not on their value. So if the input elements are unique, there will be no repeat values in each combination.

Community
  • 1
  • 1
Tagc
  • 8,736
  • 7
  • 61
  • 114
0

This one should work.

result = [i+j for i in listTags for j in listTags if i != j]

Example:

listTags = [ "TOT" , "WBA", "BUR", "SOU"]
result = [i+j for i in listTags for j in listTags if i != j]
print result

Output:

['TOTWBA', 'TOTBUR', 'TOTSOU', 'WBATOT', 'WBABUR', 'WBASOU', 'BURTOT', 'BURWBA', 'BURSOU', 'SOUTOT', 'SOUWBA', 'SOUBUR']
Sheng
  • 3,467
  • 1
  • 17
  • 21
0

Use combinatoric generators provided by itertools, for instance,

import itertools

listTags = [ "TOT" , "WBA", "BUR"]

# in sorted order, no repeated elements
results = [''.join(t) for t in itertools.combinations(listTags, 2)] 
# ['TOTWBA', 'TOTBUR', 'WBABUR']

# all possible orderings, no repeated elements
results = [''.join(t) for t in itertools.permutations(listTags, 2)]
# ['TOTWBA', 'TOTBUR', 'WBATOT', 'WBABUR', 'BURTOT', 'BURWBA']
SparkAndShine
  • 17,001
  • 22
  • 90
  • 134
0

You can use a combination(pun intended) or itertools.combinations() and set() to get all unique combinations:

>>> import itertools
>>> 
>>> listTags = [ "TOT" , "WBA", "BUR", "SOU", "HUL", "SUN", "STK", "SWA", 
...                  "ARS", "CHE", "MUN", "WHU", "WAT", "MID" "WHU", "CRY", "LEI", 
...                  "EVE", "MCI", "LIV"]
>>> 
>>> combinations = set('#{pair[0]}{pair[1]}'.format(pair=pair) for pair in itertools.combinations(listTags, 2))
>>> combinations
set(['#TOTWBA', '#BURMUN', '#HULLEI', '#WBASUN', '#HULWHU', '#SUNSWA', '#BURSWA', '#TOTCRY', '#MUNCRY', '#SOUSTK', '#WATCRY', '#SUNLIV', '#STKCHE', '#STKCRY', '#TOTSUN', '#LEILIV', '#EVELIV', '#STKSWA', '#TOTBUR', '#EVEMCI', '#SOUWAT', '#SUNEVE', '#SWAWAT', '#LEIMCI', '#TOTCHE', '#MUNMCI', '#WBASTK', '#SUNLEI', '#BURLEI', '#BURMCI', '#SOUCRY', '#BURWHU', '#HULWAT', '#WHUEVE', '#WBALIV', '#ARSMCI', '#SOUSUN', '#MUNEVE', '#BURSOU', '#WHUMIDWHU', '#ARSLEI', '#SWAMIDWHU', '#SOUARS', '#WBABUR', '#STKLIV', '#TOTLIV', '#TOTARS', '#TOTMCI', '#SWAEVE', '#WATEVE', '#SWAMCI', '#WHULIV', '#MIDWHULEI', '#WBAEVE', '#WBASWA', '#SWALIV', '#CHEMCI', '#STKMUN', '#BURSUN', '#CRYLIV', '#TOTSWA', '#WHUMCI', '#MUNLIV', '#HULMIDWHU', '#SOUEVE', '#MUNMIDWHU', '#CRYLEI', '#WBALEI', '#HULEVE', '#WBAWHU', '#WBASOU', '#SOUCHE', '#WATLEI', '#SWAWHU', '#BURARS', '#CHECRY', '#HULSWA', '#SUNSTK', '#SOULIV', '#MUNWAT', '#SOUMCI', '#SUNCRY', '#ARSMIDWHU', '#MUNWHU', '#STKWAT', '#SUNMIDWHU', '#WBAHUL', '#SOUMIDWHU', '#STKWHU', '#CHELIV', '#WHUCRY', '#BURCHE', '#HULLIV', '#SOUMUN', '#HULCRY', '#TOTLEI', '#STKLEI', '#HULMCI', '#TOTMIDWHU', '#HULSTK', '#SWAARS', '#MUNLEI', '#WBAMIDWHU', '#WATMCI', '#ARSMUN', '#TOTWAT', '#WATLIV', '#CHEMUN', '#BURLIV', '#MCILIV', '#TOTEVE', '#ARSCHE', '#CRYMCI', '#CHEEVE', '#BURSTK', '#HULSUN', '#WATMIDWHU', '#WBAWAT', '#LEIEVE', '#STKMCI', '#WBACHE', '#SUNARS', '#TOTHUL', '#ARSLIV', '#SWAMUN', '#SWACRY', '#SUNMCI', '#TOTSOU', '#ARSWAT', '#WHUWAT', '#ARSCRY', '#SOUHUL', '#HULARS', '#WHULEI', '#BURWAT', '#SUNCHE', '#MIDWHUMCI', '#CHEWHU', '#TOTSTK', '#BURCRY', '#WBAARS', '#MIDWHUEVE', '#CRYEVE', '#STKMIDWHU', '#CHEWAT', '#ARSEVE', '#CHELEI', '#WBAMCI', '#SUNWAT', '#WBACRY', '#BURHUL', '#TOTMUN', '#MIDWHULIV', '#STKARS', '#STKEVE', '#HULCHE', '#SWACHE', '#SUNMUN', '#HULMUN', '#SOUSWA', '#SOUWHU', '#SOULEI', '#BURMIDWHU', '#WBAMUN', '#SUNWHU', '#TOTWHU', '#BUREVE', '#SWALEI', '#ARSWHU', '#MIDWHUCRY', '#CHEMIDWHU'])
>>> 
Christian Dean
  • 22,138
  • 7
  • 54
  • 87