I'm a beginner with Python. Say I have a list of lists in python
list1 = [['id1','Jane','Doe',100,75,100],['id2','John','Snow',90,87,92],['id3','Peter','Pan',79,81,83]]
How can I search the list of lists for say 'id2' and print a list with only the integers in its list?
This is what I tried
import numbers
def list_search(lister,index):
for i in lister:
for j in i:
if j == index:
[x for x in i if isinstance(x, numbers.Number)]
print("Not found: ",index)
Here is the Test for my function
list_search(list1,'id2')
I was expecting [90,87,92] but I got Not found: id2
CodePudding user response:
This line doesn't do anything:
[x for x in i if isinstance(x, numbers.Number)]
You should either print it, or return it. Plus your function will always shows the message Not found
so your code should be like this:
import numbers
list1 = [['id1','Jane','Doe',100,75,100],['id2','John','Snow',90,87,92],['id3','Peter','Pan',79,81,83]]
def list_search(lister,index):
for i in lister:
for j in i:
if j == index:
return [x for x in i if isinstance(x, numbers.Number)]
print("Not found: ",index)
my_list = list_search(list1,'id2')
print(my_list)
# print average
print( sum(my_list) / len(my_list))
Output:
[90, 87, 92]
89.66666666666667
CodePudding user response:
This is the function that you can write. First look all the first elements and if the first element is equal to what you want then check for integers and append them to the result list. Finally return the result list.
def func(a, activatedString):
result = []
for i in a:
if i[0] == activatedString:
for j in i:
if isinstance(i, int):
result.append(j)
return result
But if you want more short way you can do this:
def func(list1, activatedString):
return [i for i in [a for a in list1 if a[0] == activatedString][0] if isinstance(i, int)]
print(func(list1, "id2"))
CodePudding user response:
This solution stops looping when index
is found.
Returns None
if index
has not been found.
Uses a list-comprehension to easily create a list.
No need to import Number
just test if it's an integer.
A small optimization consists to look for integers starting from the 2nd row (item[:1]
) as we know that the first row is the index.
You could even replace 1 by 3 here if you assume that rows 2 and 3 (Jane, Doe) are always string.
def list_search(lister, index):
for item in lister:
if item[0] == index:
return [x for x in item[1:] if isinstance(x, int)]
return None
That's for the first part that provides you with the integers. Compute the average given the list is the easiest part.
numbers = list_search(list1, "id2")
print(sum(numbers)/len(numbers))
CodePudding user response:
Lets start with getting only integers
list:
list1 = [['id1','Jane','Doe',100,75,100],['id2','John','Snow',90,87,92],['id3','Peter','Pan',79,81,83]]
temp_integers = [list(filter(lambda x: isinstance(x, int), list1[i])) for i in range(len(list1))]
Output:
>>> temp_integers
... [[100, 75, 100], [90, 87, 92], [79, 81, 83]]
Then we add a list of their respective means
:
temp_means=[np.mean(x) for x in temp_integers]
Output:
>>> temp_means
... [91.66666666666667, 89.66666666666667, 81.0]
Then print corresponding integers to id2
and their mean
:
for i in range(len(list1)):
if 'id2' in list1[i]:
print(temp_integers[i])
print(temp_means[i])
Put it all in one function
def list_search(lister,index):
temp_integers = [list(filter(lambda x: isinstance(x, int), lister[i])) for i in range(len(lister))]
temp_means=[np.mean(x) for x in temp_integers]
if all(index not in y for y in lister):
print("Not found: ",index)
else:
for i in range(len(lister)):
if index in lister[i]:
print(temp_integers[i])
print(temp_means[i])
Output
>>> list_search(list1,'id2')
... [90, 87, 92]
... 89.66666666666667
CodePudding user response:
One method using Try except
list1 = [['id1','Jane','Doe',100,75,100],['id2','John','Snow',90,87,92],['id3','Peter','Pan',79,81,83]]
def list_search(list_of_list,index):
result=[]
for lis in list_of_list:
if lis[0]==index:
for check in lis:
try:
result.append(int(check))
except ValueError:
continue
return sum(result)/len(result) if result else 0
print(list_search(list1,"id2"))
Output:-
207.6666666
CodePudding user response:
You can use list comprehension for this:
list1 = [['id1','Jane','Doe',100,75,100],['id2','John','Snow',90,87,92],['id3','Peter','Pan',79,81,83]]
def list_search(lister, index):
return [j for i in lister for j in i if isinstance(j, int) and i[0] == index]
results = list_search(list1,'id2')
print(results)
average = sum(results)/len(results)
print(average)
# Output:
# [90, 87, 92]
# 89.66666666666667