Python Forum
Loop do not start for letter matching algoritm
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loop do not start for letter matching algoritm
#1
Hello,
I make a simple algoritm to compare two strings, but the loop do not continue to the end. This end is the minimum string lenght from these two data entry.

Here's my code :

j1 = "BB"
j2 = "AABBCCDDEEFFGGHHIIJJKKLLMMBBDCCDEEFFGGHHIIJJKKLLMM"

t = 0
tab = []

if len(j1) < len(j2):
    n = len(j1)
else:
    n = len(j2)

for i in range(n):

    while j1[i] == j2[i]:
        t += 1
        tab.append(t)

print(tab)
print(t)
I have this :

[]
0
I must have this type of result:

[1, 2]
2
For AA in the first string = AA to the second string and the loop stop.

Thanks for any help. Big Grin
Reply
#2
This happens because you took the shorter string.

import itertools

new_list = []
for a, b in itertools.zip_longest(j1, j2):
    if a == b:
        new_list.append(a)
This must result in an empty list.
Describe what you want to do.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
Finaly I solved my problem by doing this :

t = 0
tab = [0] * 26

if len(j1) < len(j2):
    n = len(j1)
else:
    n = len(j2)

for i in range(n):

    if j1[i] == j2[i]:
        t += 1
        tab[i] = t
        
    if tab[i] == 0:
            break
Because in my programm I need to stop the loop on minimum lenght.

Thank you.
Reply
#4
This code here is simple but not easy to read
if len(j1) < len(j2):
    n = len(j1)
else:
    n = len(j2)
This code here does the same, is more simple and easy to read
n = min(len(j1), len(j2))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Start loop from different points TheHolyPyGrenade 3 2,051 Apr-11-2021, 07:57 PM
Last Post: TheHolyPyGrenade
  What's the difference b/w assigning start=None and start=" " Madara 1 2,314 Aug-06-2018, 08:23 AM
Last Post: buran
  Can I Control loop with Keyboad key (start/stop) Lyperion 2 3,331 Jul-28-2018, 10:19 AM
Last Post: Lyperion
  Python- Help with try: input() except ValueError: Loop code to start of sequence Aldi 2 6,286 Mar-08-2018, 03:46 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020