Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I want to reverse and split a string in Python. Please suggest how?
'this is Xing Min' should return ['niM', 'gniX', 'si', 'siht'].
You can do it as:
my_str[::-1].split()
Example
>>> s = 'Hello World'
>>> print s[::-1].split()
['dlroW'. 'olleH']
>>> s = 'this is Xing Min'
>>> print s[::-1].split()
['niM', 'gniX', 'si', 'siht']
Here, the [::-1] gets the whole string in reverse order. This is the syntax [start:end:step]. When you don't specify a start and end, it will deal with the whole string. When you do [::-1], the step value is -1 which means that the string is read in reverse.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to parse a string from this:
CN=ERT234,OU=Computers,OU=ES1-HER,OU=ES1-Seura,OU=RES-ES1,DC=resu,DC=kt,DC=elt
To this:
ES1-HER / ES1-Seura
Any easy way to do this with regex?
>>> import re
>>> s = 'CN=ERT234,OU=Computers,OU=ES1-HER,OU=ES1-Seura,OU=RES-ES1,DC=resu,DC=kt,DC=elt'
>>> re.findall('OU=([^,]+)', s)
['Computers', 'ES1-HER', 'ES1-Seura', 'RES-ES1']
>>> re.findall('OU=([^,]+)', s)[1:3]
['ES1-HER', 'ES1-Seura']
>>> ' / '.join(re.findall('OU=([^,]+)', s)[1:3])
'ES1-HER / ES1-Seura'
Don't use str as a variable name. It shadows builtin function str.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
strSpecialChars=['%', 'dBu', 'dB', 'kHz', 'Hz']
str = "-20.0dB"
I need to get True here as it checks for each item of the list - strSpecialChars in the string str.
Use the any() function to test each value:
>>> strSpecialChars=['%', 'dBu', 'dB', 'kHz', 'Hz']
>>> yourstr = "-20.0dB"
>>> any(s in yourstr for s in strSpecialChars)
True
where I renamed str to yourstr to avoid masking the built-in type.
any() will only advance the generator expression passed to it until a True value is returned; this means only the first 3 options are tested for your example.
You could use str.endswith() here:
any(yourstr.endswith(s) for s in strSpecialChars)
to limit matches to only those that end with any of the special characters.
map(lambda s: s in "-20.0dB", strSpecialChars)
You may need to convert the output through list to actually see it.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Can you simplify this code from Python into Ruby? Lets say I have this data
data = ['hello', 'person', ';hello', 'otherperson']
print([x.split("#") for x in "#".join(data).split(";")])
When I print it it prints this:
[['hello', 'person', ''], ['hello', 'otherperson']]
Is there something like this in Ruby? If it can be accomplished in one line, I would prefer that, but I'm after just knowing how it is.
Literally translated,
data.join(?#).split(?;).map { |x| x.split(?#) }
But you might want a different approach entirely. This will misbehave if any of the strings contain #.
This works for intended output, but do note it modifies original strings, so ideally data is a deep clone (or otherwise not a problem to alter contained strings):
data.slice_before { |s| s.gsub!(/^;/,'') }.to_a
=> [["hello", "person"], ["hello", "otherperson"]]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to convert python codes to java. But I do not understand slicing step parameter.
Example:
x = "Hello World !"
x[6:2:-1]
And what is the result x[6:2:-1] ?
-1 step just reverts the direction of the slice:
>>> x = "Hello World !"
>>> x[6]
'W'
>>> x[2]
'l'
>>> x[6:2:-1]
'W ol'
[6:2:-1] means give me a slice from the 6th to the 2nd item (not including), reversed.
FYI, you don't need python installed for checking the result of the code you've asked about, go to pythonanywhere and play:
PythonAnywhere is a Python development and hosting environment that
displays in your web browser and runs on our servers.
Also see:
Explain Python's slice notation
Extended Slices
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a file and some substitution is needed: replace "," with "," and all other characters not in rule2 with a whitespace, how can I do that?
What about this?
text = text.replace(",", ",")
You can use the regular expressions module:
text = re.sub(',', ',', text)
text = re.sub(negated_rule2, ' ', text)
where your negation of "rule2" is formatted using the regular expressions syntax (see link above).