海军一词由五个连续的,重叠的州邮政缩写组成:马萨诸塞州(MA),阿肯色州(AR),罗德岛(RI),印第安纳州(IN)和内布拉斯加州(NE)。查找具有相同属性的七个字母的单词。
我正在使用Python打开大约5000个单词的列表。我想先找到一个包含5个州缩写的单词。
def puzzleH(word):
states = ['al', 'ak', 'az', 'ar', 'ca', 'co', 'ct', 'dc', 'de', 'fl', 'ga',
'hi', 'id', 'il', 'in', 'ia', 'ks', 'ky', 'la', 'me', 'md',
'ma', 'mi', 'mn', 'ms', 'mo', 'mt', 'ne', 'nv', 'nh', 'nj',
'nm', 'ny', 'nc', 'nd', 'oh', 'ok', 'or', 'pa', 'ri', 'sc',
'sd', 'tn', 'tx', 'ut', 'vt', 'va', 'wa', 'wv', 'wi', 'wy']
checker = 0;
for st in states:
if st in word:
checker+=1
if checker==5:
# ...still thinking...
#pos = (i for i,st in enumerate(word) if st in states)
#for i in pos: print(i)
#return word
# Main program
ListH = []
for word in wordList:
if puzzleH(word)!=None:
ListH.append(puzzleH(word))
找到包含5个状态缩写的单词后,我将找到每个状态缩写的索引。而这些指标的名单与[0,1,2,3,4]
或[1,2,3,4,5]
或[2,3,4,5,6]
。但是我不知道该怎么做。
如果目的是仅标识由五个连续的,重叠的州邮政缩写组成的单词,则可以尝试以下操作:
states = ['al', 'ak', 'az', 'ar', 'ca', 'co', 'ct', 'dc', 'de', 'fl', 'ga',
'hi', 'id', 'il', 'in', 'ia', 'ks', 'ky', 'la', 'me', 'md',
'ma', 'mi', 'mn', 'ms', 'mo', 'mt', 'ne', 'nv', 'nh', 'nj',
'nm', 'ny', 'nc', 'nd', 'oh', 'ok', 'or', 'pa', 'ri', 'sc',
'sd', 'tn', 'tx', 'ut', 'vt', 'va', 'wa', 'wv', 'wi', 'wy']
def check_word(word):
if len(word) != 7:
return False
counter = 0
for i in range(0, len(word) - 1):
abv = word[i] + word[i + 1]
if abv in states:
counter += 1
if counter == 5:
return True
else:
counter = 0
return False
for w in wordList:
print("{0} : {1}".format(w, check_word(w)))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句