problem with list

This is the place for queries that don't fit in any of the other categories.

problem with list

Postby angus1964 » Sun Oct 20, 2013 9:19 am

I have a list
Code: Select all
[['a', 'd'], ['b', 'c'], ['d', 'c'], ['a', 'b'], ['b', 'd'], ['c', 'a'], ['a', 'd'], ['b', 'c'], ['d', 'c'], ['a', 'b'], ['b', 'd'], ['c', 'a']]


i want to reverse the items in the second half of it.
Ihave tried
Code: Select all
for fix in list[6:11]:
         fix.reverse()


but this reverses the whole list.
Is there an easier way to do this.

Many thanks in advance

Angus
Last edited by Mekire on Sun Oct 20, 2013 12:12 pm, edited 1 time in total.
Reason: Lock.
angus1964
 
Posts: 14
Joined: Wed Aug 21, 2013 4:42 pm

Re: problem with list

Postby micseydel » Sun Oct 20, 2013 9:47 am

I don't see the whole list being reversed. What different result are you trying to achieve from the below?
Code: Select all
>>> my_list = [['a', 'd'], ['b', 'c'], ['d', 'c'], ['a', 'b'], ['b', 'd'], ['c', 'a'], ['a', 'd'], ['b', 'c'], ['d', 'c'], ['a', 'b'], ['b', 'd'], ['c', 'a']]
>>> for fix in my_list[len(my_list)/2:]:
         fix.reverse()

         
>>> print my_list
[['a', 'd'], ['b', 'c'], ['d', 'c'], ['a', 'b'], ['b', 'd'], ['c', 'a'], ['d', 'a'], ['c', 'b'], ['c', 'd'], ['b', 'a'], ['d', 'b'], ['a', 'c']]
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 929
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: problem with list

Postby angus1964 » Sun Oct 20, 2013 12:06 pm

Hi micseydel

When i tried your solution i got the following error.
Code: Select all
slice indices must be integers or None or have an __index__ method


Im using python 3.3 if thats creating any problems.
angus1964
 
Posts: 14
Joined: Wed Aug 21, 2013 4:42 pm

Re: problem with list

Postby Mekire » Sun Oct 20, 2013 12:11 pm

Python 3 defaults to true division (2 defaults to integer).

Change this:
Code: Select all
len(my_list)/2
to this:
Code: Select all
len(my_list)//2

-Mek
User avatar
Mekire
 
Posts: 816
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: problem with list

Postby angus1964 » Sun Oct 20, 2013 12:22 pm

