d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Python Assistance > List Separation
12Next
Add Reply New Topic New Poll
Member
Posts: 4,625
Joined: Jul 3 2009
Gold: 6,520.00
Jun 29 2014 02:23pm
I am curious to know why my following code does not work. If you know python well please PM or post, it would be very appreciated.

Code
elements = [0,1,2,3,4,5]
n = len(elements)

#code to modify list how i need it.... helper returns [0,2,4,5,3,1]
mylist = helper(elements, count, choice)

even = mylist[:n/2]

#even is unable to be assigned.
#even should = [0,2,4]


again, help is appreciated.
Member
Posts: 6,481
Joined: Jul 5 2009
Gold: 0.70
Jun 29 2014 02:31pm
Quote (SleepyUnit @ Jun 29 2014 04:23pm)
I am curious to know why my following code does not work. If you know python well please PM or post, it would be very appreciated.

Code
elements = [0,1,2,3,4,5]
n = len(elements)

#code to modify list how i need it.... helper returns [0,2,4,5,3,1]
mylist = helper(elements, count, choice)

even = mylist[:n/2]

#even is unable to be assigned.
#even should = [0,2,4]


again, help is appreciated.


even = [i for i in myList if not i%2]
Member
Posts: 4,625
Joined: Jul 3 2009
Gold: 6,520.00
Jun 29 2014 02:34pm
Quote (mebeatyou @ Jun 29 2014 04:31pm)
even = [i for i in myList if not i%2]


The point of my function that I'm doing is to use recursion and not use for loops in any way unfortunately - otherwise I would be able to use that. I am trying to figure out why this part of my code is not working though:

even = mylist[:n/2]

This post was edited by SleepyUnit on Jun 29 2014 02:34pm
Member
Posts: 6,481
Joined: Jul 5 2009
Gold: 0.70
Jun 29 2014 03:02pm
Quote (SleepyUnit @ Jun 29 2014 04:34pm)
The point of my function that I'm doing is to use recursion and not use for loops in any way unfortunately - otherwise I would be able to use that. I am trying to figure out why this part of my code is not working though:

even = mylist[:n/2]


Without actually testing it, I think what you want is mylist[::n/2]
Member
Posts: 4,625
Joined: Jul 3 2009
Gold: 6,520.00
Jun 29 2014 03:06pm
Quote (mebeatyou @ Jun 29 2014 05:02pm)
Without actually testing it, I think what you want is mylist[::n/2]


Just trying to make it so even = [0,2,4], or every even element in the list elements.

Since I cannot do any looping, I modify a new list to be [0,2,4,5,3,1] from the original [0,1,2,3,4,5].

Then I will assign even to [0,2,4] and odd to [1,3,5].

I'll try out your suggestion though
Member
Posts: 6,481
Joined: Jul 5 2009
Gold: 0.70
Jun 29 2014 03:07pm
Quote (SleepyUnit @ Jun 29 2014 05:06pm)
Just trying to make it so even = [0,2,4], or every even element in the list elements.

Since I cannot do any looping, I modify a new list to be [0,2,4,5,3,1] from the original [0,1,2,3,4,5].

Then I will assign even to [0,2,4] and odd to [1,3,5].

I'll try out your suggestion though


The syntax [::x] means to get every element starting at index 0 ending at index x, which I think is what you want after reordering the array.

My bad on the first suggestion I didn't really understand the scope of your problem. I also think you might want (n/2)-1? I'm not really a python expert I've only ever played with it a few times
Member
Posts: 4,625
Joined: Jul 3 2009
Gold: 6,520.00
Jun 29 2014 03:19pm
Quote (mebeatyou @ Jun 29 2014 05:07pm)
The syntax [::x] means to get every element starting at index 0 ending at index x, which I think is what you want after reordering the array.

My bad on the first suggestion I didn't really understand the scope of your problem. I also think you might want (n/2)-1? I'm not really a python expert I've only ever played with it a few times


No don't think it's working sadly.. Thanks for the help though
Member
Posts: 6,481
Joined: Jul 5 2009
Gold: 0.70
Jun 29 2014 03:30pm
Just ran a quick test:

Code
elements = [0,1,2,3,4,5]
n = len(elements)

newElements = [0,2,4,5,3,1]
even = newElements[:n/2]

print even


This printed an array of [0,2,4]


EDIT: That was python2 code, if using python3 it appears that python3 doesn't like math in the list slicing syntax, so I had to store another variable off:


firstHalf = n/2

even = newElements[:firstHalf]

This post was edited by mebeatyou on Jun 29 2014 03:31pm
Member
Posts: 4,625
Joined: Jul 3 2009
Gold: 6,520.00
Jun 29 2014 03:44pm
Quote (mebeatyou @ Jun 29 2014 05:30pm)
Just ran a quick test:

Code
elements = [0,1,2,3,4,5]
n = len(elements)

newElements = [0,2,4,5,3,1]
even = newElements[:n/2]

print even


This printed an array of [0,2,4]


EDIT: That was python2 code, if using python3 it appears that python3 doesn't like math in the list slicing syntax, so I had to store another variable off:


firstHalf = n/2

even = newElements[:firstHalf]


ohhh lol thank you. never thought about different of python2 and python3. I am fairly new to python :)

I'll try that now

edit: the gods hate me. still not working

This post was edited by SleepyUnit on Jun 29 2014 03:46pm
Member
Posts: 6,481
Joined: Jul 5 2009
Gold: 0.70
Jun 29 2014 03:51pm
If you wanna post full code here I can try and run it myself and see if I can help you debug
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll