Traversing a list passing a closure to process elements - groovy - groovy

Which is the correct way from the below snippets?
1) def list = ["a", "b", "c", "d"]
list.findAll({println(it)})
2) def list = ["a", "b", "c", "d"]
list.collect({println(it)})
3) def list = ["a", "b", "c", "d"]
list.grep({println(it)})
Please advice.

If all you are doing is printing elements, you should use each.
def list = ["a", "b", "c", "d"]
list.each { println(it) }
findAll and grep are used to find specific elements in a list. collect is used to build a new collection from the elements in the given list.
So the correct way to print all the elements is using each, which is exactly what your example is. This is also likely the choice for when you don't care about the result of the processing. If you only want to process part of the list, use findAll or grep, which can filter the list before running the closure. These, along with collect, all return a new list.

Related

How to correctly arrange elements by the right indexes in haskell?

I have a problem. I wanted to write a function that would compare 2 sheets and if they are equal, put them on the same position, otherwise put any handicap sign on that position, such as "-".
I was thinking something like this
let l1 = ["a", "b", "c"]
let l2 = ["a", "d", "c"]
(l1', l2') = myFunc l1 l2
l1' == ["a", "b", "c", "-"]
l2' == ["a", "-", "c", "d"]
I just don't understand what the algorithm should be, that's the point, maybe I could implement it in Haskell, but I think it would be very ugly
You can use the Diff package for this. Example:
Data.Algorithm.Diff> getDiff "abc" "adc"
[Both 'a' 'a',First 'b',Second 'd',Both 'c' 'c']

Calculating the number of characters inside a list in python

I made a list with some character in it and I looped through it to calculate the number of a specific character and it returns the number of all the characters inside the list and not the one's that I said it to. Take a look at my code and if someone can help I will appreciate it!
This is the code:
array = ['a', 'b', 'c', 'a']
sum_of_as = 0
for i in array:
if str('a') in array:
sum_of_as += 1
print(f'''The number of a's in this array are {sum_of_as}''')
If you know the list is only ever going to contain single letter strings, as per your example, or if you are searching for a word in a list of words, then you can simply use
list_of_strings = ["a", "b,", "c", "d", "a"]
list_of_strings.count("a")
Be aware though that will not count things such us
l = ["ba", "a", "c"] where the response would be 1 as opposed to 2 when searching for a.
The below examples do account for this, so it really does depend on your data and use case.
list_of_strings = ["a", "b,", "c", "d", "ba"]
count = sum(string.count("a") for string in list_of_strings)
print(count)
>>> 2
The above iterates each element of the list and totals up (sums) the amount of times the letter "a" is found, using str.count()
str.count() is a method that returns the number of how many times the string you supply is found in that string you call the method on.
This is the equivalent of doing
count = 0
list_of_strings = ["a", "b,", "c", "d", "ba"]
for string in list_of_strings:
count += string.count("b")
print(count)
name = "david"
print(name.count("d"))
>>> 2
The if str('a') in array evaluates to True in every for-loop iteration, because there is 'a' in the array.
Try to change the code to if i == "a":
array = ["a", "b", "c", "a"]
sum_of_as = 0
for i in array:
if i == "a":
sum_of_as += 1
print(sum_of_as)
Prints:
2
OR:
Use list.count:
print(array.count("a"))

List of Lists of Lists of... to one List

How can I convert List of Lists of Lists into one List? For example I would like to make one list which contains all elements from all nested lists, i.e if I have:
l = [[["A", ["B"]], ["C", "D"]], [["E", "F"], ["A"]]]
then the results should be:
["A", "B", "C", "D", "E", "F", "A"]
This is perhaps not the most efficient, or most pythonic way:
def extract(a):
#recursive algorithm for extracting items from a list of lists and items
if type(a) is list:
l = []
for item in a:
l+=extract(item)
return l
else:
return [a]
Essentially what this does is check if the input is a list. If it is, we split it up into its elements, and run the function on those elements. When we get to a non-list, we return those, forming a complete list.
Another way of doing this would be using a global list to store each element. Again, this isn't the most efficient or pythonic way:
l = []
def extract(a):
#recursive algorithm for extracting items from a list of lists and items
if type(a) is list:
for item in a:
extract(item)
else:
global l
l.append(a)

how can I generate the list of elements of list with duplicated elements

for example if I have a list like this :
list =['a','a','a','b','b','b','c']
I want to know how many different elements are in my list and generate a list like this:
list1 = ['a','b','c']
set(list)
produces
>>> set(list)
{'b', 'a', 'c'}
If you then want it as a list you can use
list(set(list))
full_list = ["a", "b", "a", "c", "c"]
list_without_duplicaion = list(dict.fromkeys(full_list))
print(list_without_duplicaion)
Try out this answer and let me know is working as your expectations.
https://repl.it/#TamilselvanLaks/arrwithoutdup

How to get intersection of two lists in terraform?

I have two lists in terraform and want to get the intersection of these lists.
For example,
list1 = ["a", "b", "c"]
lists2 = ["b", "c", "d"]
I am looking to get output as ["b", "c"] using built-in terraform functions.
You are looking for something like this
output my_intersection {
value = setintersection( toset(var.list1), toset(var.list2) )
}

Resources