Hi Mek thanks for the reply.
i have the following function.
Code: Select all
def revfix(list):
   print(fixtureList)
   for fix in list[len(list)//2:]:
         fix.reverse()
   print(fixtureList)


which i pass the above list.
when i run this the full list is reversed.
Just cant see why.
angus1964
 
Posts: 14
Joined: Wed Aug 21, 2013 4:42 pm

Re: problem with list

Postby stranac » Sun Oct 20, 2013 12:27 pm

What's fixtureList?
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 893
Joined: Thu Feb 07, 2013 3:42 pm

Re: problem with list

Postby angus1964 » Sun Oct 20, 2013 12:28 pm

Sorry its the list thats in the first post.
angus1964
 
Posts: 14
Joined: Wed Aug 21, 2013 4:42 pm

Re: problem with list

Postby Mekire » Sun Oct 20, 2013 12:41 pm

Not sure what the problem is.

This seems to do what you are asking:
Code: Select all
my_list = [['a', 'd'], ['b', 'c'], ['d', 'c'], ['a', 'b'], ['b', 'd'],
           ['c', 'a'],['a', 'd'], ['b', 'c'], ['d', 'c'], ['a', 'b'],
           ['b', 'd'], ['c', 'a']]


def reverse_half(list_of_lists):
    for fix in list_of_lists[len(list_of_lists)//2:]:
        fix.reverse()
Code: Select all
>>> print(my_list)
[['a', 'd'], ['b', 'c'], ['d', 'c'], ['a', 'b'], ['b', 'd'], ['c', 'a'], ['a', 'd'], ['b', 'c'], ['d', 'c'], ['a', 'b'], ['b', 'd'], ['c', 'a']]
>>> reverse_half(my_list)
>>> print(my_list)
[['a', 'd'], ['b', 'c'], ['d', 'c'], ['a', 'b'], ['b', 'd'], ['c', 'a'], ['d', 'a'], ['c', 'b'], ['c', 'd'], ['b', 'a'], ['d', 'b'], ['a', 'c']]
>>>
Oh, and also... don't use names of builtins for variable names. The name list is a builtin; don't use it as a variable name.

-Mek
User avatar
Mekire
 
Posts: 816
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: problem with list

Postby angus1964 » Sun Oct 20, 2013 12:53 pm

Your code worked when i ran it on its own but when i included it in my program it reversed the full list again.
angus1964
 
Posts: 14
Joined: Wed Aug 21, 2013 4:42 pm

Re: problem with list

Postby Mekire » Sun Oct 20, 2013 12:54 pm

Going to need to see your whole program.

-Mek
User avatar
Mekire
 
Posts: 816
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: problem with list

Postby angus1964 » Sun Oct 20, 2013 1:13 pm

Here is the full code.
There is another program which creates fixtureList
Code: Select all
import pickle
import os

def reverse_half(list_of_lists):
    for fix in list_of_lists[len(list_of_lists)//2:]:
        fix.reverse()
       
fixlist = open("fixtures", 'rb')

fixtureList = pickle.load(fixlist)
numberOfTeams = pickle.load(fixlist)
print(fixtureList)
reverse_half(fixtureList)
print(fixtureList)
print(numberOfTeams)

fixlist.close()


When i run this the first print is fine and the second is the whole list reversed!!!!
angus1964
 
Posts: 14
Joined: Wed Aug 21, 2013 4:42 pm

Re: problem with list

Postby Mekire » Sun Oct 20, 2013 2:04 pm

My guess is that your pickled data is nested one level deeper than you think it is.

Could you show me exactly what the results of your print statements are (direct copy and paste; no editing whatsoever)

-Mek
User avatar
Mekire
 
Posts: 816
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: problem with list

Postby angus1964 » Sun Oct 20, 2013 2:07 pm

Here is the output.
Code: Select all
[['a', 'd'], ['b', 'c'], ['d', 'c'], ['a', 'b'], ['b', 'd'], ['c', 'a'], ['a', 'd'], ['b', 'c'], ['d', 'c'], ['a', 'b'], ['b', 'd'], ['c', 'a']]
[['d', 'a'], ['c', 'b'], ['c', 'd'], ['b', 'a'], ['d', 'b'], ['a', 'c'], ['d', 'a'], ['c', 'b'], ['c', 'd'], ['b', 'a'], ['d', 'b'], ['a', 'c']]
angus1964
 
Posts: 14
Joined: Wed Aug 21, 2013 4:42 pm

Re: problem with list

Postby angus1964 » Sun Oct 20, 2013 2:17 pm

Also had the same results when function was used in program that created fixtureList, so it cant really be a problem with the pickling.
angus1964
 
Posts: 14
Joined: Wed Aug 21, 2013 4:42 pm

Re: problem with list

Postby Mekire » Sun Oct 20, 2013 2:38 pm

Let me know the results of this one:
Code: Select all
def reverse_half(list_of_lists):
    for i,item in enumerate(list_of_lists):
        if i >= len(list_of_lists)//2:
            item.reverse()

-Mek
User avatar
Mekire
 
Posts: 816
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: problem with list

Postby angus1964 » Sun Oct 20, 2013 3:06 pm

still the same whole list reversed.
angus1964
 
Posts: 14
Joined: Wed Aug 21, 2013 4:42 pm

Re: problem with list

Postby Mekire » Sun Oct 20, 2013 3:13 pm

Ok...

Please place this print statement in the previous function:
Code: Select all
def reverse_half(list_of_lists):
    print(len(list_of_lists)//2)
    for i,item in enumerate(list_of_lists):
        if i >= len(list_of_lists)//2:
            item.reverse()

Confirm if the result is 6 or not. Honestly pretty confused myself. Let me see all the code. The generation of the list as well (minus the pickling).

-Mek
User avatar
Mekire
 
Posts: 816
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: problem with list

Postby Mekire » Sun Oct 20, 2013 3:18 pm

Omg... I just realized what you are doing...

You have the same lists within your list multiple times.

Do an experiment for me.

Try this:
Code: Select all
my_list[0][0] = "I changed in two places"
print(my_list)

The problem is having multiple references to the same object within the main list.

Code: Select all
>>> my_list = [['a', 'd'], ['b', 'c'], ['d', 'c'], ['a', 'b'], ['b', 'd'], ['c', 'a']]*2
>>> my_list[0][0] = "I changed twice"
>>> print(my_list)
[['I changed twice', 'd'], ['b', 'c'], ['d', 'c'], ['a', 'b'], ['b', 'd'], ['c', 'a'], ['I changed twice', 'd'], ['b', 'c'], ['d', 'c'], ['a', 'b'], ['b', 'd'], ['c', 'a']]
>>>

-Mek
User avatar
Mekire
 
Posts: 816
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: problem with list

Postby angus1964 » Sun Oct 20, 2013 4:02 pm

Yes i see the problem now will have to go back to the drawing board.

help much appreciated

Angus
angus1964
 
Posts: 14
Joined: Wed Aug 21, 2013 4:42 pm

Re: problem with list

Postby Mekire » Sun Oct 20, 2013 4:07 pm

You might be interested in deepcopy. It will allow you to create copies of nested mutables.

Code: Select all
from copy import deepcopy

base_list = [['a', 'd'], ['b', 'c'], ['d', 'c'],
             ['a', 'b'], ['b', 'd'], ['c', 'a']]

my_list = base_list + deepcopy(base_list)

-Mek
User avatar
Mekire
 
Posts: 816
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Next

Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests