Count of different numbers in one long number [closed] - python

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

Related

Python List to Float conversion [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 5 years ago.
Improve this question
The result of one of my web scrapes produces the following:
price = ['$1049.98']
which is type list, and I am trying to convert this to float.
Take the first and only element in the list, strip off the $ sign and convert it to a float:
parsed_price = float(price[0].lstrip('$'))
import re
price = ['$1049.98']
get_values = [float(re.search(r'(\d+\.\d+)+',cost).group()) for cost in price]
print(get_values)
>>>[1049.98]
Here is my simple solution. Hope that helps.
price = ['$1049.98']
result = [float(i[1:]) for i in price][0]
print(result)
You can change the index(I mean, replace 0 with other numbers) or loop though the list(result).

"while True" intermediate solutions [closed]

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'm working with big multidimensional arrays. I want to know after x loops the values S,S2,E,E2
I want to find all the solutions of a loop.
I have a code that iterates with a greedy. My output is a list of numers. I want to save the arrays that generate those numbers. My code:
while True:
if f2<f:
f,S,S2=f2,E,E2 # f is a value and S and E are arrays
......
print f2-f
else:
break
If i run this code I have a list of f2-f but I need also to save the f,S,S2,f2,E,E2 created in each passage of the while.. Thank you
results = []
while True:
if f2<f:
f,S,S2=f2,E,E2 # f is a value and S and E are arrays
......
results.append((f,S,S2,E,E2))
print f2-f
print "intermediate result:", results[-1]
else:
break

Please help me understand this - Python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
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
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
Can someone please explain how this works? Like, how does it count?
for a in range(2,5):
for b in range(1,2):
print (a+b,end=" ")
print("---",end=" ")
the output is: 3 --- 4 --- 5 ---
It just adds 2, 3, 4 with 1 and prints the result.
In Python-3.x, print() is a function and the argument end in it means
string appended after the last value, default a newline
Values returned by range(x, y) are [x, y-1]
If you don't know the usage of a function, you could open ipython and type something like the following:
help(print)
help(range)

Check if part of text is in tuple [closed]

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

Python List sorting by columns [closed]

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.

Categories

Resources