将Python列表中的字符串以两位为单位

时间:2019-07-05 16:52:49

标签: python list iteration concatenation combinations

如何连接字符串列表中的所有元素,并获得仅将元素连接为二的输出?例如,有一个列表:

engWords=["water","big","apple","watch","banana","york","amsterdam","orange","macintosh","bottle","book"]

我想要一个输出,其中还有另一个列表,其中包含两个字符串的所有可能串联的串联,即

outout = ["waterbig","waterapple", "waterbanana",.....,"bigapple",....]

就像一个字符串的所有可能组合的列表。如何遍历此列表并产生此输出?

1 个答案:

答案 0 :(得分:1)

output = []
c=0
engWords=["water","big","apple","watch","banana","york","amsterdam","orange","macintosh","bottle","book"]
for x in range(0,len(engWords)-1):
    for i in range(1,len(engWords)):
        if i == len(engWords) and c == len(engWords):
            break
        elif i == c:
            pass
        else:
            output.append(engWords[c]+engWords[i])
    c += 1

print(output)