An array of random numbers from n elements is given. Print the numbers in the index of which there is a number 2 . | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
- 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

5th Apr 2022, 4:15 PM
Gulnur
3 Antworten
+ 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}" )
5th Apr 2022, 6:09 PM
Ipang
+ 1
Ipang thank you so much for your help 😭😭😭
5th Apr 2022, 6:52 PM
Gulnur
0
You mean numbers at index 2, 12, 20, 21 ...
5th Apr 2022, 4:22 PM
Ipang