« first day (888 days earlier)      last day (2490 days later) » 

7:52 PM
1473
Q: Iterating over dictionaries using 'for' loops

TopChefI am a bit puzzled by the following code: d = {'x': 1, 'y': 2, 'z': 3} for key in d: print key, 'corresponds to', d[key] What I don't understand is the key portion. How does Python recognize that it needs only to read the key from the dictionary? Is key a special word in Python? Or is it ...

 
1 message moved from Python
 
8:32 PM
:38316559
 
:38316500 does this work
no...
 
haha, sad
 
ah
not yet there
sigh, it went to another room
@AnttiHaapala now?
11 messages moved from Python
 
9:13 PM
def subset_sum(numbers, target, partial=[]):
s = sum(partial)

if s == target:
print("sum({})".format(partial))

if s >= target:
return # if we reach the target, why bother to continue?

for i in range(len(numbers)):
n = numbers[i]

remaining = numbers[i+1:]

stop = subset_sum(remaining, target, partial+[n])

subset_sum([1,2,3,4,5], target=6)

- How can I completely exit the recursion when the first if statement becomes true "s==target"; putting a return won't completely exit the function, but only the last subset_sum call.
 
1 message moved from Python
 
9:30 PM
But I want it to see if actual and predicted are the same then make a accurcary count
 
or is your question "how do I return a number from a function"?
 
I know how to do both
 
I don't understand your question but something tells me that image_list is not really relevant to it
please read How to Ask etc
 
I want to see if actual and predicted are the same in each list and if they are then add to the int count by 1
 
that sentence doesn't contain anything with decimals
consider writing up your question elsewhere and making sure it's clear and coherent and as simple as possible
 
9:34 PM
How can I put the list "image_list" into a function measure_accuracy then have it return a number of times actual and predicted are the same? In this case, it would return 3.

List:

image_list = [
{ 'path':'1.jpg', 'actual':{'dog'}, 'predicted':{'dog'} },
{ 'path':'2.jpg', 'actual':{'dog'}, 'predicted':{'cat'} },
{ 'path':'3.jpg', 'actual':{'cat'}, 'predicted':{'cat'} },
{ 'path':'4.jpg', 'actual':{'cat'}, 'predicted':{'dog'} },
{ 'path':'5.jpg', 'actual':{'dog'}, 'predicted':{'dog'} }
]
I have no idea where to start or what to try.
There
 
if your question is "how do I compare items in a list of nested dicts and perform simple arithmetic operations and return the result?", I would ask you to read a tutorial
in other words: start with a tutorial
 
What code do I use?
 
Hi @Brendan I suspect you need to review the early material in your Python textbook or tutorial because you appear to be a bit confused over some important fundamentals. Like the difference between a set and a list:
It actully does not need to be a set. It can be a list — Brendan 3 hours ago
I just need it to have the {} — Brendan 3 hours ago
Also
What does this mean? Doing if ['images'][0]['classifiers'][0]["classes"] not in json_data: Gives me TypeError: string indices must be integers @PM2Ring @roganjosh — Brendan 2 hours ago
 
Ok. What should I do about this other question?
 
9:38 PM
@Brendan I suggest you start by turning off your computer. Get a piece of paper and a pencil and describe in words the steps you need to follow to solve the problem.
Learning how to describe a solution is an important skill for a computer programmer.
When you can clearly describe the solution in English, then writing the code becomes easier.
 
I know the steps. You need to compare each item in the list and if then if the two items are the same add to a variable then in the end return that variable.
 
start with the first piece then
 
@Brendan so how don't you know where to start or what to try?
 
@Brendan That's a good start. You will need to refine this to be a little more specific. How do you "compare each item in the list"? Break this into smaller steps.
 
Yes. That is what I do not know how to do.
I know the rest
 
9:41 PM
eh
 
What does "compare each item in the list" mean? What steps do you need to do this?
Think about how you would do it by hand if someone gave you the list written on a piece of paper.
 
So how do I compare an item in a list. How do I see if in the following list the values of keys actual and predicted are the same.
 
Say you have a thousand lines on the paper. How would you go about "comparing each item"?
 
I would look at the two values
 
try searching "how do I compare each item in a list in python" on google
 
9:42 PM
Then I would see if they are the same
 
Garlic
 
@Brendan what two valeus?
 
