简体   繁体   中英

Finding All Variants Of Word With Shifted Capital Letter

I need a python function that can do the following:

Given an input of 't' and 'tattle', it should return a list like so: ['Tattle','taTtle','tatTle'] Or with 'z' and 'zzzzz': ['Zzzzz','zZzzz','zzZzz','zzzZz','zzzzZ']

I coded the following, but it does not work with the second example because the current function checks to see if the basestr matches what is already in the resulting list, R, and can pick up false positives due to words with multiple basestr's already in the word. Anyone have any advice?

def all_variants(wrapletter,word):
    L,R,WLU,basestr=list(word),[],wrapletter.upper(),''
    if L.count(wrapletter)==1:
        for char in L:
            if wrapletter==char:
                basestr=basestr+WLU
            else:
                basestr=basestr+char
        R.append(basestr)
        return(R)
    else:
        for i in range(L.count(wrapletter)):
            basestr=''
            if i==0 and L[0]==wrapletter:
                basestr=WLU
                for char in range(1,len(L)):
                    basestr=basestr+L[char]
                R.append(basestr)
            else:
                for char in L:
                    if wrapletter==char:
                        if WLU in basestr:
                            basestr=basestr+char
                        elif basestr in str(R):
                            basestr=basestr+char
                        else:
                            basestr=basestr+WLU
                    else:
                        basestr=basestr+char
            R.append(basestr)
        R.remove(R[0])
        return(R)

It's not elegant, but maybe it's what you need?

target = "daaddaad"

def capitalize(target_letter, word):
    return [word[:i] + word[i].upper() + word[i + 1:]
            for i in xrange(len(word)) if word[i] == target_letter]

print capitalize("d", target)

Outputs:

['Daaddaad', 'daaDdaad', 'daadDaad', 'daaddaaD']
def all_variants(wrapletter, word):
    list = []
    for i in range(len(word)):
        if(word[i]==wrapletter):
            start = word[0:i].lower()
            str = word[i].upper()
            end = word[i+1::].lower()
            list.append(start+str+end)
    return list

These returned when I ran this function:

>>>all_variants("t", "tattle")
['Tattle', 'taTtle', 'tatTle']

>>>all_variants("z", "zzzzz")
['Zzzzz', 'zZzzz', 'zzZzz', 'zzzZz', 'zzzzZ']
inp = 't'
word = 'tattle'
inds = (i for i,ele in enumerate(word) if ele == inp)

print([word[:i]+word[i].upper()+word[i+1:] for i in inds])
['Tattle', 'taTtle', 'tatTle']

Try this. I iterate through each letter, shift it to uppercase, and sandwich it with the other parts of the original string.

def all_variants(wrapletter, word):
    variants = []
    for i, letter in enumerate(word):
        if letter == wrapletter:
            variants.append(word[:i] + letter.upper() + word[i+1:])
    return variants

print all_variants('z', 'zzzzz')
print all_variants('t', 'tattle')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM