I want to remove '202103' in a list - list

I have this list :
['20210301', '20210302', '20210303']
and i want to remove '202103' to have this list :
['01', '02', '02'].
I haven't found the right solution. Can you please help me?
Thank you.

Is this your expected result?
lst = ['20210301', '20210302', '20210303']
to_remove = '202103'
res = []
for i in lst:
if to_remove in i:
res.append(i.replace(to_remove, ""))
print(res)
Hope so this is useful to you and solves your question.

Related

Intersection of two lists in kotlin

How can we intersect two lists in kotlin and save it in another variable(collection of String)
for example I have two lists like this
val list: MutableList<JSONArray> = Arrays.asList(requestedFields)
val otherList: MutableList<MutableList<String>> = Arrays.asList(requiredFields)
Any help is highly appreciated,
Thanks
You can use the intersect method: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/intersect.html
For example:
val first = listOf("Ragh", "Cat")
val second = listOf("Dog", "Ragh", "Giraffe")
val third = first.intersect(second)
println(third) // prints [Ragh]

Haskell How should I check and return the elements in one tuple that partially exists in the other tuple?

I am new to Haskell programming, I currently Have a list1 [[String]] that contains
[["H1","B1"],["H2","B2"],["H3","B2"]]
and another List2 [[String]]
[["H1","B1","H5"],["H2","B2","H5"],["H2","H3","B2"],["H5","B1","H4"]]
and I want to return a result based on list2 that contains elements in List1 like
[["H1","B1","H5"],["H2","B2","H5"],["H2","H3","B2"]]
does anyone understand what I mean?
I totally have no idea how to start on this problem, Thanks for helping
I want to use it as filter and what I design was
isInList :: [[String]] -> [[String]] -> Bool
isInList (x:xs) y = filter (any (`elem` x)) y
But this gave me a wrong answer [["H1","B1","H5"],["H5","B1","H4"]]
where did I wrong?
Your example output contains the first three sublists from List2. Your List1 is of length 3. Do you want to truncate List2 to the length of List1? If so, try
list1 = [["H1","B1"],["H2","B2"],["H3","B2"]]
list2 = [["H1","B1","H5"],["H2","B2","H5"],["H2","H3","B2"]]
truncatedList2 = take (length list1) list2
main = print truncatedList2
Try running the Haskell snippet.

Count duplicate, two-element lists within ArrayList (Groovy)

I have a list of two-item lists:
def list = [[key1, value1], [key1, value1], [key2, value1], [key2, value2], ...]
I am trying to write Groovy code which will take "list" and return another list, which displays not only the frequency of each duplicate, two-item list, but also if there are items which are unique:
def newList = [[key1, value1:2], [key2, [value1:1, value2:1]], ...];
The reasoning behind this format is because I'd write more code to throw an error if a specific "key" had more than one "value". If there is an easier way to do this, any help/suggestions would be appreciated.
It normally helps if you show what you've tried when asking a question here...
In this case though, you just need:
def result = list.groupBy { it[0] }
.collect { k, v -> [k, v.countBy { it[1] }] }
And result will equal:
[['key1', ['value1':2]], ['key2', ['value1':1, 'value2':1]]]
You could also do something like this:
def result = list.groupBy { it[0] }
.collectEntries { k, v -> [k, v*.getAt(1).unique().size() ] }
To give you:
['key1':1, 'key2':2]

how to get the list of the lists?

I have a problem like that:
list = ['a1',['b1',2],['c1',2,3],['d1',2,3,4]]
I want to get a new list like that
new_list['a1','b1','c1','d1']
I do like this:
lst = ['a1',['b1',2],['c1',2,3],['d1',2,3,4]]
for item in lst:
print(item)
result is:
a1
['b1', 2]
['c1', 2, 3]
['d1', 2, 3, 4]
But I want the first element of each result
The best answer is like this :
my_list = list()
lst = ['a1',['b1',2],['c1',2,3],['d1',2,3,4]]
for element in lst:
if type(element)==type('string'):
my_list.append(element)
else:
my_list.append(element[0])
print(my_list)
Thank you!
Do it as below:
>>> my_list = list()
>>> lst = ['a1',['b1',2],['c1',2,3],['d1',2,3,4]]
>>> for element in lst:
if type(element)==type('string'):
my_list.append(element)
else:
my_list.append(element[0])
It will produce:
>>> my_list
['a1', 'b1', 'c1', 'd1']
>>>
As you see above, first I created a list (named my_list) and then checked each elements of your list. If the element was a string, I added it to my_list and otherwise (i.e. it is a list) I added the first element of it to my_list.
I would do
res = []
for x in the_list:
if x is Array:
res.append(x[0])
else:
res.append(x)

How to select sequential elements from a list?

Roughly speaking I am looking for a more elegant alternative for this snippet:
# my_list = range(6) # example list
my_list = ["this", "is", "an", "example", "list"]
max = len(my_list)
for i, elem in enumerate(my_list, start=1):
if i < max:
print elem, my_list[i]
which produces:
this is
is an
an example
example list
Is there some builtin for that?
Edit: I should have said, that I took range(6) as a representant for any iterable to avoid confusion. The goal was to iterate over an iterable pairwise pictured by the result above.
I usually do this:
# x = range(6)
x = ["this", "is", "an", "example", "list"]
for prv, nxt in zip(x, x[1:]):
print prv, nxt
There is no builtin, but the docs for the itertools module contains definition of the pairwise function that does the same.