subtracting the previously used letters to num_letters

m
mjijena (280 points)
0 3 7
asked Oct 28, 2020 in HW2 required by mjijena (280 points)
I created a function which tells me how many letters a word contains and I assigned the variable " i " to " num_letters "

My problem is that now I want to subtract the number of letters of each word, obtained from my function, from " i " but it doesn't let me since " i " is an integer and I'm supposing the result from the function which tells me the letters in every word of a string is not an integer (I don't really know what it is)

I also tried using map, such as: i = i - map(function_of_letters, g1[0]) but when I run it it tells me " unsupported operand type(s) for -: 'int' and 'map' "

How could I convert the number of letters of each word into an integer so I can make the operation?
122 views

1 Answer

S
Silktrader (2550 points)
1 6 16
answered Oct 28, 2020 by Silktrader (2,550 points)
edited Oct 28, 2020 by Silktrader

... when I run it it tells me " unsupported operand type(s) for -: 'int' and 'map' "

`Map` returns its own object, an iterator — not a sequence, not a single value. The interpreter justifiably complains it can't subtract an iterator from an integer.

`Map` won't apply the function you specified until it is actually iterated. One way to do so would be to invoke the list constructor, such as:

list(map(custom_function, custom_sequence))

You'd be getting a new sequence though, not a single numeric value as the integer subtraction requires.


I'm supposing the result from the function which tells me the letters in every word of a string is not an integer (I don't really know what it is)

You should investigate what type you're getting so to understand the error. The IDE's hints, debugging or printing the type might help:

print(type(your_function(argument)))

You probably need to rewrite the function that tells you how many letters a word contains.

I am going to point out that a word, or string, is an immutable, ordered, sequence of characters ... so it has a length. You could perhaps count letters in a word with the built-in `len` function.