python 中单词的笛卡尔积
Cartesian product of a word in python
我正在寻找一种方法来获取以下形式的字符串的笛卡尔积,
text = 'school'
我想要这样的结果,
list_ = [(s,c),(c,h),(h,o),(o,o),(o,l)]
这是我试过的,
text = 'school'
list_=[]
for i in range(len(text)):
while i < len(text)+1:
print(text[i], text[i+1])
list_.append((text[i], text[i+1]))
i = i+1
我得到了必要的列表,但抛出了一些错误。有什么优雅的方法可以做到这一点吗?
text = 'school'
list(zip(text, text[1:]))
Out[1]:
[('s', 'c'), ('c', 'h'), ('h', 'o'), ('o', 'o'), ('o', 'l')]
我正在寻找一种方法来获取以下形式的字符串的笛卡尔积,
text = 'school'
我想要这样的结果,
list_ = [(s,c),(c,h),(h,o),(o,o),(o,l)]
这是我试过的,
text = 'school'
list_=[]
for i in range(len(text)):
while i < len(text)+1:
print(text[i], text[i+1])
list_.append((text[i], text[i+1]))
i = i+1
我得到了必要的列表,但抛出了一些错误。有什么优雅的方法可以做到这一点吗?
text = 'school'
list(zip(text, text[1:]))
Out[1]:
[('s', 'c'), ('c', 'h'), ('h', 'o'), ('o', 'o'), ('o', 'l')]