Search for element list inside list in python - list

My objective is to search for the word after "IDENTIFIER" in the list. I have input as list which can have list inside list inside list or just list inside list. Basically we have to find how many list inside lists are present and then perform the check.
I have lists inside lists as shown below
[['CLASS', 'class'], ['CLASS', 'animal'], ['LCURLY', '{'], [[['INT', 'int'], ['IDENTIFIER', 'a'], ['SEMICOLON', ';']], [['BOOL', 'bool'], ['IDENTIFIER', 'x'], ['SEMICOLON', ';']]], ['RCURLY', '}']]
I want to search for next element after the word IDENTIFIER. that is i am looking for "a" , and "x". This is case of list inside list inside list. That is two groups of nested list.
But I also have a list as follows:
[['INT', 'int'], ['IDENTIFIER', 'e'], ['SEMICOLON', ';']]
This is case of only list inside list. Now i want to search for word after IDENTIFIER that is "e".
So, Basically all I want to know how to search if there is list inside list and if there is no list inside list then perform a check. All I could think is write a function where i would pass my list and then check if there exist a list inside the list if there is still list then call the function again ( recursively ) and once there is no such list present then perform the check.
Is this the right way to go ahead ? Are there any better suggestions than this .

Related

How do I output a specific item in a list when inputted a number that corresponds to that item? [Indexing?]

Number,IceCream
1,Chocolate
2,Vanilla
3,Mixed
Say if I
Number = input("Flavor?:")
I know that I need to index [0] because the numbers are on the first column. I also know that I will need to use .split(",") to remove the commas and to create a list.
Some assistance would be greatly appreciated!
It's confusing whether you plan to include integers in the list with the strings or not
Method 1: including integers with strings(flavor), create a list of tuples
icecream=[(1,'choc'),(2,'mix'),(3,'blueberry')]
print(icecream[0][1])
print(icecream[2][1])
Note: tuples are immutable
Method 2: I believe the best way to do this would be to use a dictionary instead of list. As dictionary has (Key, value) pairs, you could assign key(integer) to values(flavor), which then would make it easy accessing items just by keys(integers in your case) ex.
Ice_cream_flavors={1:"chocolate", 2:"vanilla", 3:"mixed"} #dictionary
to access values, you could use methods available in dictionary use get(), items()
Note: items() returns a tuple for each key,value pair.
ex.
Ice_cream_flavors={1:"chocolate", 2:"vanilla", 3:"mixed"}
new=Ice_cream_flavors.items()
for k,v in new:
if input==k:
print(v)

How do I get elements from list of lists based on a index value using python?

I have lists of lists like this :
Input_list= [['a','b'],[1,6],[1,13.8]]
I have a dictionary
dictny={'a':'mdl/a','b':'mdl/b'}
I am trying to find whether keys present in dictny will be present in Input_list or not by using following code:
Key_list=[]
for key,value in dictny.iteritems():
Key_list.append(key)
Here I am appending all keys into a separate list.
for key in Key_list:
for indx,list in enumerate(Input_list):
for element_indx,element in enumerate(list):
if element == key:
print "matching key",element
print "element_index",element_indx
Here I am trying to find whether my key is present in Input_list or not and I am trying to catch the index of the matching element. Now I want to use this index and to extract corresponding elements from sublist of Input_list based on the element_indx. How can I achieve this?

List Manipulation in prolog

Given a list such that [[%,A,B,C,%D,E,%],[%,F,G,%,H,I,J,%]] how do I return [[A,B,C],[D,E],[F,G],[H,I,J]]
I tried the following code:
filtertolists([],[]).
filtertolists([Head|Tail],Y):-
(Head = '%' ->
filtertolists(Tail,Y)).
I want to get the first element from the list of lists, and check whether members of the list are equal to '%' and start creating the list to be given as output, but after getting the head how do I check each element in it?
I would split the task into two tasks:
iterating over the list of lists
removing the elements of one list
My resulting sourceocde would look like this:
filtertolists([],[]).
filtertolists([List|Tail], [FilteredList|FilteredTail]):-
filterList(List, FilteredList),
filtertolists(Tail, FilteredTail).
filterList([], []).
% your filter code for one list goes here .
filterList(...).
filterList(...).

Netlogo: Retrieve position of a nested list given an element of the nested list

I want to retrieve the position of a nested list given an element of the nested list. Is this possible without traversing through all the nested lists?
For example, given a list of lists, [[1.1 a b][1.2 c d][1.3 e f]] I'd like to get the position 1 given element 1.2 .
This is similar to what was asked in this question: NetLogo : How to do multiple operations on lists (find, get , replace, remove , search elements within lists , ....)
In your particular case, you can use a combination of map to extract the sublist element you're looking for and position to get its index:
observer> show position 1.2 map first [[1.1 "a" "b"][1.2 "c" "d"][1.3 "e" "f"]]
observer: 1

How to check and change the value of a list?

I would like to check all list values in a list and change them if necessary.
p.e.
I want to check the next lists if there are values higher or lower then the next values:
min-value = 6
max-value = 22
mylist = ['4-8','25','16-19','21-32']
if one of the list values is below the min-value or higher then the max-value, the list values must be changed to the min-value and max-value. p.e. in example, the new list must be:
mylist = ['6-8','22','16-19','21-22']
if the entire value of the list item is below the min-value or higher then the max-value the list item can be removed.
How can I check my list values and change them?
There are two approaches. In the procedural one, you iterate over the list items and modify or skip the element:
let newlist = []
for element in mylist
" Parse element.
if ! OutsideBounds(element)
call add(newlist, AdjustBounds(element))
endif
endfor
In the functional programming approach, you use the built-in map() to modify elements (i.e. adjust the bounds), but that one cannot remove elements. So just empty those elements and then do a second pass with filter() to remove them. Note that both functions modify the original lists, so use copy() if you need to keep the original.
call filter(map(mylist, 'AdjustBounds(v:val)'), '! OutsideBounds(v:val)')
I hope I don't need to tell you how to write the AdjustBounds() and OutsideBounds() functions...