Python Forum
A strange value in dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A strange value in dictionary
#1
Hello,
I'm trying to write a solution for exercise 12.4 from "Think Python", and I have a strange behavior of my return value.
Here's the code:
def tuple_list_dict_init(w_list):
	"""
	Return a dictionary that have as keys tuples of letters(sorted) from words in a given list.
	w_list:	a given list of words. Should be of type list of strings.
	d = dict()
	"""
	for l in w_list:					#	this loop for test purposes only
		if (type(l) is int):
			print('Integer detected')
	
	for w in w_list:
		if (type(tuple(sorted(w))) is int):		#	this conditional for test purposes only
			print('Integer key detected')
		d.setdefault(tuple(sorted(w)), [])
		
	return d
When I call this function no 'print' is executed. But in the returned dictionary I can find key of type 'int'.
Where is the error? Thank you.
Reply
#2
well, what is the w_list you test with?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Jan-07-2019, 01:23 PM)buran Wrote: well, what is the w_list you test with?

The 'w_list' is a list of strings (should be).
I can test it like this too:
if not (type(l) is str):
    some_code_here
Reply
#4
(Jan-07-2019, 01:30 PM)python_user_n Wrote: The 'w_list' is a list of strings (should be).
In this case how you expect that any print will be executed.

On line 7 you iterate over elements in the list and on line 8 check if the type of the element is int (obviously it's not because you say it's string)

then on line 11 you iterate over elements in the list and on line 12 you first convert the string to tuple, then check if that tuple is int?! (obviously it's not because it's a tuple :-))
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(Jan-07-2019, 01:35 PM)buran Wrote: In this case how you expect that any print will be executed.

On line 7 you iterate over elements in the list and on line 8 check if the type of the element is int (obviously it's not because you say it's string)

then on line 11 you iterate over elements in the list and on line 12 you first convert the string to tuple, then check if that tuple is int?! (obviously it's not because it's a tuple :-))
And it's should not print (as I was thinking) ;)
But in the returned dictionary I found an item with a key of the type 'int' that have a value of 'int'.
Maybe it's .setdefault() method behavior(predicted?). Thanks.
Reply
#6
Shouldn't this function give NameError as d is not defined?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
(Jan-07-2019, 01:40 PM)perfringo Wrote: Shouldn't this function give NameError as d is not defined?

...
Thank you. One hour for debugging because of this definition on top of my test .py file:
d = {1:2, 0:'a'}
Reply
#8
(Jan-07-2019, 01:40 PM)python_user_n Wrote: But in the returned dictionary I found an item with a key of the type 'int' that have a value of 'int'.
Maybe it's .setdefault() method behavior(predicted?). Thanks.


once again - show us your full code. we cannot guess what your list that you pass to the function looks like. what you get from the function and what is expected output?

(Jan-07-2019, 01:40 PM)perfringo Wrote: Shouldn't this function give NameError as d is not defined?

I guess the line d = dict() is in the doc-string by mistake :-)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
(Jan-07-2019, 01:45 PM)buran Wrote: once again - show us your full code. we cannot guess what your list that you pass to the function looks like. what you get from the function and what is expected output?
The full code was in the same file where I was making test of different sequences behavior. That's my mistake and that's why I had no 'NameError'. Basically I had 'd' defined with some values, and not commented it out, and in my case 'd' was a global value.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Nested dictionary acting strange Pedroski55 2 2,085 May-13-2021, 10:37 PM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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