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 9 years ago.
Improve this question
I want to get incremental output each time i call the count function
import collections
result = collections.defaultdict(list)
global probability
def count():
vent ="Event"
if event in result:
probability +=1
else:
probability = 0
result[event] = {"Count":probability,"Event Type":"Login","Source":"Security","Message":"msg"}
print result[event]
count()
count()
in your count() function, the variable probability is created when the function called, it is not the same variable probability you declared at beginning.
I think you may want to use the variable as global variable.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I tried searching online but did not find any answer to this particular question. In python there are no function declarations and a function can't be used until it has been defined.
Does this mean that indirect recursion is impossible in python?
And is there a way around this by using some modules?
No, it is possible
def f():
print('from f')
g()
def g():
print('from g')
f()
"a function can't be used until it has been defined" is not so straightforward. When the code runs, the name of the objects that it refers to have to exist. So, you can't do
f()
def f():...
because f() actually executes something. But definitions create a function object, without running it at the time. In the example, the function is claled at the last line of the script, and, by that time, both f, g do exist.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
The community is reviewing whether to reopen this question as of 1 year ago.
Improve this question
I am Relatively new to python, and I am not sure what exactly this statement does. And I'm also unsure of the token lambda here.
What exactly does this statement mean/do?
checks = len(g[max(g, key=lambda key: len(g[key]))])
checks = len(g[max(g, key=lambda key: len(g[key]))])
Let's start from the inner statement.
'max' is going to return the maximum value, in this case using the defined lambda function as key indicator. The lambda function returns the length of the dictionary value, having that provided key.
Afterwards you use that maximum value inside the g[] brackets, which is going to return the dictionary value with the returned maximum value as key.
Since you wanted the keyword lambda clarified: Lambda calls are anonymous functions, which you can for example quickly use without defining a def statement. They are particularly useful for list comprehensions, or other quick operations, like this one.
In the end you pass that value to the len function, which is going to return an int with the length of the value.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Write a program in Python that defines a function that takes a single argument, a positive integer. The function should employ a while loop, and return the largest power of two that is less than or equal to the number. So, for example, if the function was called with the value 133, it would return 128, and if it was called with 19, it would return 16.
Please include a number of function calls in your code to test your function beneath its definition.
The answer in simplest python code would be best thanks.
P.S. Im no expert with python so details would be appreciated
A simple while loop works, just make sure you divide the number by 2, otherwise you'll get the NEXT power of 2.
def function(number):
x = 1
while x <= number:
x *= 2
return x / 2
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 8 years ago.
Improve this question
How can I refer to a previous function in a new function in python 2.7.
For instance, say if I want to display the result which function1 calculated in function2. How would I go about this?
wouldn't it be:
def func_one():
return 2 + 2
def func_two():
x = func_one()
print x
func_two()
#output: 4
You need to understand the flow of your program. A function doesn't run until it is called. When it is called, it may return a value (if you say it calculates something, it should return that). That return value is available to the caller of the function, but not to any other functions. So function2 cannot use it. Unless it is passed that value as an argument, that is:
def function1():
return 42
def function2(value):
print('The value is %d' % value)
x = function1()
function2(x)
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 currently have a class that turns my list of lists into a list of objects, where every object has a certain amount of stuff from the constructor. Lets say they have names, and some random numbers.
What I would like to do is print all of these objects simultaneously, where each object is one line. How would I go about doing this, I tried making a Str function, but it still returns ""
Okay, I have a class which has 10 objects, these have the attributes self.planet, self.distance, self.distsquared, self.radius, self.diamater where distance/distsquared/radius/diamater are all integers and I have a function which is supposed to print all of the planets after their distance, with the furthest distance highest. But when I try to make a function return "" % (self.planet, self.distance, self.distsquared, self.radius self.diameter) it still only prints I want every object to be printed
Thanks in advance!
For a list of objects, you can print them neatly using:
print("\n".join(str(x) for x in object_list))
The class should have the function to make each object into a string as follows:
def __str__(self):
return "Attr1: {0.attr1}, Attr2: {0.attr2}, ...".format(self)