Split string in the Python script - python

I have following string under variable test:
1 - 10 of 20 Results
I want to split this string and need 20 as a result.
I am using the following script to achieve this:
result=test.split('of')
mid_result=result[1].split(" ")
final_result=mid_result[1]
Is there any way to achieve this in one line or any direct method ?
Thanks.

You could do this:
result = test.split('of')[1].split(" ")[1]
But this would be faster:
result = test.split(' ')[4]

like this?
result = test.split('of')[1].split(" ")[1]
Splitting on whitespaces seems better idea. Thanks Useless

you can use str.split method like this,
str.split(' ')[4]

You had it almost right, but there is not need to split it first by the word "of".
test.split(" ")[4]

result = test.split( )[-2]
Use a reverse index. much easier and more fool proof.

If you are sure of that the format of string remains same for all the strings you are processing, it can be accomplished in the following ways.
test.split(' ')[4]
test.split(' ')[-2]
explanation:
test.split(' ')
will return u a list of
['1', '-', '10', 'of', '20', 'Results']
and the 4th element left to right and the second element right to left will give you the result.

Related

Regex Get String Subset

How can we get the substring based on fullstops using regex? We only wish to get the data after the full stop
Str = “i like cows. I also like camels”
// Regex Code here
Output : “I also like camels”
No need to use regex for that. Use split() method.
splitted = Str.split('.')
# splitted[0] will be 'i like cows'
# splitted[1] will be 'I also like camels'
You can use this approach:
str1 = 'i like cows. I also like camels'
print(str1.split('.')[1:][0].strip())
output:
I also like camels
Try this split
String dataIWant = mydata.split(".")[1];
Result : I also like camels
Using split('.') and selecting the last element is generally better but for fun this is a RegEx solution:
import re
Str = "i like .cows. I also like camels"
pattern = r"([^\.]*$)"
results = re.search(pattern, Str)
print(results.group(1).strip())
This (?:[.]\s([A-Z].+)) picks "I also like camels"

str.replace() or re.sub() continually until substring no longer present

Let's say I have the following string: 'streets are shiny.' I wish to find every occurrence of the string 'st' and replace it with 'ts'. So the result should read 'tseets are shiny'.
I know this can be done using re.sub() or str.replace(). However, say I have the following strings:
'st'
'sts'
'stst'
I want them to change to 'ts','tss' and 'ttss' respectively, as I want all occurrences of 'st' to change to 'ts'.
What is the best way to replace these strings with optimal runtime? I know I could continually perform a check to see if "st" in string until this returns False, but is there a better way?
I think that a while loop that just checks if the 'st' is in the string is best in this case:
def recursive_replace(s, sub, new):
while sub in s:
s = s.replace(sub, new)
return s
tests = ['st', 'sts', 'stst']
print [recursive_replace(test, 'st', 'ts') for test in tests]
#OUT: ['ts', 'tss', 'ttss']
While the looping solutions are probably the simplest, you can actually write a re.sub call with a custom function to do all the transformations at once.
The key insight for this is that your rule (changing st to ts) will end up moving all ss in a block of mixed ss and ts to the right of all the ts. We can simply count the ss and ts and make an appropriate replacement:
def sub_func(match):
text = match.group(1)
return "t"*text.count("t") + "s"*text.count("s")
re.sub(r'(s[st]*t)', sub_func, text)
You can do that with a pretty simple while loop:
s="stst"
while('st' in s):
s = s.replace("st", "ts")
print(s)
ttss
If you want to continually check, then the other questions work well (with the problem that if you have something like stt you would get stt->tst->tts). I don't know if want that.
I think however, that you are trying to replace multiple occurences of st with ts. If that is the case, you should definitely use string.replace. .replace replaces every occurrence of a str, up to the extent you want.
This should be faster according to this.
string.replace(s, old, new[, maxreplace])
example:
>>>import string
>>>st='streets are shiny.streets are shiny.streets are shiny.'
>>>string.replace(st,'st','ts')
#out: 'tsreets are shiny.tsreets are shiny.tsreets are shiny.'
Naively you could do:
>>> ['t'*s.count('t')+'s'*s.count('s') for s in ['st', 'sts', 'stst']]
['ts', 'tss', 'ttss']

how to get the same required string with better and shorter way

s = 'myName.Country.myHeight'
required = s.split('.')[0]+'.'+s.split('.')[1]
print required
myName.Country
How can I get the same 'required' string with better and shorter way?
Use str.rpartition like this
s = 'myName.Country.myHeight'
print s.rpartition(".")[0]
# myName.Country
rpartition returns a three element tuple,
1st element being the string before the separator
then the separator itself
and the the string after the separator
So, in our case,
s = 'myName.Country.myHeight'
print s.rpartition(".")
# ('myName.Country', '.', 'myHeight')
And we have picked only the first element.
Note: If you want to do it from the left, instead of doing it from the right, we have a sister function called str.partition.
You have a few options.
1
print s.rsplit('.',1)[0]
2
print s[:s.rfind('.')]
3
print s.rpartition('.')[0]
Well, that seems just fine to me... But here are a few other ways I can think of :
required = ".".join(s.split(".")[0:2]) // only one split
// using regular expressions
import re
required = re.sub(r"\.[^\.]$", "", s)
The regex only works if there are no dots in the last part you want to split off.

Split a string with space and bracket

I have some coordinates in form of
coordinate = (2.50 6.50)
I want it should split like
2.50:6.50
I have used coordinate.split(" "). But don't know how to use it properly to get above line.
Use the str.format() function.
So Try this:
coordinate = (2.50, 6.50)
print "{}:{}".format(*coordinate)
Output:
2.5:6.5
K DawG's answer is good if you have them directly as tuples.
If you have them as a string coord = '(2.50 6.50)', you can parse it like this:
'(2.50 6.50)'.strip("()").split(' ')
And then, using his formatting:
>>> coord = '(2.50 6.50)'
>>> '{}:{}'.format(*coord.strip("()").split(' '))
'2.50:6.50'
Since coordinate is a string as:
coordinate = '(2.50 6.50)'
Apart from val's answer you can do this also:
print("{0}:{1}").format(*coordinate[1:-1:].split())
Well if it's actually a string you can just go with:
coordinate.strip("()").replace(' ', ':')
which will get the output you wanted.
you can read more about strings in the docs
http://docs.python.org/2/library/string.html

"/1/2/3/".split("/")

It's too hot & I'm probably being retarded.
>>> "/1/2/3/".split("/")
['', '1', '2', '3','']
Whats with the empty elements at the start and end?
Edit: Thanks all, im putting this down to heat induced brain failure. The docs aren't quite the clearest though, from http://docs.python.org/library/stdtypes.html
"Return a list of the words in the string, using sep as the delimiter string"
Is there a word before the first, or after the last "/"?
Compare with:
"1/2/3".split("/")
Empty elements are still elements.
You could use strip('/') to trim the delimiter from the beginning/end of your string.
As JLWarlow says, you have an extra '/' in the string. Here's another example:
>>> "//2//3".split('/')
['', '', '2', '', '3']
Slashes are separators, so there are empty elements before the first and after the last.
you're splitting on /. You have 4 /, so, the list returned will have 5 elements.
That is exactly what I would expect, but we are all different :)
What would you expect from: : "1,,2,3".split(",") ?
You can use strip() to get rid of the leading and trailing fields... Then call split() as before.
[x for x in "//1///2/3///".split("/") if x != ""]

Categories

Resources