The values of predicted and actual
 
Where do these "two values" come from? Specifically, how do you get them?
 
{ 'path':'5.jpg', 'actual':{'dog'}, 'predicted':{'dog'}
 
9:43 PM
@Code-Apprentice sigh
le sigh
 
From that list. On a piece of paper I would look at them
 
so it sounds like you are repeating an action for each item in the list. Now we can start thinking in Python a little more and English a little less. In python, when you want to repeat something, you need to use a loop of some type.
 
@Brendan here's a tutorial for you: skidmore.edu/~pdwyer/e/eoc/help_vampire.htm
 
yes so for actual in list:
 
lol :D
 
9:44 PM
would that be right?
 
read under the heading "If You're a Help Vampireā€¦"
 
@Brendan Close. One thing to be careful about here, is this list a Python list or a JSON array?
Because the items in the list you showed earlier look like JSON objects.
 
Json?
So put quotes in actual?
around*
 
Where do you get the list from?
 
The list "image_list"
image_list = [
{ 'path':'1.jpg', 'actual':{'a'}, 'predicted':{'a'} },
{ 'path':'2.jpg', 'actual':{'b'}, 'predicted':{'b'} },
]
 
9:47 PM
okay...maybe I'm just confused
 
@Code-Apprentice here's a tutorial for you too: skidmore.edu/~pdwyer/e/eoc/help_vampire.htm - start under the heading "Reforming the Help Vampire (e.g. Not You)"
 
I have a that list image_list, how do I see if actual and predicted are the same?
 
@Brendan You have a list of two dictionaries
 
you guys are going to kill davidism
 
Will they contain the same keys everytime?
Or will they differ in keys sometimes?
 
9:48 PM
yes, always the keys path, actual, and predicted
 
@Brendan Then just iterate through the keys using a for-each and then make sure they're equal
 
@Brendan nvm...forget about JSON. I was confused. Yes, your start with the for loop is correct. As long as you know what to do with actual.
 
or would it be for actual, predicted in image_list
then do if actual == predicted: count = count + 1
 
@Brendan what are the elements of the list?
 
This is the full list image_list = [
{ 'path':'1.jpg', 'actual':{'dog'}, 'predicted':{'dog'} },
{ 'path':'2.jpg', 'actual':{'dog'}, 'predicted':{'cat'} },
{ 'path':'3.jpg', 'actual':{'cat'}, 'predicted':{'cat'} },
{ 'path':'4.jpg', 'actual':{'cat'}, 'predicted':{'dog'} },
{ 'path':'5.jpg', 'actual':{'dog'}, 'predicted':{'dog'} }
]
The eleements would be path actual and predicted?
 
9:50 PM
PM 2Ring said as much...
 
So if you do for actual in image_list: what is the first value of actual? And what kind of thing is it?
 
@Brendan how about you also look at the link on the right: the one with 27 stars about how to paste formatted code into chat
 
then again you seem to be in write-only mode
 
78 messages moved from Python
 
 
1 hour later…
10:54 PM
-1
Q: Python str() : TypeError: 'str' object is not callable - the stop code

Khorudcode can only run the first time. after that I found a mistake : TypeError: 'str' object is not callable create : soup = BeautifulSoup(r.content, "lxml") berat = soup.find_all("dd", {"class": "pull-left m-0 border-none"})[0].text var1 = str(berat) str = string.maketrans('us', '12') result = va...

here too... and this was closed as a dupe of that abomination
2
Q: Str Object is not Callable; Python Dictionary Loop Error

AsadAnwer#Dictionary Loop d = {'key1':1,'key2':2} for j in d: print(j, end=" ") print(j+" "+ str(d[j])) Traceback (most recent call last): File "E:/Learning/Python/ComparisonAndControlFlow/ForLoop.py", line 22, in print(j+" "+ str(d[j])) TypeError: 'str' object is not callable*

stackoverflow.com/questions/22818189/… and here, martijn answering a blatant dupe :D
stackoverflow.com/questions/35077194/… cv pls unable to reproduce. I don't see how that code could produce that typeerror and how that answer addresses that at all
 
11:14 PM
that's an incorrect answer altogether, accepted, lol
that's 2 flies with one.
10 messages moved from Python
 

« first day (888 days earlier)      last day (2490 days later) »