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 2 days ago.
Improve this question
So I already read a chess opening pgn in python so that I have that kind of a list:
pgn = ['e4', 'c5', ['e5', 'Nc3', 'Nf6', ['Bc5']], 'Nf3', 'Nc6', 'd4', ['Bb5', 'e6', 'O-O', ['Bxc6']], 'cxd4', 'Nxd4']
Now I want to get a new list of objects of that kind of class:
class Node:
def __init__(self, name, move, parent):
self.name = name
self.move = move
self.parent = parent
where every object knows it's parent (or children if that's easier). I have quite some trouble to do this. How can I manage to do this?
The result of the new list should be in this example be in no parti:
[Node(1,"e5",None),Node(2,"c5",1),Node(3,"e5",1),Node(4,"Nc3",3),Node("5,Nf6",4),Node(6,"Bc5",4),Node(7,"Nf3",2),Node(8,"Nc6",7),Node(9,"d4",8),Node(10,"Bb5",8),Node(11,"e6",10),Node(12,"O-O",11),Node(13,"Bxc6",11),Node(14,"cxd4",9),Node(15,"Nxd4",14)]
I tried several iterative or recursive functions to make my new list of object but I can't seem to get the exact parent or children of a given Node.
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 2 years ago.
The community is reviewing whether to reopen this question as of 2 years ago.
Improve this question
I was working with Python3, and trying some sorts of code. And then i came to try some function features, here is my code
def print_list_members(some_list):
for i in first_list:
print(i)
that's all my function definition. and then i add for example new to the code
first_list = ["Alfried", "Michael", "John"]
second_list = ["Joseph", "Tim", "Delta"]
then i try to produce traceback by passing different argument with the function code
print_list_members(second_list)
but, no traceback raised, except something make me a bit confused, the output is
Alfried
Michael
John
the question is, how it be possible? or is it an error from python itself?
Change your code here
def print_list_members(some_list):
for i in some_list:
print(i)
You iterate over the global first_list inside the body of the function, so you print first_list. Whatever you pass as an argument is ignored. Perhaps you wanted iterate over some_list?
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 3 years ago.
Improve this question
I am using Python for some operations on a XML file.
Because I am new to programming I would like to know how I can re-use the snippet below, currently it has a hard-coded statement in it.
Please look at the line with
for ERPRecord in aroot.iter('part'):
inside it, aroot should be replaced with the modular option or variable.
def SetERP(ArticleN,ERPn):
for ERPRecord in aroot.iter('part'):
if ERPRecord.get('P_ARTICLE_ORDERNR') == ArticleN:
ERPRecord.set('P_ARTICLE_ERPNR', ERPn)
I would like to have a function without hard-coded parts in so it is able to be used again in other projects. My best guess is that the sequence "aroot" will be replaced by a variable like this:
def SetERP(ArticleN,ERPn, XMLroot):
for ERPRecord in XMLroot.iter('part'):
if ERPRecord.get('P_ARTICLE_ORDERNR') == ArticleN:
ERPRecord.set('P_ARTICLE_ERPNR', ERPn)
Any advice on this would be welcome!
You could define aroot as a parameter, so you would have to pass your root in every time you call the function, if that is what you mean?
def SetERP(ArticleN, ERPn, aroot):
for ERPRecord in aroot.iter('part'):
if ERPRecord.get('P_ARTICLE_ORDERNR') == ArticleN:
ERPRecord.set('P_ARTICLE_ERPNR', ERPn)
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 4 years ago.
Improve this question
I found code on GitHub that is similar to my ExampleClass, but I don't understand the benefit of using getx = calcualte_x. I feel like it is somewhat close to #property, but it is not obviously.
what is the benefit of that line and why not just use class-instance.calcualte_x(add) instead of class-instance.getx(add)
class ExampleClass():
def __init__(self, x_val):
self.x = x_val
def calcualte_x(self, add):
x = self.x + add
getx = calcualte_x
It can be an alias - for ease of use or for backward compatibility for example.
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 6 years ago.
Improve this question
Suppose I have a function -
def foo(x,y):
pass
a = foo(5,6)
How do I access the values 5 and 6 from a?
From the code you have shown us, you cannot -- 5 and 6 were passed in to foo, you didn't keep a copy of them, foo didn't keep a copy of them, so they are gone.
So, as the above paragraph hinted, somebody has to keep a copy of those arguments if you want to do something else with them later, and while it is possible to have a function do so, that's not really what they are intended for. So your easy options are:
make foo a class that saves the arguments it was called with (which is still highly unusual), or
save the arguments yourself (arg1, arg2 = 5, 6 for example)
You can't. You'd need to use an object.
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 8 years ago.
Improve this question
I define a deque and insert a vector in it in python.My goal was define a deque from vectors.
But when I iterate on this queue,python returns first argument of this vector.
How can I define a deque from vector?
I'm not sure what you mean by vector, but, as with about any sequence in Python you are able to store any type of object in it. Unlike C++, where the type of the stored objects needs to be known at compile time.
Here is an example:
class vector(object):
def __str__(self):
return "I'm a vector, for realz!"
...
mydeque = deque()
for i in range(1, 20):
mydeque.append(vector())
for vec in mydeque:
print(vec)