I need to write a function that takes a number as a parameter and a list, checks each element in the list to locate where the number exists and returns the index.
For example:
func(1, [11, 14, 56, 71, 9])
I want the answer: "The number is located at: [0, 1, 3]"
If the number doesn't exist in the list, then just print a blank list []
CodePudding user response:
Since you're trying to figure out if the input number is a digit in any of the list numbers, the easiest thing is to convert both to strings and use the in
operator:
>>> def func(n, h):
... return [i for i, x in enumerate(h) if str(n) in str(x)]
...
>>> print(f"The number is located at: {func(1, [11, 14, 56, 71, 9])}")
The number is located at: [0, 1, 3]
CodePudding user response:
def find(x,L:list):
g = []
for i in L :
if str(x) in str(i):
g.append(L.index(i))
return g
print(find(1, [11, 14, 56, 71, 9]))