What is the shortest way to initiate nested lists with some values in Robot Framework?
Something like:
myList = new List (new List (1, 2 , 3), new List (a, b, c))
You can use the Evaluate keyword. You can then use normal python syntax to define the list.
*** Test Cases ***
Example
${myList}= Evaluate [[1,2,3], ['a', 'b', 'c']]
should be equal ${myList[1][2]} c
Related
Below are my 2 lists:
a: ['a0', 'a3']
b: ['b0', 'b1', 'b0', 'b1']
I need to merge the above 2 list as below:
final: ['a0b0', 'a0b1', 'a3b0', 'a3b1']
Need to write a for loop which will check for "0" in list b and then start with the next element in list a.
Thanks,
If I understand the rules, you need to combine two lists such as:
items from both lists will be combined, items from the first list will come first (e.g. "a0" and "b1" will become "a0b1")
there could be repeating items in both lists but you don't want these repetitions to be present in the final list
I think Python is your best friend here, I'd propose this piece of code as a custom library:
Libraries/ListUtils.py
def combine_lists(list1, list2):
set1 = set(list1)
set2 = set(list2)
final_list = []
for i1 in set1:
for i2 in set2:
final_list.append("{}{}".format(i1, i2))
return final_list
and an example test case:
*** Settings ***
Library ../Libraries/ListUtils.py
Library Collections
*** Test Cases ***
Combine Lists
${list1}= Create List a0 a3
${list2}= Create List b0 b1 b0 b1
${final_list}= Combine Lists ${list1} ${list2}
Log To Console ${final_list}
This will log into console:
['a3b1', 'a3b0', 'a0b1', 'a0b0']
which is the output you specified in your question. It's not ordered in any way, you might want to add this to it.
Also note that there's Combine Lists keyword in Collections library (but this one does something else than you want). As a result of that, my example will produce one warning:
[ WARN ] Keyword 'Combine Lists' found both from a custom test library 'ListUtils' and a standard library 'Collections'. The custom keyword is used. To select explicitly, and to get rid of this warning, use either 'ListUtils.Combine Lists' or 'Collections.Combine Lists'.
So you might want to pay more attention to naming conventions, or call keywords by their full name like e.g. Collections.Combine Lists.
let's say I have a list defined in Kotlin:
val mylist = mutableListOf<List<Int>>(listOf(2,3,5), listOf(2,5,6))
Now, I want to assign a certain value to one of these sublists. For example, now that I have a list of
((2,3,5)(2,5,6))
I would like my list to be
((2,3,5)(2,100,6))
I'm used to doing this in Python by something like myList[1][1] = 100. How do I achieve the same result in Kotlin?
Kotlin has two sets of collection interfaces, the regular List, Set, etc. which are read-only, and the same ones with the Mutable prefix, which can be modified.
listOf will give you a List instance, while mutableListOf gives you a MutableList instance. If you use the latter for creating your nested lists, you can use the exact syntax you've asked about:
val mylist: MutableList<MutableList<Int>> = mutableListOf(mutableListOf(2,3,5), mutableListOf(2,5,6))
mylist[1][1] = 100
println(mylist) // [[2, 3, 5], [2, 100, 6]]
(I've added the explicit type for myList for clarity's sake, it can be omitted from the left side of the assignment.)
Hi everyone I'm trying to learn Prolog on my own and I am stuck trying to create a function that takes a nested list and returns a list of only the unique numbers. So unique(L,T) means that T is a list of unique numbers extracted from L.
The format of the lists look like this where c is always followed by an int:
[a,[b,[c,5],[c,3]],[c,4]]
Example of what should return as true:
unique([b,[c,4],[c,3]], [4,3])
This is the code I have tried however it is just returning an empty list when i try to query it:
unique([],[]).
unique([X|XS],[X|L]) :-
integer(X),
unique(XS,L).
unique([X|XS],L) :-
\+integer(X),
unique(XS,L).
I have tried various other solutions as well and seem to keep getting an empty list as output or just 'false'. I would appreciate any sort of guidance on this!
This code does not take into account that the lists are nested; it should succeed on (for example):
unique([b,c,4,c,3],[4,3]).
You'll need a case where the head of the list is a list, recursively find the integers in that, and append what you find to what you find for the tail of the list.
Flatten the list, filter it to get the numbers, sort to make them unique:
?- L = [a,[b,[c,5],[c,3]],[c,4]], flatten(L, F), include(number, F, N), sort(N, Unique_numbers).
L = [a, [b, [c, 5], [c, 3]], [c, 4]],
F = [a, b, c, 5, c, 3, c, 4],
N = [5, 3, 4],
Unique_numbers = [3, 4, 5].
If you want to keep the original order you can't sort, but there are many answers on SO that show you how to do it.
If you don't want to use the library predicates, look up how they are defined (flatten/2 and include/2) and get inspiration for your own solution.
I have a list val l=List(4,3,2,1), I am trying to generate a list of tuples of the format (4,3), (4,2) and so on.
Here's what I have so far:
for (i1<-0 to l.length-1;i2<-i1+1 to l.length-1) yield (l(i1),l(i2))
The output is : Vector((4,3), (4,2), (4,1), (3,2), (3,1), (2,1))
Two questions:
It generates a Vector, not a List. How are these two different?
Is this the idiomatic scala way of doing this? I am very new to Scala, so it's important to me that I learn right.
On the first part of the question, the for comprehension implementation defines ranges 0 to l.length-1 and i1+1 to l.length-1 as IndexedSeq[Int] hence the yielded type is trait IndexedSeq[(Int, Int)] implemented by final class Vector.
On the second part, your approach is valid, yet consider the following where we do not use indexed references to the lists,
for (List(a,b,_*) <- xs.combinations(2).toList) yield (a,b)
Note that
xs.combinations(2).toList
List(List(4, 3), List(4, 2), List(4, 1), List(3, 2), List(3, 1), List(2, 1))
and so with List(a,b,_*) we pattern-match and extract the first two elements of each nested list (the _* indicates to ignore possible additional elements). Since the iteration is over a list, the for comprehension yields a list of duples.
Let me try to put this in a simple manner.
I have 2 lists, which look like below:
List1 = [('a', 1, 'low'), ('b', 10, 'high')] # --> Tuples in List
List2 = ["('a', 1, 'low')", "('b', 10, 'high')"] # --> Here the Tuples are actually of Type String.
List1 is output of a SQL query. List2 is defined by me as expected result.
I am using Robot Framework to compare these two lists with the Keyword Lists Should Be Equal. But it fails as List2 has strings which look like Tuple.
How can I compare these two lists? Can I convert both the lists to a different variable type so that I can compare them. I am trying to avoid the python coding here.
It's unclear exactly what your data looks like, but since the two lists have different contents you'll have to convert one or both of them to a common format.
You could, for example, convert the first list to a string with something like this:
| ${actual}= | Evaluate | [str(x) for x in ${List1}]
I doubt that gives you exactly what you need because, again, it's unclear exactly what you need. However, the technique remains the same: use Evaluate to write a little bit of python code to convert one of your lists to be the same format as the other list before doing the compare.
This may be the long procedure, i have used tuples (1,2) instead (a,1,low) #( cause name error in python). But you told its getting from SQL. Important is difference between (1,2) and (1, 2) #(space mismatch)
var.py
List1 = [(1,2), (3,4)]
test.robot(txt file)
*** Settings ***
Library BuiltIn
Library Collections
Variables var.py
Library String
*** Variables ***
#{appnd_li}
*** Test Cases ***
TEST
#constructing List2=["(1, 2)","(3, 4)"]
${List2}= Create List (1, 2) (3, 4)
# importing List1 from variable file
${len}= Get Length ${List1}
#initialize empty list
${li}= Create List #{appnd_li}
:FOR ${I} IN RANGE 0 ${len}
\ ${item}= Convert To String ${List1[${I}]}
\ Append To List ${li} ${item}
Lists Should Be Equal ${li} ${List2}
~