How to fix? 'int' object is not subscriptable
📁 Advanced Python Topics
Here's the task: Generate 20 random integers in the range from -5 to 4, write them into array cells. Count how many of them are positive, negative, and zero values. Display the array elements and the counted quantities. Here's the error text:
a[i]=int(a[i])TypeError: 'int' object is not subscriptable.How to fix this? Thank you very much!
import randoma=[]for i in range(20): a=random.randint(-5,4
You can use the itertools module:
from itertools import groupbyx = ['a', 'a', 'a', 'f', 'h', 'k', 'k']new_x = [el for el, _ in groupby(x)]print(new_x) # ['a', 'f', 'h', 'k']This approach is better than using set because itertools.groupby preserves the order in which elements appeared in the original sequence. While set does not guarantee order preservation.
Log in to leave an answer
Blogs