Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Printing empty list?
#1
def kasinski(text):
    trigraphs=[]
    distances=[]
    for trigraph in range (len(text)-2):
        newtrigraph= text[trigraph:trigraph+3]
    if newtrigraph in trigraphs:
        distances.append(trigraph-(text.index(newtrigraph)))
    else:
        trigraphs.append(newtrigraph)
        trigraphs.append(trigraph)
        return distances
Can anyone identify why this function is returning "[]"? I am printing with the string "UZRZEGNJENVLISEXRHLYPYEGTESBJHJCSBPTGDYFXXBHEEIFTCCHVRKPNHWXPCTUQTGDJHTBIPRFEMJCNHVTCFSAIIJENREGSALHXHWZWRZXGTTVWGDHTEYXISAGQTCJPRSIAPTUMGZALHXHHSOHPWCZLBRZTCBRGHCDIQIKTOAAEFTOPYEGTENRAIALNRXLPCEPYKGPNGPRQPIAKWXDCBZXGPDNRWXEIFZXGJLVOXAJTUEMBLNLQHGPWVPEQPIAXATYENVYJEUEI"(this string does contain repeated trigraphs)
Reply
#2
Don't double post
Reply
#3
If the way your code is indented is the way it is in your code snippet, you just have to indent the lines after your for loop as shown below. Hope that helps =]

def kasinski(text):
    trigraphs=[]
    distances=[]
    for trigraph in range (len(text)-2):
        newtrigraph= text[trigraph:trigraph+3]
        if newtrigraph in trigraphs:
            distances.append(trigraph-(text.index(newtrigraph)))
        else:
            trigraphs.append(newtrigraph)
            trigraphs.append(trigraph)
        return distances
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Code with empty list not executing adeana 9 3,797 Dec-11-2023, 08:27 AM
Last Post: buran
  set.difference of two list gives empty result wardancer84 4 1,535 Jun-14-2022, 01:36 PM
Last Post: wardancer84
Question Printing through list.. again jesse68 2 1,162 Apr-16-2022, 03:24 PM
Last Post: jesse68
  displaying empty list vlearner 5 1,712 Jan-19-2022, 09:12 AM
Last Post: perfringo
  Remove empty keys in a python list python_student 7 3,105 Jan-12-2022, 10:23 PM
Last Post: python_student
  help for list printing jip31 8 3,703 May-01-2021, 03:52 AM
Last Post: Pedroski55
  What is the value after JOINING an empty list? JaneTan 2 5,275 Jan-04-2021, 06:25 PM
Last Post: deanhystad
  Problem printing last element from a list tester_V 3 2,429 Oct-30-2020, 04:54 AM
Last Post: tester_V
  Stop a function if the list it needs is empty Pedroski55 2 2,956 Jul-25-2020, 11:50 PM
Last Post: Pedroski55
  Printing images from a list Heyjoe 4 2,853 Jun-22-2020, 02:28 AM
Last Post: Heyjoe

Forum Jump:

User Panel Messages

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