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
how to count vowels in python for example
'XaXeXUU'
should give me the answer 4
how can i calculate this?
my_string = 'XaXeXUU'
vowels = 'aeiou'
sum(ch in vowels for ch in my_string.lower())
len(my_string)-len(my_string.lower().translate(None,'aeiou'))
Related
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
How can I count how many different numbers there are in one long number?
For example: this number 1122334455 has 5 different numbers.
How can I do that in Python?
You can do that as:
print len(set(str(s)))
The str() casts the int as a string
The set() takes the unique elements of the string and creates a set of it
The len() returns the length of the set
Examples
>>> print len(set(str(s)))
5
s = 1324082304
>>> print len(set(str(s)))
6
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
How to check if part of text is in tuple?
For example:
my_data = ((1234L,), (23456L,), (3333L,))
And we need to find if 123 or 1234 is in tuple.
I didn't worked lot with tuples before.
In array we use:
if variable in array
But its not working for tuples like my_data
PS. 1st answer solved problem.
def findIt(data, num):
num = str(num)
return any(num in str(i) for item in data for i in item)
data = ((1234L,), (23456L,), (3333L,))
print findIt(data, 123)
Output
True
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
If I had a list
['xxxx', 'oooo', 'xxxx', 'oooo'......etc]
which looks like
(xxxx)
(oooo)
(xxxx)
(oooo)
and the list could be as long as the user inputs,
how would I make a new list sorted by each column which would look like:
['xoxo', 'xoxo', 'xoxo', 'xoxo']
which would be
(xoxo)
(xoxo)
(xoxo)
(xoxo)
myList=['xxxx', 'oooo', 'xxxx', 'oooo']
print [''.join(element) for element in zip(*myList)]
Output
['xoxo', 'xoxo', 'xoxo', 'xoxo']
What you are looking for, is called, transposing an array. That can be achieved with zip function.
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).