[Tutor] Counting Items on a List associated w/ Index #
Rafael Knuth
rafael.knuth at gmail.com
Fri Aug 3 07:14:11 EDT 2018
Hi there,
I wrote a function which is supposed to count the number of items on
each index #.
For starters, here's a list I created to test the function:
properties = ["mansion, modern, swimming_pool" , "mansion, historic,
air_conditioning", "penthouse, modern, whirlpool"]
# index 0 = property type
# index 1 = style
# index 2 = feature
Here's the function:
def feature_counter(input_lst, index, input_str):
num_elt = 0
output_lst = []
for row in input_lst:
split_row = row.split(",")
output_lst.append(split_row)
for each in output_lst:
if each[index] == input_str:
num_elt = num_elt + 1
return num_elt
This function returns the correct result when checking index 0:
print(feature_counter(properties, 0, "mansion"))
>>> 2
Weirdly, it returns the wrong result when checking index 1:
print(feature_counter(properties, 1, "modern"))
>>> 0
and index 2:
print(feature_counter(properties, 2, "swimming_pool"))
>>> 0
Can anyone advise what's wrong with my function?
Thanks,
Rafael
More information about the Tutor
mailing list