Skip to content
Advertisement

How can I iterate through two lists and compare the values of each in Python?

Suppose I have the following lists:

x = [0.5, 0.9, 0.1, 0.3, 0.6]

y = [0.4, 0.3, 0.6, 0.1, 0.9]

I want to write a function that iterates through the list and compares each value in order.

JavaScript
JavaScript

This gives me Type Error: 'float' object is not iterable'

Why is this?

I would expect the output to look like this: [1, 1, 0, 1, 0]

Is there anyway I could write this function such that if the “threshold” value was only one digit it would still work?

My current code to make the latter function is:

JavaScript
JavaScript

Yet this returns the Type Error: TypeError: '>=' not supported between instances of 'int' and 'list' when thresh is a list.

Advertisement

Answer

The problem is that you are comparing the value n from prob with the entire list thresh. What you need to do is to also iterate over thresh. Here is a small fix that will allow your code to work:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement