Your question has been reopened by 5 votes. So seems like all users don't really agree if your question is on topic or not.
In my opinion your questions is on topic and is not even a low quailityquality one because :
- The question is clear : "my code is not working as expected how can iI fix it"
- You have searchsearched by yourself before posting here
- You give us every elements that we need to be able to answer : the code, the current result, the desired result.
And iI disagree with the comment saying "a high level approach". However your problem is not only about python but with the algorithm to be used too this may be why this question was closed on the first hand.