Pushing a stack to another stack in python












-1















I´m trying to push the content of stack A to stack B. Also I want to keep the order of both stacks. For this I wanted to use a stack C. I tried to do it in the following function:



class Stack:
...

def pop(self):
self.items.pop()
self.topindex -= 1
return self

def some_func(A, B, C):
for item in A.items:
while A.topindex is not 0:
A.push(A.pop(item), C)
for items in C.items:
while C.topindex is not 0:
C.push(C.pop(item), B)
...


topindex is the index of the last item in the stack.
If 2 items are in the stack topindex would be 2.



However when executed an Error is thrown:
TypeError: pop() takes 1 positional argument but 2 were given



I also tried to use this post(Push a stack onto another stack) for help, but that didn´t work for me either, mainly because I don´t know anything about c#.



Any help is much appreciated.










share|improve this question























  • idownvotedbecau.se/nomcve

    – gilch
    Nov 23 '18 at 18:21
















-1















I´m trying to push the content of stack A to stack B. Also I want to keep the order of both stacks. For this I wanted to use a stack C. I tried to do it in the following function:



class Stack:
...

def pop(self):
self.items.pop()
self.topindex -= 1
return self

def some_func(A, B, C):
for item in A.items:
while A.topindex is not 0:
A.push(A.pop(item), C)
for items in C.items:
while C.topindex is not 0:
C.push(C.pop(item), B)
...


topindex is the index of the last item in the stack.
If 2 items are in the stack topindex would be 2.



However when executed an Error is thrown:
TypeError: pop() takes 1 positional argument but 2 were given



I also tried to use this post(Push a stack onto another stack) for help, but that didn´t work for me either, mainly because I don´t know anything about c#.



Any help is much appreciated.










share|improve this question























  • idownvotedbecau.se/nomcve

    – gilch
    Nov 23 '18 at 18:21














-1












-1








-1








I´m trying to push the content of stack A to stack B. Also I want to keep the order of both stacks. For this I wanted to use a stack C. I tried to do it in the following function:



class Stack:
...

def pop(self):
self.items.pop()
self.topindex -= 1
return self

def some_func(A, B, C):
for item in A.items:
while A.topindex is not 0:
A.push(A.pop(item), C)
for items in C.items:
while C.topindex is not 0:
C.push(C.pop(item), B)
...


topindex is the index of the last item in the stack.
If 2 items are in the stack topindex would be 2.



However when executed an Error is thrown:
TypeError: pop() takes 1 positional argument but 2 were given



I also tried to use this post(Push a stack onto another stack) for help, but that didn´t work for me either, mainly because I don´t know anything about c#.



Any help is much appreciated.










share|improve this question














I´m trying to push the content of stack A to stack B. Also I want to keep the order of both stacks. For this I wanted to use a stack C. I tried to do it in the following function:



class Stack:
...

def pop(self):
self.items.pop()
self.topindex -= 1
return self

def some_func(A, B, C):
for item in A.items:
while A.topindex is not 0:
A.push(A.pop(item), C)
for items in C.items:
while C.topindex is not 0:
C.push(C.pop(item), B)
...


topindex is the index of the last item in the stack.
If 2 items are in the stack topindex would be 2.



However when executed an Error is thrown:
TypeError: pop() takes 1 positional argument but 2 were given



I also tried to use this post(Push a stack onto another stack) for help, but that didn´t work for me either, mainly because I don´t know anything about c#.



Any help is much appreciated.







python stack push pop






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 18:19









janvanhjanvanh

12




12













  • idownvotedbecau.se/nomcve

    – gilch
    Nov 23 '18 at 18:21



















  • idownvotedbecau.se/nomcve

    – gilch
    Nov 23 '18 at 18:21

















idownvotedbecau.se/nomcve

– gilch
Nov 23 '18 at 18:21





idownvotedbecau.se/nomcve

– gilch
Nov 23 '18 at 18:21












1 Answer
1






active

oldest

votes


















0














You did not provide a complete stack implementation for me to use, but Python's list type already has a pop method, so you can use them like stacks.



