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.
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)
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 json array in this format in PYTHON. How to make it to a 2d numpy array?
[{"z":1,"y":0.10000000149011612,"x":0.10000000149011612},{"z":0.6666666865348816,"y":0.10000000149011612,"x":0.20000000298023224},{"z":0.5,"y":0.10000000149011612,"x":0.30000001192092896},{"z":0.6666666865348816,"y":0.10000000149011612,"x":0.4000000059604645},{"z":0.3333333432674408,"y":0.10000000149011612,"x":0.5},{"z":0.1666666716337204,"y":0.10000000149011612,"x":0.6000000238418579},{"z":0.8333333134651184,"y":0.10000000149011612,"x":0.699999988079071},{"z":0.5,"y":0.10000000149011612,"x":0.800000011920929},{"z":0.5,"y":0.25999999046325684,"x":0.10000000149011612},{"z":0.5,"y":0.25999999046325684,"x":0.20000000298023224},{"z":1,"y":0.25999999046325684,"x":0.30000001192092896},{"z":0.1666666716337204,"y":0.25999999046325684,"x":0.4000000059604645},{"z":0.1666666716337204,"y":0.25999999046325684,"x":0.5},{"z":0.5,"y":0.25999999046325684,"x":0.6000000238418579},{"z":0.8333333134651184,"y":0.25999999046325684,"x":0.699999988079071},{"z":0.6666666865348816,"y":0.25999999046325684,"x":0.800000011920929},{"z":0.5,"y":0.41999998688697815,"x":0.10000000149011612},{"z":0.6666666865348816,"y":0.41999998688697815,"x":0.20000000298023224},{"z":0.1666666716337204,"y":0.41999998688697815,"x":0.30000001192092896},{"z":0.6666666865348816,"y":0.41999998688697815,"x":0.4000000059604645},{"z":0.5,"y":0.41999998688697815,"x":0.5},{"z":0.8333333134651184,"y":0.41999998688697815,"x":0.6000000238418579},{"z":0.8333333134651184,"y":0.41999998688697815,"x":0.699999988079071},{"z":0.5,"y":0.41999998688697815,"x":0.800000011920929},{"z":0.6666666865348816,"y":0.5799999833106995,"x":0.10000000149011612},{"z":0.6666666865348816,"y":0.5799999833106995,"x":0.20000000298023224},{"z":0.1666666716337204,"y":0.5799999833106995,"x":0.30000001192092896},{"z":0.1666666716337204,"y":0.5799999833106995,"x":0.4000000059604645},{"z":0.8333333134651184,"y":0.5799999833106995,"x":0.5},{"z":0.5,"y":0.5799999833106995,"x":0.6000000238418579},{"z":0.6666666865348816,"y":0.5799999833106995,"x":0.699999988079071},{"z":0.1666666716337204,"y":0.5799999833106995,"x":0.800000011920929},{"z":0.3333333432674408,"y":0.7400000095367432,"x":0.10000000149011612},{"z":1,"y":0.7400000095367432,"x":0.20000000298023224},{"z":0.1666666716337204,"y":0.7400000095367432,"x":0.30000001192092896},{"z":0.3333333432674408,"y":0.7400000095367432,"x":0.4000000059604645},{"z":1,"y":0.7400000095367432,"x":0.5},{"z":0.1666666716337204,"y":0.7400000095367432,"x":0.6000000238418579},{"z":0.1666666716337204,"y":0.7400000095367432,"x":0.699999988079071},{"z":0.1666666716337204,"y":0.7400000095367432,"x":0.800000011920929}]
The array look like: ([[1,2,3],[4,5,6]]) Every json object is a []
As I understand, json_array is a list of dictionaries
m = np.zeros((len(json_array), 3))
for i, elem in enumerate(json_array):
m[i,0] = elem['x']
m[i,1] = elem['y']
m[i,2] = elem['z']
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
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"]]