for循环遍历单词 [英] for loop to iterate through words

查看:149
本文介绍了for循环遍历单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前的帖子引起了很多混乱,并且充满了与我的问题无关的答案. (我的错是不明确的意思)我标记了该帖子,这是新帖子. 所以基本上我想用词做连词.

My previous post caused a lot of confusion and it flooded with answers that is not relevant to my questions. (My fault for not clarifying things) I flagged that post and this is the new post. So basically I would like to do a conjunction of words.

EG1

    input [jason, sonny, nyorth]

    output [jason, sonny, nyorth, jasonnyorth]

EG2
    Sample input: [aw, was,poq, qo, soo] 
    Output [aw, was, poq, qo, soo, awasoo, poqo] 


EG3

    input: `[keyboard, ardjimmy]    
    output: `[keyboard, ardjimmy, keyboardjimmy]

我正在尝试

['jimmy', 'myolita'] 
jimmyolita
['jimmy', 'myolita', 'jimmyolita']

['myolita', 'jimmy']
jimmyolita
['myolita', 'jimmy', 'jimmyolita']

我知道这是一个双for循环,但是我一直在获得超级奇怪的东西.我想简化我的帖子,所以我不会发布我的尝试S =.另外,我也不喜欢执行检查"和"maxNum"操作的方式,我感觉它不能涵盖所有情况,并且在某些特定情况下会给我带来奇怪的输出.我更喜欢在for循环中执行此操作,因为这里没有列出其他问题.一个例子是当存在无限情况时:

I know it's a double for loop but I just keep getting super weird stuff. I want to keep my post simple so I am not posting my attempts S=. On the side note I also dislike the way I do the "check" and "maxNum" thing, I have a feeling that it doesn't cover all case and gives me weird output for certain specific case. I prefer to do it in for loop because there are other part of the questions that is not listed in here. One example is when there's an infinite case:

[abc, bca] -> [abc, ca, abca] -> [abc, ca, abca, abcabc] -> it will keep going.



testing = ["jimmy", "myolita"]

testing1 = ["myolita", "jimmy"]
def frags(strings):
    check = 1
    maxNum = 1
    for i in range(0,len(strings[1])):
        if strings[0].find(strings[1][:i]) > maxNum:
            check = 0
            maxNum = strings[0].find(strings[1][:i])

    if check == 0:
       toReturn = strings[0][:maxNum] + strings[1]
       strings.append(toReturn)
    else:
       toReturn = "no match"

    return toReturn



print(testing)
print(frags(testing))                            
print(testing)

print("   ")

print(testing1)
print(frags(testing1))                            
print(testing1)

推荐答案

我认为您的规则是这样的:如果第一个字符串的后缀也是第二个字符串的前缀,则将其切掉.然后合并两个字符串.

I think your rule goes something like this: If the first string has a suffix that's also a prefix of the second string, chop it off. Then merge the two strings.

如果您向后搜索,从整个字符串开始,然后向下进行操作,而不是从单个字符开始,然后向上进行处理,则除了要测试的当前后缀之外,您根本不需要跟踪任何内容.换句话说:

If you search backward, starting with the whole string and working down instead of starting with a single character and working up, you don't need to keep track of anything at all, other than the current suffix you're testing. In other words:

def frags(strings):
    left, right = strings
    for i in reversed(range(len(left))):
        if right.startswith(left[-i:]):
            return left[:-i] + right
    return left+right

这篇关于for循环遍历单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