Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a list of packages and want to remove suffixes from specific combination of characters.
Should I use regex or other mehotds such as replace ? All packages' endings are different because these are versions so I am not sure which to use and how. I think I should clear the remaining characters from -0: in string.
What list is like:
... 'znc-devel-0:1.4-1.el6.i686', 'znc-devel2-0:1.4-1.el6.x86_64' ...
What I want to have:
...'znc-devel', 'znc-devel2'...
What should I do?
partition on "-0" and take the left portion.
seq = ['znc-devel-0:1.4-1.el6.i686', 'znc-devel2-0:1.4-1.el6.x86_64']
print [item.partition("-0")[0] for item in seq]
Result:
['znc-devel', 'znc-devel2']
Through re.sub and list comprehension.
>>> L = ['znc-devel-0:1.4-1.el6.i686', 'znc-devel2-0:1.4-1.el6.x86_64']
>>> import re
>>> [re.sub(r'-0:.*', r'', line) for line in L]
['znc-devel', 'znc-devel2']
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
I have a list of lists, and I'm attempting to loop through and check to see if the strings in a specific index in each of the inner lists contain a combination of "XY" and then 4 numbers immediately following. The "XY" could be in various locations of the string, so I'm struggling with the syntax beyond just using "XY" in row[5]. How to I add the digits after the "XY" to check? Something that combines the "XY" and isdigit()? Am I stuck using the find function to return an index and then going from there?
You can use Python's regex module re with this pattern that matches XY and then four digits anywhere in the string.
import re
pattern = r'XY\d{4}'
my_list = [['XY0'],['XY1234','AB1234'],['XY1234','ABC123XY5678DEF6789']]
elem_to_check = 1
for row in my_list:
if len(row) > elem_to_check:
for found in re.findall(pattern, row[elem_to_check]):
print(f'{found} found in {row[elem_to_check]}')
Output:
XY5678 found in ABC123XY5678DEF6789
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Suppose I have string
exp='"\"OLS\".\"ORDER_ITEMS\".\"QUANTITY\" <50 and \"OLS\".\"PRODUCTS\".\"PRODUCT_NAME\" = 'Kingston''
How can I find word before string \"OLS\".\"PRODUCTS\".\"PRODUCT_NAME\" = 'Kingston' that is and in my case
result should be and
Here's one way (after fixing your string literal):
import re
exp='"OLS"."ORDER_ITEMS"."QUANTITY" <50 and "OLS"."PRODUCTS"."PRODUCT_NAME" = \'Kingston\''
search_for = '"OLS"."PRODUCTS"."PRODUCT_NAME"'
m = re.search(r'(\w+)\s+' + search_for, exp)
print(m.group(1))
Result:
and
Note that there's no reason to escape the double quote characters in your string, since you're defining the string with single quotes. For the same reason, you do have to escape the single quotes around Kingston.
This?
key = "\"OLS\".\"PRODUCTS\".\"PRODUCT_NAME\" = 'Kingston'"
exp[:exp.index(key)].split()[-1]
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
For Example I need each char after '!' in my String ("192.168.!A.!B")
I want a list contains A and B
The String maybe like this "Interface Vlan !A , Name !B"
I would do it with a regular expression:
import re
pattern = r"!(.)"
search_string = "192.168.!A.!B"
results = re.findall(pattern, search_string)
# Returns ['A', 'B']
This also returns an empty list if the ! happens at the end of the string.
regex101 is a good place to learn regular expressions and test them out on strings: https://regex101.com/r/lZClzT/1
Try using split with list comprehension:
print([next(iter(i),None) for i in string.split('!')])
Or in a map-style way:
print(list(map(lambda i: next(iter(i),None),string.split('!'))))
This is the answer by someone delete his answer.
a= re.findall('!(\w)',"192.168.!A.!B")
print(a)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am trying to write a program that orders a list of strings based on the count of specific character, which is dot .
['C222.', 'C4444', 'C22..', 'C333.', 'C2222']are how my list is imported, but I need to put them like this:
['C2222', 'C4444', 'C333.', 'C222.', 'C22..']
I don't care how items are sorted if they have the same number of dots
Thanks in advance!
You can use sorted() with a key function checking the count of specified character:
>>> lst = ['C222.', 'C4444', 'C22..', 'C333.', 'C2222']
>>> sorted(lst, key=lambda t: t.count("."))
['C4444', 'C2222', 'C222.', 'C333.', 'C22..']
An alternative approach with numpy:
np.array(ll)[np.argsort([i.count('.') for i in ll])].tolist()
#['C4444', 'C2222', 'C222.', 'C333.', 'C22..']
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have
filename = src_filename
print filename
somehow it is printing filename = 'apple' instead of just apple. Is there a way I can remove these single quotes.
The single quotes denote a string literal
>>> 'apple'
'apple'
If you print the string, it will not use the single quotes
>>> print('apple')
apple
So in your case you can simply
print(filename)
>>> print(filename.strip('"\''))