Is there a simple way to remove sublits - python-3.x

I have a list(rs_data) with sublists obtained from a Dataframe, and some rows from Dataframe contain multiple elements, like those:
print(rs_data)
rs1791690, rs1815739, rs2275998
rs6552828
rs1789891
rs1800849, rs2016520, rs2010963, rs4253778
rs1042713, rs1042714, rs4994, rs1801253
I want to obtain a list in which each element (rs….) is separated, something like this:
{'rs1791690', 'rs1815739', 'rs227599', 'rs401681', 'rs2180062', 'rs9018'….}
How can I eliminate sublits or generate a new list without sublists, in which each element is unique.

To generate a new list you could iterate over the old one and throw out the elements you don't like.
Something like this
for i in rs_data:
if i in bad_values:
# do something
else:
# do something else
If you just want to eliminate duplicates it would be the best to use a set
Like this
mynewset = set(rs_data)

Related

Remove list of strings from list of strings

I have a long list of strings (the main list), and a short list of strings. The short list is an exclude list, and I want to remove all occurences of the elements in the exclude list from the main list.
I've found these two ways of doing it, but none of them seem to work:
val fnrliste: MutableList<String> = ArrayList()
val excludeList = listOf("28030140259", "12050101833", "21089233132", "12050101833")
//Alternative 1:
for (fnr in excludeList) { fnrliste.remove(fnr) }
//Alternative 2:
fnrliste.removeAll(excludeList)
With both alternatives, the strings in the entries are still there when I list the contents of fnrliste.
As can be seen in this screenshot, some entries have been removed (different result with the two methods), but the first entry in the exclude list is still present:
What am I missing here?
Looks like your fnrliste contains some duplicates, so that's why you get different result. Also, as far as I can remember your screenshot, in case of removeAll the first element is "28030140259 " and not "28030140259" so everything is correct

Map repeated values in presto

I'm extracting data from JSON and mapping two arrays in presto.It works fine when there are no repeated values in the array but fails with error - Duplicate map keys are not allowed if any of the values are repeated.I need those values and cannot remove any of the values from the array.Is there a work around for this scenario?
Sample values:
array1 -- [Rewards,NEW,Rewards,NEW]
array2 -- [losg1,losg2,losg3,losg4]
Map key/value has to be generated like this [Rewards=>losg1,NEW=>losg2,Rewards=>losg3,NEW=>losg4]
Pairs of associations can be returned like this:
SELECT ARRAY[ROW('Rewards', 'losg1'), ROW('NEW', 'losg2'), ROW('Rewards', 'losg3')]

Getting the value separated from List

I would like to know how to split this list. How do I separate this:
'[{"cod": "starting"}, "d45aff-dffdfd-dfgssgsdf-dsg"]'
to:
{"cod": "starting"}
and:
"d45aff-dffdfd-dfgssgsdf-dsg"

Is a list in dictionary values?

a = {0:[[1,2,3], [1,3,4,5]]}
print([1,2,3] in a.values())
I get False. Because this list is in values I need True. Is it possible to check all lists in nested list as a value in dictionary? Maybe without loops?
Since you're using python3 you can do this:
[1,2,3] in list(a.values())[0]
a.values() returns a dictionary view. dictionary views
Then you can wrap the dictionary view into list but this list will contain only one element which can be accessed by index 0. However, if your dictionary contains several keys and corresponding values then list(a.values()) will contain the same number of elements (values mapped to keys) as keys in the dictionary.
Note that when you use some_value in some_collection construct and don't use loops explicitly it will still iterate through the collection.

Apply a operation to every item in a set in Python

Is there any way of applying a function to every item in a set. For example, i want to make a every item in a set lowercase. If it was a list, i could do this as:
new_list = [item.lower() for item in old_list]
Obviously i could convert the set to a list, then do this, and then convert it back to a set again, but is there a more direct way?
You can simply use:
new_set = {set.lower() for item in old_set}
This is a set comprehension rather than a list comprehension.

Resources