Term for sublist of all elements except last? - list

The sublist, containing all elements, except first is called "tail".
What is the name of sublist of all elements, except first?

Without context, tail is the end of a list. Head is the beginning of a list. Your clarification of "all except the first" must depend on the environment in which you are working. Tail can refer to any number of elements at the end of a list.

Related

How do I remove duplicate words from a list in python

I have the following list:
Liste=['hello','hello word','word','red','red apple','apple','king']
and I want to remove the duplicate words that include in other words like 'hello' and 'word','red', and 'apple'
so the ResultsList will be like this:['hello word', 'red apple','king']
i tried a few methods but didn't work for me!
So anyone can help with a simple solution to my problem?
myList = ['hello','hello word','word','red','red apple','apple','king']
newList = []
for item in myList:
unique = True
current = myList.pop()
for string in myList:
if current in string:
unique = False
if unique:
newList.append(current)
myList.insert(0, current)
print(newList)
Loop through your list, each iteration pops the last element from the list. Thereafter loop through the remaining elements and evaluate if the string we popped is a substring of any of the other remaining strings.
If not, we consider the string we popped unique and we can append it to an empty list. At the end of each loop iteration insert the string we popped to the beginning of the original list.
set() would work if you were looking to remove exact duplicates and not substrings.

Combining elements of a sublist of a list

I have the following list of sublists
[[1;5;10];
[2;6;11];
[3;7;12]];
I am trying to a create the following list of sublists:
[[1;2;3];
[5;6;7];
[10;11;12]]
The first sublist of the result should containt the first element of each original sublist, second result sublist should contian the second elements of each of the original sublists and so on.
Each sublist contains the same number of elements as the other sublists. The amount of sublists is at least 2.
I was thinking of using List.map but I am not sure what function to apply to each sublist to exctract the needed elements.
This is what I have so far:
let rec compute list =
List.map (fun x -> ) list
Any suggestions are appreciated!
Here you need two recursions (as you would need 2 imbricated loops in an imperative language).
The first recursion should allow you to go through the inputs line, say from 1 to 3, and at each step of this recursion, you will need a second recursion,to go along the full row.
You can either do it all by hand or you can use List.fold_left. (I would use fold for the inner recursion.

How to get list elements by matching string using scala?

I have following list-
List((name1,233,33),(name2,333,22),(name3,444,55),())
I have another string which I want to match with list and get matched elements from list.
There will be only one element in list that matches to given string.
The list may contains some empty elements as given as last element in above list.
Suppose I am maching string 'name2' which will occurs only once in the list, then
My expected output is -
List(name2,333,22)
How do I find matching list element using scala??
.find(_._1 == name2)
will be better
Consider collect over the tuples list, for instance like this,
val a = List(("name1",233,33),("name2",333,22),("name3",444,55),())
Then
a collect {
case v # ("name2",_,_) => v
}
If you want only the first occurrence, use collectFirst. This partial function ignores tuples that do not include 3 items.

Insert element last into list

How do you insert an element (in this case, a char) into a list so it ends up as the last element?
For example, say that I want to add #"D" as the last element in the list [#"A, #"B", #"C"] so I then would have [#"A, #"B", #"C", #"D"].
(This should also work for inserting a string containing more than one element: adding [#"D", #"E"] should give [#"A, #"B", #"C", #"D", #"E"].)
Just append it to the end using "#".
[#"A", #"B", #"C"] # [#"D"]

Can someone please explain to me how the pipe "|" character works in Prolog?

I have this code:
[a,b,[]]=[First,Second,Third|Fourth].
and it gives me the following output:
First = a, Second = b, Third = Fourth, Fourth = [].
I'd like to know how Third got assigned to Fourth.
The pipe character is very similar to the "consing dot" in Lisp. The variable after the pipe takes the entire remainder of the corresponding list.
So, here we expect Third to bind to the explicitly given empty list in the data. But there is nothing after that and so Fourth also binds empty.
Third being bound to Fourth is just an indirect way of Third being bound to empty.
See my answer here: https://stackoverflow.com/a/7559044/467473 for details on how Prolog lists are implemented.
Basically, a prolog list is a simple data structure. The empty list is denoted by the atom []. A non-empty list is the structure ./2. The left argument in this structure is the head of the list; the right argument is the tail of the list, which is another list (either the empty list [] or a non-empty list (./2).
The friendly list notation is just syntactic sugar on top of this. The expression [H|T] is exactly the same as the expression .(H,T). The expression [a,b|T] is exactly the same as .(a,.(b,T)). And the expression [a,b,c] is exactly the same as .(a,.(b,.(c,[]))).