蟒蛇检查字符串中包含的所有字符 [英] python check string contains all characters
问题描述
我读的字一个长长的清单,我做了一个节点列表中的每一个字。每个节点都有一个属性字为它们在列表中的位置。
I'm reading in a long list of words, and I made a node for every word in the list. Each node has an attribute 'word' for their position in the list.
我试图连接一个节点到下一个节点,如果下一节点是previous节点,有加只是一个字母的
I am trying to connect a node to the next node if the next node is the previous node, with an addition of just one letter
我也按字母顺序排列的每个字符每个单词,使CAT - > ACT
I also alphabetically ordered each word per character, so that CAT -> ACT
我想提请每个唯一的起始字边,以所有可能的链条,这样我就可以看到列表中的所有可能的链条。
I want to draw an edge from each unique starting word, to all of the possible chains, so I can see all the possible chains in the list.
例如
A - > AN - >谭 - > RANT
A -> AN -> TAN -> RANT
然而,一个--x-> T
However A --x-> T
这是我尝试
for i in range(0, G.number_of_nodes()-1):
if ( ( (len(G.node[i]['word'])+1) == len(G.node[i+1]['word']) ) and (G.node[i]['word'] in G.node[i+1]['word'])):
print G.node[i]['word'], G.node[i+1]['word']
给了我这个,
Gave me this,
DGO DGOS
DGOS DGOSS
I IN
ELLMS ELLMSS
AEPRS AEPRSS
INW DINW
DINW DINWY
为什么我看不到INW?
Why do I not see IN INW?
此外,AGNRT AGNRST应该是有,但我不明白为什么,还有很多其他的对
Also, AGNRT AGNRST should be on there but I don't understand why, along with a lot of other pairs
如果你觉得我哪里出了问题?
Where do you think I went wrong?
推荐答案
现在的问题是,你是出现紧挨着对方列表中唯一比较的话,即字我
和 I + 1
,例如: 我
和在
显示彼此相邻,如做 WIN
和 WIND
,而在
和 WIND
相距甚远。看来你要比较所有可能的话,这需要更复杂的算法。这里有一个想法:
The problem is that you are only comparing words that appear right next to each other in the list, i.e. words i
and i+1
, e.g. I
and IN
appear next to each other, as do WIN
and WIND
, but IN
and WIND
are far apart. It seems you want to compare all possible words, which requires a more sophisticated algorithm. Here's an idea:
- 请一本字典,他们的键进行排序的话,值是实际的话,如清单
{ACT:CAT,法,TAC],...}
Acollections.defaultdict(名单)
将是这个有用的。 - 排序字的完整输入列表按长度。您可以使用
list.sort(键= LEN)
假设你有话只是一个列表。 - 遍历列表按长度排序。对于每一个字,都要经过长
N-1
的每一个子集。像为我的range(LEN(字)):流程(字[我] +字[我+ 1:])
。您可能要小心重复这里。 - 对于每个子集,排序的子集,看它在字典中。请从每一个字在字典中的值的链接(实际单词的列表),以更大的字。
- Make a dictionary where they keys are sorted words and the values are lists of actual words, e.g.
{"ACT": ["CAT", "ACT", "TAC], ...}
. Acollections.defaultdict(list)
will be useful for this. - Sort the full input list of words by length. You can use
list.sort(key=len)
assuming you have just a list of words. - Iterate through the list sorted by length. For each word, go through every subset of length
n-1
. Something likefor i in range(len(word)): process(word[:i] + word[i+1:])
. You may want to be careful about duplicates here. - For each subset, sort the subset and look it up in the dictionary. Make a link from every word in the dictionary's value (a list of actual words) to the bigger word.
这篇关于蟒蛇检查字符串中包含的所有字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!