Reputation: 306
I am working with a list of lists and I want to match patterns in lists. For instance, I have shown a list of lists below.
x = [[['a', 5],
['b', 0.11],
['c', 0.85]]
[['a', 'b', 0.5],
['b', 'c', 1.0],
['c', 'a', 0.7]],
[['a', 'b', 'c', 0.9],
['b', 'c', 'a', 0.4],
['a', 'c', 'b', 1.0]]]
Now I want to give an input like ("a","b","c") then it should give me respective value ex. 0.9 in above list.
I tried to use
if ["a","b","c"] in x :
>>> False.
But it is not working that way. How can I solve this ? Please suggest any solution.
Aditya.
Upvotes: 0
Views: 1017
Reputation: 17156
Recursively searches regardless of levels of sublists
Since poster requires:
this list is dynamic and it can have more than 3 lists inside
def find(lst, t, result = None):
if not isinstance(t, list): # insure t is a list
t = [t]
if result is None: # Set default result
result = []
for sublist in lst: # outer list loop
if isinstance(sublist, list):
if sublist[:-1] == t:
result.append(sublist[-1])
return result
else:
find(sublist, t, result) # recursively check sublists
return result
x = [[['a', 5],
['b', 0.11],
['c', 0.85]],
[['a', 'b', 0.5],
['b', 'c', 1.0],
['c', 'a', 0.7]],
[['a', 'b', 'c', 0.9],
['b', 'c', 'a', 0.4],
['a', 'c', 'b', 1.0]],
[[
['a', 'a', 'c', 2], # additional level added
['x', 'a', 5]]]]
print(find(x, 'a')) # [5]
print(find(x, ['a', 'b'])) # [0.5]
print(find(x, ['a', 'b', 'c'])) # [0.9]
print(find(x, ['a', 'a', 'c'])) # [2]
Upvotes: 0
Reputation:
Try this code
x = [
[['a', 5],['b', 0.11],['c', 0.85]],
[['a', 'b', 0.5],['b', 'c', 1.0],['c', 'a', 0.7]],
[['a', 'b', 'c', 0.9],['b', 'c', 'a', 0.4],['a', 'c', 'b', 1.0]]]
checklist = ['a', 'b', 'c']
return_value = [j[-1] for i in x for j in i if j[:-1] == checklist][0]
print(return_value)
>> 0.9
with this code, you can find any pattern that exists by changing checklist like checklist = ['a']
output will be 5
Upvotes: 2
Reputation: 12515
x = [
[
['a', 5],
['b', 0.11],
['c', 0.85]
],
[
['a', 'b', 0.5],
['b', 'c', 1.0],
['c', 'a', 0.7]
],
[
['a', 'b', 'c', 0.9],
['b', 'c', 'a', 0.4],
['a', 'c', 'b', 1.0]
]
]
# Define search term as string
search = 'abc'
desired_value = None
for collection in x:
for row in collection:
# convert everything but the last element in each row to a string for easy comparison
key = ''.join(row[:-1])
# if you've found a key matching your search criterion, grab the last value for that row
if search == key:
desired_value = row[-1]
break
Upvotes: 0
Reputation: 54148
The first thought would be to check that all elements of the input are in the subsublist, like this
value = ['a', 'b', 'c']
for sublist in x:
for part in sublist:
if all(v in part for v in value):
print(part)
Or setting the value as a set
you can check issubset
value = {'a', 'b', 'c'}
for sublist in x:
for part in sublist:
if value.issubset(set(part)):
print(part)
But it doesn't look at the order, as all are string you could simply concat the elements in part and check if abc
is in
value = 'abc'
for sublist in x:
for part in sublist:
if value in ''.join(map(str, part)):
print(part)
Upvotes: 0
Reputation: 150735
Lists are not hashable, you need to use tuple
:
xx = {tuple(a[:-1]):a[-1] for b in x for a in b}
if ('a','b','c') in xx: print(xx[('a','b','c')])
# 0.9
Upvotes: 2