Abdul Haseeb
Abdul Haseeb

Reputation: 13

How to find how many number contain a certain say 7) integer in a list from 1 to 9999

nums = [i for i in range(1,10000)]

b=[x for x in nums if '7' in x ]

print (b)

TypeError: argument of type 'int' is not iterable

Upvotes: 1

Views: 265

Answers (3)

Robin Sage
Robin Sage

Reputation: 959

nums = [i for i in range(1,100)]

b=[x for x in str(nums) if '7' in x]

print(f'There are {len(b)} numbers that contain the number 7.')

Upvotes: 1

Ashok Khoja
Ashok Khoja

Reputation: 11

nums = [i for i in range(1,10000)]

b=[x for x in nums if '7' in str(x) ]

print (len(b))
print(b)

Upvotes: 1

ThePyGuy
ThePyGuy

Reputation: 18426

Create a Boolean generator for the given condition, and call sum on it, you will get the number of integers that evaluates to be True for the given condition.

>>>sum('7' in str(i) for i in range(1,10000))
3439

Upvotes: 2

Related Questions