Get all the Values before a delimiter from a string - python-3.x

I have the following string below, and I want to get all the values before the equal sign and in a list.
asaa=tcp:192.168.40.1:1119 dsae=tcp:192.168.40.2:1115 dem=tcp:192.168.40.3:1117 ape=tcp:192.168.40.4:1116
Result should be:
asaa
dsae
dem
ape
Any help would be appreciated. been trying a couple different things but can get it into a list nor can i get the rest of the values.

s = 'asaa=tcp:192.168.40.1:1119 dsae=tcp:192.168.40.2:1115 dem=tcp:192.168.40.3:1117 ape=tcp:192.168.40.4:1116'
parts = s.split()
result = [part.split('=')[0] for part in parts]
print(result)
# ['asaa', 'dsae', 'dem', 'ape']

Related

Python: (partial) matching elements of a list to DataFrame columns, returning entry of a different column

I am a beginner in python and have encountered the following problem: I have a long list of strings (I took 3 now for the example):
ENSEMBL_IDs = ['ENSG00000040608',
'ENSG00000070371',
'ENSG00000070413']
which are partial matches of the data in column 0 of my DataFrame genes_df (first 3 entries shown):
genes_list = (['ENSG00000040608.28', 'RTN4R'],
['ENSG00000070371.91', 'CLTCL1'],
['ENSG00000070413.17', 'DGCR2'])
genes_df = pd.DataFrame(genes_list)
The task I want to perform is conceptually not that difficult: I want to compare each element of ENSEMBL_IDs to genes_df.iloc[:,0] (which are partial matches: each element of ENSEMBL_IDs is contained within column 0 of genes_df, as outlined above). If the element of EMSEMBL_IDs matches the element in genes_df.iloc[:,0] (which it does, apart from the extra numbers after the period ".XX" ), I want to return the "corresponding" value that is stored in the first column of the genes_df Dataframe: the actual gene name, 'RTN4R' as an example.
I want to store these in a list. So, in the end, I would be left with a list like follows:
`genenames = ['RTN4R', 'CLTCL1', 'DGCR2']`
Some info that might be helpful: all of the entries in ENSEMBL_IDs are unique, and all of them are for sure contained in column 0 of genes_df.
I think I am looking for something along the lines of:
`genenames = []
for i in ENSEMBL_IDs:
if i in genes_df.iloc[:,0]:
genenames.append(# corresponding value in genes_df.iloc[:,1])`
I am sorry if the question has been asked before; I kept looking and was not able to find a solution that was applicable to my problem.
Thank you for your help!
Thanks also for the edit, English is not my first language, so the improvements were insightful.
You can get rid of the part after the dot (with str.extract or str.replace) before matching the values with isin:
m = genes_df[0].str.extract('([^.]+)', expand=False).isin(ENSEMBL_IDs)
# or
m = genes_df[0].str.replace('\..*$', '', regex=True).isin(ENSEMBL_IDs)
out = genes_df.loc[m, 1].tolist()
Or use a regex with str.match:
pattern = '|'.join(ENSEMBL_IDs)
m = genes_df[0].str.match(pattern)
out = genes_df.loc[m, 1].tolist()
Output: ['RTN4R', 'CLTCL1', 'DGCR2']

Selecting a string from list of strings

I have a list of strings [abc1, abc2, abc3, xyz3, xyz4]
Out of the elements with the same string preceding the number, I need to keep just the string with the highest number in my output list. So out of abc1, abc2 and abc3, the string abc3 should be selected. Out of xyz3 and xyz4, xyz4 should be kept.
So the final list should contain [abc3, xyz4].
I've been thinking of how this problem can be solved since the past 2 days and after unsuccessfully trying out some approaches, I am still in the dark how this can be done. I would greatly appreciate any help on this.
This function is what you need
The first step of each item is divided into two parts, number and string
Step 2 If the aphid already exists in the dictionary, its value is compared to the current item value. If it is smaller, its value is moved to the current number.
Otherwise I save the value in the dictionary.
Finally, we turn the dictionary into a list
def split(items):
biggest=dict()
for i in items:
string = i[:-1]
number = int(i[-1])
if string in biggest:
if biggest[string]<number:
biggest[string]=number
else:
biggest[string]=number
return list([k+str(v) for k,v in biggest.items()])
x = ['abc1', 'abc2', 'abc3', 'xyz3', 'xyz4']
print(split(x))
output :
['abc3', 'xyz4']

I need an Integer but its a string with a comma

I'm using sqlite3 and trying to get the oid by using the title of the row and then trying to use that oid to update a column in my table.
allOID is a tuple, and when I print it i get this:
>>> <class 'tuple'>
>>> [(1,)]
I'm trying to get the integer out of this tuple but the comma is throwing it off and I can't seem to get it.
Here is all of the code being used currently:
c.execute("""SELECT oid FROM books
WHERE title = :title""",
{
'title': title
})
allOID = c.fetchall()
print(type(allOID[0]))
print(allOID)
c.execute("SELECT * FROM books")
c.execute("""UPDATE books SET
rented = :rented
WHERE oid = :oid""",
{
'rented': rentedVar,
'oid': allOID[0]
})
any help and comments are greatly appreciated!
The comma just indicates that it is a tuple with a single element.
Access it using allOID[0][0].
allOID[0] gets you the tuple out of the list of results, going one level further with allOID[0][0] gets you the first element of the tuple.
For more info, see the docs:
Empty tuples are constructed by an empty pair of parentheses; a tuple with one item is constructed by following a value with a comma (it is not sufficient to enclose a single value in parentheses). Ugly, but effective.

How do I take a string and turn it into a list using SCALA?

I am brand new to Scala and having a tough time figuring this out.
I have a string like this:
a = "The dog crossed the street"
I want to create a list that looks like below:
a = List("The","dog","crossed","the","street")
I tried doing this using .split(" ") and then returning that, but it seems to do nothing and returns the same string. Could anyone help me out here?
It's safer to split() on one-or-more whitespace characters, just in case there are any tabs or adjacent spaces in the mix.
split() returns an Array so if you want a List you'll need to convert it.
"The dog\tcrossed\nthe street".split("\\s+").toList
//res0: List[String] = List(The, dog, crossed, the, street)

Format string with list in Python3

Lets say I have string: s = '{1} goes to {0}'
And I want to format this string with list: l = ['Hollywood', 'Frankie']
I cannot modify string and list both. Is there way to write simple piece of code to handle this case?
PS. I know about question "Python Format String with List", but it is not what Im asking.
Use the unpack operator * when passing the list to the format method.
s = '{1} goes to {0}'
l = ['Hollywood', 'Frankie']
print(s.format(*l))
This outputs:
Frankie goes to Hollywood

Resources