Getting the value separated from List - string

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"

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

Add single quotes around string in python

I have converted list to string.
but after conversion I am getting string without single quote around the string
for eg:
items = ['aa','bb','cc']
items = ','.join(items)
output is : aa,bb,cc
expected output: 'aa','bb','cc'
You could use a list comprehension to quote the individual strings in the list:
items = ['aa','bb','cc']
items = ','.join([f"'{i}'" for i in items])
print(items) # 'aa','bb','cc'
One way to accomplish this is by passing the list into a string formatter, which will place the outer quotes around each list element. The list is mapped to the formatter, then joined, as you have shown.
For example:
','.join(map("'{}'".format, items))
Output:
"'aa','bb','cc'"

Check if string is in list with python

I'm new to python, and I'm trying to check if a String is inside a list.
I have these two variables:
new_filename: 'SOLICITUDES2_20201206.DAT' (str type)
and
downloaded_files:
[['SOLICITUDES-20201207.TXT'], ['SOLICITUDES-20201015.TXT'], ['SOLICITUDES2_20201206.DAT']] (list type)
for checking if the string is inside the list, I'm using the following:
if new_filename in downloaded_files:
print(new_filename,'downloaded')
and I never get inside the if.
But if I do the same, but with hard-coded text, it works:
if ['SOLICITUDES2_20201206.DAT'] in downloaded_files_list:
print(new_filename,'downloaded')
What am I doing wrong?
Thanks!
Your downloaded_files is a list of lists. A list can contain anything insider it, numbers, list, dictionaries, strings and etc. If you are trying to find if your string is inside the list, the if statement will only look for identical matches, i.e., strings.
What I suggest you do is get all the strings into a list instead of a list of lists. You can do it using list comprehension:
downloaded_files = [['SOLICITUDES-20201207.TXT'], ['SOLICITUDES-20201015.TXT'], ['SOLICITUDES2_20201206.DAT']]
downloaded_files_list = [file[0] for file in downloaded_files]
Then, your if statement should work:
new_filename = 'SOLICITUDES2_20201206.DAT'
if new_filename in downloaded_files_list:
print(new_filename,'downloaded')
Your code is asking if a string is in a list of lists of a single string each, which is why it doesn't find any.

Is there a simple way to remove sublits

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)

MYSQL: Using GROUP BY with string literals

I have the following table with these columns:
shortName, fullName, ChangelistCount
Is there a way to group them by a string literal within their fullName? The fullname represents file directories, so I would like to display results for certain parent folders instead of the individual files.
I tried something along the lines of:
GROUP BY fullName like "%/testFolder/%" AND fullName like "%/testFolder2/%"
However it only really groups by the first match....
Thanks!
Perhaps you want something like:
GROUP BY IF(fullName LIKE '%/testfolder/%', 1, IF(fullName LIKE '%/testfolder2/%', 2, 3))
The key idea to understand is that an expression like fullName LIKE foo AND fullName LIKE bar is that the entire expression will necessarily evaluate to either TRUE or FALSE, so you can only get two total groups out of that.
Using an IF expression to return one of several different values will let you get more groups.
Keep in mind that this will not be particularly fast. If you have a very large dataset, you should explore other ways of storing the data that will not require LIKE comparisons to do the grouping.
You'd have to use a subquery to derive the column values you'd like to ultimately group on:
FROM (SELECT SUBSTR(fullname, ?)AS derived_column
FROM YOUR_TABLE ) x
GROUP BY x.derived_column
Either use when/then conditions or Have another temporary table containing all the matches you wish to find and group. Sample from my database.
Here I wanted to group all users based on their cities which was inside address field.
SELECT ut.* , c.city, ua.*
FROM `user_tracking` AS ut
LEFT JOIN cities AS c ON ut.place_name LIKE CONCAT( "%", c.city, "%" )
LEFT JOIN users_auth AS ua ON ua.id = ut.user_id

Resources