Python - Typeerror: 'Int' Object Is Not Iterable

Python - TypeError: 'int' object is not iterable

Your problem is with this line:

number4 = list(cow[n])

It tries to take cow[n], which returns an integer, and make it a list. This doesn't work, as demonstrated below:

>>> a = 1
>>> list(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
>>>

Perhaps you meant to put cow[n] inside a list:

number4 = [cow[n]]

See a demonstration below:

>>> a = 1
>>> [a]
[1]
>>>

Also, I wanted to address two things:

  1. Your while-statement is missing a : at the end.
  2. It is considered very dangerous to use input like that, since it evaluates its input as real Python code. It would be better here to use raw_input and then convert the input to an integer with int.

To split up the digits and then add them like you want, I would first make the number a string. Then, since strings are iterable, you can use sum:

>>> a = 137
>>> a = str(a)
>>> # This way is more common and preferred
>>> sum(int(x) for x in a)
11
>>> # But this also works
>>> sum(map(int, a))
11
>>>

Why am I getting TypeError: 'int' object is not iterable

To figure this out, look at what line the error occurs on:

Traceback (most recent call last):
File "main.py", line 15, in <module>
sonuc=(list(reduce(toplama,list(ciftler))))
TypeError: 'int' object is not iterable

It shows the line where the error occurs, but there are several things happening on that line, so we can break it up into separate parts:

ciftler_list = list(ciftler)
reduced_value = reduce(toplama, ciftler_list)
sonuc = list(reduced_value)

And now the error changes:

Traceback (most recent call last):
File "main.py", line 18, in <module>
sonuc = list(reduced_value)
TypeError: 'int' object is not iterable

The error is saying that list is trying to iterate a value, but that value is an int, so it can't. That means reduced_value must be an int. So, the reduce function must be returning an int.

And this makes sense, because your reduce function toplama is returning a + b, so reduce is going to return the sum of all the values in ciftler. Its return value is an int, not a list, so you can't call list() on it.

You probably want the assignment to look like this:

# ciftler is already a list, don't need to call list() again
sonuc = reduce(toplama, ciftler)

TypeError: 'int' object is not iterable Python?

Integers aren't sequences - they're just atomic things, so you can't iterate over them. It looks like you're trying to apply a string function to each digit - you'll need to convert code to a string (i.e. str(code)).

Python challenge TypeError: 'int' object is not iterable

You are trying to iterate over the integer i in the for loop below:

for i in range(1000,3001):
l = [str(x) for x in i] # the error line

If you want to iterate over the string representation of that integer, use this instead:

for i in range(1000,3001):
l = [x for x in str(i)] # the error line

getting 'int' object is not iterable error

On line 12, instead of writing keyboard.write(numb), write keyboard.write(str(numb)). This changes numb which is an int type to a str type before writing it.

How do I fix TypeError: 'int' object is not iterable?

When you wrote

for number in students:

your intention was, “run this block of code students times, where students is the value I just entered.” But in Python, the thing you pass to a for statement needs to be some kind of iterable object. In this case, what you want is just a range statement. This will generate a list of numbers, and iterating through these will allow your for loop to execute the right number of times:

for number in range(students):
# do stuff

Under the hood, the range just generates a list of sequential numbers:

>>> range(5)
[0, 1, 2, 3, 4]

In your case, it doesn't really matter what the numbers are; the following two for statements would do the same thing:

for number in range(5):

for number in [1, 3, 97, 4, -32768]:

But using the range version is considered more idiomatic and is more convenient if you need to alter some kind of list in your loop (which is probably what you're going to need to do later).

UDF: 'TypeError: 'int' object is not iterable'

It's because your tokensToSearchIn is integer, while it should be string. The following works:

def getVectors(searchTermsToProcessWithTokens): 
def addVectors(tokensToSearchFor: str, tokensToSearchIn: str):
tokensToSearchFor = [1 if token in str(tokensToSearchIn) else 0 for token in tokensToSearchFor]
return tokensToSearchFor
addVectorsUdf = udf(addVectors, ArrayType(StringType()))

TokenizedSearchTerm = searchTermsToProcessWithTokens \
.withColumn("search_term_vector", addVectorsUdf(col("name"), col("age"))) \
.withColumn("keyword_text_vector", addVectorsUdf(col("name"), col("age")))

return TokenizedSearchTerm

For the sake of curiosity, you don't need a UDF. But it doesn't look much simpler...

def getVectors(searchTermsToProcessWithTokens): 
def addVectors(tokensToSearchFor: str, tokensToSearchIn: str):
def arr(s: str):
return F.split(F.col(s), '(?!$)')
return transform(arr(tokensToSearchFor), lambda token: when(array_contains(arr(tokensToSearchIn), token), 1).otherwise(0))

TokenizedSearchTerm = searchTermsToProcessWithTokens \
.withColumn("search_term_vector", addVectors("name", "age")) \
.withColumn("keyword_text_vector", addVectors("name", "age"))

return TokenizedSearchTerm


Related Topics



Leave a reply



Submit