用威尔士语计数文本中的字母
如何计算Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch中的字母? print(len('Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch')) 说58 好吧,如果那是那么容易,我不会问你,现在可以吗? 维基百科说(https://en.wikipedia.org/wiki/Llanfairpwllgwyngyll#Placename_and_toponymy) 名称的长格式是英国最长的地名,也是世界上最长的地名之一,共58个字符(因为“ ch”和“ ll”是二字,所以为51个“字母”,在英文字母中被视为单个字母)。威尔士语)。 所以我想算一下,得到答案51。 对。 print(len(['Ll','a','n','f','a','i','r','p','w','ll','g','w','y','n','g','y','ll','g','o','g','e','r','y','ch','w','y','r','n','d','r','o','b','w','ll','ll','a','n','t','y','s','i','l','i','o','g','o','g','o','g','o','ch'])) 51 是的,但这很欺骗,显然我想使用单词作为输入,而不是列表。 维基百科还说威尔士语中的有向字母是ch,dd,ff,ng,ll,ph,rh,th https://zh.wikipedia.org/wiki/威尔士orthography#Digraphs 所以我们走了。让我们加长长度,然后取消重复计算。 word='Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch' count=len(word) print('starting with count of',count) for index in range(len(word)-1): substring=word[index]+word[index+1] if substring.lower() in ['ch','dd','ff','ng','ll','ph','rh','th']: print('taking off double counting of',substring) count=count-1 print(count) 这使我走得很远 starting with count of 58 taking off double counting of Ll …