Home > Software design >  zip or forloop two different number of lenth in python
zip or forloop two different number of lenth in python

Time:01-09

Let me expalain what the real problem is.

i am having two strings :

str1="QKHE=K"
str2="Qkhek" #this string donot have '=' inthe same index 'k' is here.

i wanted to match the two strings and the desire result should be like

result="Qkhe=k"#case sensitive

i tried my function :

def matchCap(cap1=None,cap2=None):
        if cap1==None or cap2==None:
            return cap1
        else:
            cap1=list(cap1)
            cap2=list(cap2)
            dictionary = (zip(cap1, cap2))
            print(list(dictionary))
            final_cap=[]
            for key, value in dictionary:
                if key=='@' or key=='=':
                    final_cap.append(key)
                else:
                    final_cap.append(value)
            final_cap=''.join(final_cap)
            return final_cap

But the zip function not workingwell because of different lengths of list.

so basically i am having two string ..amm like two captcha answers . Gave captcha example for the case sensitive . i want the result captcha answer is Qkhe=k

CodePudding user response:

str1="QKHE=K"
str2="Qkhek"
def matchCap(cap1=None,cap2=None):
        if cap1==None or cap2==None:
            return cap1
        else:
            cap1=list(cap1)
            cap2=list(cap2)
            final_cap=[]
            cap_2_i = 0
            for cap_1 in range(len(cap1)):
                key = cap1[cap_1]
                value = cap2[cap_2_i]
                if key=='@' or key=='=':
                    final_cap.append(key)
                else:
                    final_cap.append(value)
                    cap_2_i  = 1
            final_cap=''.join(final_cap)
            return final_cap
matchCap(str1, str2)

Result: 'Qkhe=k'

CodePudding user response:

You don't need to use zip, just for iterates through it.

Define a variable i that represents the index position of the value from cap2, and i is accumulated each time a value is taken from cap2.

def matchCap(cap1=None, cap2=None):
    if cap1 is None or cap2 is None:
        return cap1
    else:
        total_len = len(cap2)
        i = 0
        final_cap = []
        for index, value in enumerate(cap1):
            if value == '@' or value == '=':
                final_cap.append(value)
            else:
                final_cap.append(cap2[i])
                i  = 1
                if i >= total_len:
                    break
        final_cap = ''.join(final_cap)
        return final_cap


print(matchCap("QKHE=K", "Qkhek"))

# Qkhe=k

CodePudding user response:

One thing you can do is, when you encounter @ or = in the string, you can append both the key and value, like this...

def matchCap(cap1=None,cap2=None):
    if cap1==None or cap2==None:
        return cap1
    else:
        cap1, cap2 = list(cap1), list(cap2)
        dictionary = (zip(cap1, cap2))
        final_cap = []

        for key, value in dictionary:
            if key in ('@', '='):
                final_cap.append(key)
                final_cap.append(value)
            else:
                final_cap.append(value)

        final_cap = ''.join(final_cap)
        return final_cap

print(matchCap("QKHE=K","Qkhek"))

Output:-

Qkhe=k
  •  Tags:  
  • Related