【发布时间】:2015-04-22 00:22:34
【问题描述】:
这个 Python 函数将两个单词的字符互锁(例如,“sho” + “col” -> “school”)。 word1-char1 + word2-char1 + word1-char2 + ...
def interlock(a,b):
i = 0
c = ""
d = ""
while (i < len(a) and len(b)):
c = (a[i]+b[i])
d = d + c
i+=1
return(d)
interlock("sho", "col")
现在,我想将此函数应用于单词列表。目标是找出任何联锁对应于列表中的一项。
word_list = ["test", "col", "tele", "school", "tel", "sho", "aye"]
为此,我首先必须创建一个包含所有互锁的新列表。这正是我卡住的地方——我不知道如何使用联锁来迭代 word_list。
感谢您的帮助!
【问题讨论】:
-
i < len(a) and len(b)表示(i < len(a)) and len(b) -
是
test-col和col-test有效配对还是只配对一次? -
@PadraicCunningham:test-col 和 col-test 都无效。例如,test + col -> tceoslt : 无效 // test + tele -> tteeslte : 无效 // col + test -> ctoelst : 无效 // sho + col -> school : 有效
-
但它们都出现在列表中?我说的是要传递给联锁的组合
-
联锁应用于单词的单个字符,而不是单词。感谢您指出这一点!
标签: python list function loops python-3.x