连接list python中的字符串以提供不同的输出

时间:2017-01-07 19:14:11

标签: python list

我有下面显示的字符串列表。我想将每个字符串与每个其他字符串组合在一起,以提供独特的组合。

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

所以对于上面的列表,它将是#TOTWBA,#TOTBUR,#TOTSOU ......等。我怎么能在python中做到这一点?

4 个答案:

答案 0 :(得分:3)

['#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']

<强>输出

itertools.combinations

这个答案取决于html,body { padding: 0; margin: 0; background-color: red; } .row { background-color: darkgray; }

的使用
  

itertools.combinations(iterable,r)

     

从输入迭代中返回元素的r长度子序列。

     

组合以字典排序顺序发出。因此,如果输入iterable已排序,则组合元组将按排序顺序生成。

     

元素根据其位置而不是其价值被视为唯一。因此,如果输入元素是唯一的,则每个组合中都不会有重复值。

答案 1 :(得分:0)

这个应该有效。

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

示例:

['TOTWBA', 'TOTBUR', 'TOTSOU', 'WBATOT', 'WBABUR', 'WBASOU', 'BURTOT', 'BURWBA', 'BURSOU', 'SOUTOT', 'SOUWBA', 'SOUBUR']

输出:

{{1}}

答案 2 :(得分:0)

使用itertools提供的组合生成器,例如,

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']

答案 3 :(得分:0)

您可以使用组合(双关语)或set()>>> 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']) >>> 来获得所有独特组合:

file <file>.cer