substring

How to extract largest possible string's alpha substrings

How to extract largest possible string's alpha substrings Question: I want to extract some kind of variable names from the string (digits, _, etc. not allowed), without any imports (pure python only) and as short as possible (even if it’s unreadable). How can i do it? Example: "asd123*456qwe_h" -> [asd, qwe, h]. (‘a’, ‘qw’, etc. …

Total answers: 2

Finding Longest Common Substring

Finding Longest Common Substring Question: I’m trying to find the longest common substring in all DNA strands but in testing I’m using strings of numbers. Here is the funciton I wrote: def lcsm(string_list): strands = sorted(string_list, key = len, reverse = False) longest_motif = ” seq1 = strands[0] seq2 = strands[1] motif = ” for …

Total answers: 1

Searching for sequence of bits in an integer in Python

Searching for sequence of bits in an integer in Python Question: I have two integers, lets call them haystack and needle. I need to check that, if the binary representation of needle occurred in haystack [and OPTIONALLY find the position of the first occurrence] Example haystack = 0b10101111010110010101010101110 needle = 0b1011001 # occurred in position …

Total answers: 2

python remove characters between quotes

python remove characters between quotes Question: I tried to remove characters between quotes. Input string: "a string ,5,to",5,6,my,use,"123456,inside",1,a,b I would like this output: 5,6,my,use,1,a,b I tried with re.sub without success: line = [‘"a string ,5,to",5,6,my,use,"123456,inside",1,a,b’] substr = re.sub("["].*?["]", "", line) many thanks for any help Asked By: greg || Source Answers: You can match two …

Total answers: 2

Remove words from list but keep the ones only made up from the list

Remove words from list but keep the ones only made up from the list Question: I have one dataframe containing strings and one list of words that I want to remove from the dataframe. However, I would like to also keep the strings from the df which are entirely made up of words from the …

Total answers: 3

extract substring from a string in python?

extract substring from a string in python? Question: I have two strings like below – 1)500 Rahway Avenue, Westfield, NJ 07090 2)910 N Harbor Drive, San Diego CA 92101 Want to get 2 Expected output- 1)Westfield 2)San Diego And 1)NJ 2)CA I tried below approach for output NJ and CA – s1.rsplit(" ")[-2] But this …

Total answers: 1

How to extract only specific elements, combine "find_all" and "find_elements_by_xpath"?

How to extract only specific elements, combine "find_all" and "find_elements_by_xpath"? Question: I want to extract all of ‘data-test-id’=’^fdbk-item-.*$’ in <span> from link. Futhermore, within that contain whichever capital or lower case mirror|tray|ceramic. source Using find_all(), retrieving ‘data-test-id’=’^fdbk-item-. *$’ was successfully. from selenium import webdriver from selenium.webdriver.common.by import By from bs4 import BeautifulSoup import time import …

Total answers: 1

match multiple substrings using findall from re library

match multiple substrings using findall from re library Question: I have a large array that contains strings with the following format in Python some_array = [‘MATH_SOME_TEXT_AND_NUMBER MORE_TEXT SOME_VALUE’, ‘SCIENCE_SOME_TEXT_AND_NUMBER MORE_TEXT SOME_VALUE’, ‘ART_SOME_TEXT_AND_NUMBER MORE_TEXT SOME_VALUE] I just need to extract the substrings that start with MATH, SCIENCE and ART. So what I’m currently using my_str = …

Total answers: 2

String Compression in Python

String Compression in Python Question: I have the following input : my_list = ["x d1","y d1","z d2","t d2"] And would like to transform it into : Expected_result = ["d1(x,y)","d2(z,t)"] I had to use brute force, and also had to call pandas to my rescue, since I didn’t find any way to do it in plain/vanilla …

Total answers: 5