>>> a = [1,2,3]
>>> b =
>>> c =
>>> while a:
c.append(a.pop())


>>> c
[3, 2, 1]
>>> while c:
b.append(c.pop())


>>> b
[1, 2, 3]
>>> a

>>> c



There are easier ways to manipulate lists, but the above is how you would move the elements of a stack using only push(append)/pop.



Your stack implementation should have an equivalent of .append() and .pop(), and some way to tell if it is empty to end the while loop.






share|improve this answer
























  • Ahh yeah sure, I could have thought about that. Thanks for your help!

    – janvanh
    Nov 23 '18 at 19:45













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53451401%2fpushing-a-stack-to-another-stack-in-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You did not provide a complete stack implementation for me to use, but Python's list type already has a pop method, so you can use them like stacks.



>>> a = [1,2,3]
>>> b =
>>> c =
>>> while a:
c.append(a.pop())


>>> c
[3, 2, 1]
>>> while c:
b.append(c.pop())


>>> b
[1, 2, 3]
>>> a

>>> c



There are easier ways to manipulate lists, but the above is how you would move the elements of a stack using only push(append)/pop.



Your stack implementation should have an equivalent of .append() and .pop(), and some way to tell if it is empty to end the while loop.






share|improve this answer
























  • Ahh yeah sure, I could have thought about that. Thanks for your help!

    – janvanh
    Nov 23 '18 at 19:45


















0














You did not provide a complete stack implementation for me to use, but Python's list type already has a pop method, so you can use them like stacks.



>>> a = [1,2,3]
>>> b =
>>> c =
>>> while a:
c.append(a.pop())


>>> c
[3, 2, 1]
>>> while c:
b.append(c.pop())


>>> b
[1, 2, 3]
>>> a

>>> c



There are easier ways to manipulate lists, but the above is how you would move the elements of a stack using only push(append)/pop.



Your stack implementation should have an equivalent of .append() and .pop(), and some way to tell if it is empty to end the while loop.






share|improve this answer
























  • Ahh yeah sure, I could have thought about that. Thanks for your help!

    – janvanh
    Nov 23 '18 at 19:45
















0












0








0







You did not provide a complete stack implementation for me to use, but Python's list type already has a pop method, so you can use them like stacks.



>>> a = [1,2,3]
>>> b =
>>> c =
>>> while a:
c.append(a.pop())


>>> c
[3, 2, 1]
>>> while c:
b.append(c.pop())


>>> b
[1, 2, 3]
>>> a

>>> c



There are easier ways to manipulate lists, but the above is how you would move the elements of a stack using only push(append)/pop.



Your stack implementation should have an equivalent of .append() and .pop(), and some way to tell if it is empty to end the while loop.






share|improve this answer













You did not provide a complete stack implementation for me to use, but Python's list type already has a pop method, so you can use them like stacks.



>>> a = [1,2,3]
>>> b =
>>> c =
>>> while a:
c.append(a.pop())


>>> c
[3, 2, 1]
>>> while c:
b.append(c.pop())


>>> b
[1, 2, 3]
>>> a

>>> c



There are easier ways to manipulate lists, but the above is how you would move the elements of a stack using only push(append)/pop.



Your stack implementation should have an equivalent of .append() and .pop(), and some way to tell if it is empty to end the while loop.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 18:38









gilchgilch

3,567715




3,567715













  • Ahh yeah sure, I could have thought about that. Thanks for your help!

    – janvanh
    Nov 23 '18 at 19:45





















  • Ahh yeah sure, I could have thought about that. Thanks for your help!

    – janvanh
    Nov 23 '18 at 19:45



















Ahh yeah sure, I could have thought about that. Thanks for your help!

– janvanh
Nov 23 '18 at 19:45







Ahh yeah sure, I could have thought about that. Thanks for your help!

– janvanh
Nov 23 '18 at 19:45




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53451401%2fpushing-a-stack-to-another-stack-in-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Berounka

Sphinx de Gizeh

Different font size/position of beamer's navigation symbols template's content depending on regular/plain...