Concatenate two list into a new list - list

I have two list:
lis1=[['a','b','x'],['c','d','x']]
lis2=['1','2']
and i wont a new list insert elements lis2 in lis like
[[['a','b','c','x'],['1']],
[['c','d','x'],['2']]]
What can i do?
Append method don't work for it.

Related

I am trying to remove the last item of each list in a list of lists

I have a data structure which is a list of lists. I want to remove the last item of each list, preferably using a filter. How would i go about doing this?
Here is an example show how to do it:
map (\xs->if null xs then xs else init xs) [[], [1..3], [4..6]]
and the result is:
[[],[1,2],[4,5]]
Note that it need to check each list in list whether is empty before remove the last item of the list, since init cannot apply to empty list.

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(...).

Search for element list inside list in python

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 .

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...

Prolog Insert list as element into another List

In my program my P = [0,1,2] I want to store it into another LIST, because P will keep changing in a loop so I want to store P into a LIST, so my LIST will be like below :
eg.
LIST = [[0,1,2],[3,4,5],[6,7,8]]
create_list([],[]).
create_list(G, [H|G]).
This is what I did, create_list(P,LIST). I not sure how to do it as it keep return me no. But I am pretty sure I can get different P because I am able to print them out each time P changed.
You need to create a predicate that receives the item (list in this case) you want to append to another input list, and this would give you a new list with the which has all the items of your input list plus the new item.
So, it would be something like:
create_list(Item, List, [Item|List]).
Initially the input List would be an empty list ([]), so you might call it
create_list([0,1,2], [], List1),
create_list([3,4,5], List1, List2),
create_list([6,7,8], List2, List).
This will result in List instantiated with [[0,1,2],[3,4,5],[6,7,8]]