0

I have a loop:

 for x in range(0, len(temp)):
    if temp[val] == char:
        print (" %-9r | %10d | %12s" % (char, frequency, huffmanCode[char]))
        val += 1

What it is supposed to do is that char variable should be searched in temp list and if it is found it will be displayed.

For example, if I enter "google" then the output should be:

Character | Frequency | Huffman Code
------------------------------------
'g'       |         2 |           0
'o'       |         2 |          11
'o'       |         2 |          11
'g'       |         2 |           0
'l'       |         1 |         101
'e'       |         1 |         100

But for some reason it does not display anything. How would I search my whole temp list for characters?

7
  • could you define temp? Is it a dictionary or list or ... Commented May 11, 2016 at 14:29
  • temp is a list @DrBwts basically it contains all characters in a given string including whitespaces and special characters Commented May 11, 2016 at 14:43
  • Well, I beleive you post isn't clear. You're not using x, you haven't explained what temp is nor what it contains, you haven't told what val is. Also in your 'example', "if you enter 'google'" ... enter google where? Commented May 11, 2016 at 14:46
  • my mistake. val is just a counter for the specific list position. I replaced it with x already. But it still does not print my desired output. Basically my program asks you to enter a word/phrase which will be turned to a list, which is my temp (temp = list(word)) so if I enter "google" then temp contains [g,o,o,g,l,e] @tglaria Commented May 11, 2016 at 14:52
  • If you have changed your code, then edit this question!. What is char then? Commented May 11, 2016 at 15:27

2 Answers 2

2

should you be looking for the index of x instead of val? Without seeing your whole code / what those variables mean, it's hard to tell. But if temp[val] doesn't equal char initially, it will never be incremented and therefore the result will never change.

Try this maybe?

 for x in range(0, len(temp)):
     if temp[x] == char:
         print (" %-9r | %10d | %12s" % (char, frequency, huffmanCode[char]))
         val += 1
Sign up to request clarification or add additional context in comments.

8 Comments

I tried that and entered the word "testing" and this came out imgur.com/gA3X4w1. It should be printed based on how the word is spelled... @stackunderflow
are you presorting your list before iterating over it, perhaps? try running it with this: temp = list('testing')
no @stackunderflow I created another list to make sure but still prints the same and yes that is exactly what I did. I only used the temp variable on that part.
the logic, in that case doesn't make much sense. If temp == ['t','e','s','t','i','n','g'] iterating over over the indicies will print them in order. The only thing that could possibly change that (without seeing the rest of your code) would be if you ordered or sorted temp somehow. :/
Check this out, imgur.com/65tNctj I printed the contents of the list to make sure :/ @stackunderflow
|
1

You could use the count function from the array object, it will return the number of times the item appears in your list, and if the method returns 0 you could skip the print. Like this:

if temp.count(char) > 0 :
    print (" %-9r | %10d | %12s" % (char, frequency, huffmanCode[char]))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.