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.
Related
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.
I'm trying to do a basic grab from a Text Column in sqlite and processing it to a field in a model that is a List<String>. If data is empty then I want to set it as an empty list []. However, when I do this for some reason I get a list that looks empty but really with a length of 1. To recreate this issue I simplified the issue with the following.
String stringList = '';
List<String> aList = [];
aList = stringList.split(',');
print(aList.length);
Why does this print 1? Shouldn't it return 0 since there are no values with a comma in it?
This should print 1.
When you split a string on commas, you are finding all the positions of commas in the string, then returning a list of the strings around those. That includes strings before the first comman and after the last comma.
In the case where the input contains no commas, you still find the initial string.
If your example had been:
String input = "451";
List<String> parts = input.split(",");
prtin(parts.length);
you would probably expect parts to be the list ["451"]. That is also what happens here because the split function doesn't distinguish empty parts from non-empty.
If a string does contain a comma, say the string ",", you get two parts when splitting, in this case two empty parts. In general, you get n+1 parts for a string containing n matches of the split pattern.
I am working in Python 2.7
I am trying to iterate over 2 lists, of un-equal length, and I want to create a new list, containing the matching elements (same elements in the same position), and when the elements do not match, I need to have some text as well as the position of the miss-matching elements.
list1=[1,2,3,4]
list2=[1,2,3,5,6]
This outputs the matches
match=[[b] for a, b in zip(list1, list2) if a==b]
result:
[1,2,3]
But I do not know, in a one-liner, how to also flag the mis-matches:
[1,2,3,"nomatch-pos4"]
or
[1,2,3,"nomatch-pos4","nomatch-pos5"]
It does not matter if it will iterate over the maximum or minimum of the 2 list lengths.
it first find the minimum of the two lists and iterate over the shorter list and check if an element in the list matches with other list in same position. check below code:
match = [list1[i] if list1[i] == list2[i] else 'nomatch-pos'+str(i+1) for i in range(0,min(len(list1),len(list2)))]
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"]
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,[]))).