I have a list. I want to convert it to string. However, the results are a bit different from what I expected.
Expected result:
'["a", "b", "c", "d"]'
Actual result:
[a, b, c, d]
My code:
final List list = ["a", "b", "c", "d"];
print(list.toString());
How can I get my expected results? I would appreciate any help. Thank you in advance!
You could use the jsonEncode of it to get the desired result:
print(jsonEncode(list));
You can simply do this by using json.encode() function:
final list = ['a', 'b', 'c', 'd'];
final result = json.encode(list);
You can try jsonEncode:
final List list = ["a", "b", "c", "d"];
print(jsonEncode(list));
Output:
["a","b","c","d"]
Related
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']
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"))
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
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) )
}
When I read specific lines from a .txt file, I get a string like this one:
"Test,Test2,Test3,Test4,Test5,Test6"
I want to convert this string so it can fill a list, which looks like this:
List = [A, B, C, D, E, F]
Inserting values in such list can be done like this, for example:
["A", "B", "C", "D", "E", "F"]
But when I try to insert the string from the file, it ends up being stored only in the A variable, as the content is not being split. The other variables don't get the expected values.
What I'm getting:
List = ["Test,Test2,Test3,Test4,Test5,Test6", "B", "C", "D", "E", "F"]
What I want:
List = ["Test", "Test2", "Test3", "Test4", "Test5", "Test6"]
So basically I'm asking help with splitting a string to separate values by a certain char in Erlang!
Thanks for any help!
There are two built-in ways to split strings in erlang: string:tokens/2 and re:split/2,3.
For instance, using string:tokens:
Line = "Test,Test2,Test3,Test4,Test5,Test6",
[A, B, C, D, E, F] = string:tokens(Line, ",").
Using re:split:
Line = "Test,Test2,Test3,Test4,Test5,Test6",
[A, B, C, D, E, F] = re:split(Line, ",")