- 2
An array of random numbers from n elements is given. Print the numbers in the index of which there is a number 2 .
Please help me, language Python
3 Answers
+ 5
If performance wasn't an issue, you can convert the index to string, and check whether it contains digit 2.
But I won't recommend this approach if the list has many elements. Maybe you can try to find an algorithm that does the job better.
from random import randint
lst = [ randint( 1, 22 ) for i in range( 100 ) ]
for index, value in enumerate( lst ):
if '2' in str( index ):
print( f"{index} {value}" )
+ 1
Ipang thank you so much for your help đđđ
0
You mean numbers at index 2, 12, 20, 21 ...