Bottom Page

Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Making indices and rearrranging list
#1
Good day,

I'm fairly new to Python. I have generated a list and want to rearrange it. I want to know what's the best way to go about doing that — should I use indices or a numpy function?

This is a portion of my 24 by 24 grid that contains 576 entries. 
['-19.2740144', '-17.9460128', '-18.8874128', '-22.9789464', '-27.8428464'] This is the data set with only 5 entries. 

1. Is this the right format to manipulate the numbers? Should I remove the quotation marks using 'strip'?
2. I want to keep the first entry, then reverse the order of the indices, so instead of having 1, 2, 3, 4, 5, I would have 1, 5, 4, 3, 2. For a set of 24 numbers in the list. 
3. There's 24 sets of 24. I want to perform strategy 2, then conduct the same procedure on the global list of 576, which means after rearranging I would have the first set of 24, then the 24th set, 23rd set and so on. 

These are some of the things I've tried


F=open('tranform.txt','r+')
for line in F
      for index in xrange(23,0,1):
           print index


*** An example I saw that could be worth using — Would I have to modify definition of the sorted? ***
def get_list_indices(ls):
    indices = {}
    for n, i in enumerate(ls):
        try:
            indices[i].append(n)
        except KeyError:
            indices[i] = [n]
    return [i[1] for i in sorted(indices.items(), reverse=True)]

#test_list = [4, 1, 4, 8, 5, 13, 2, 4, 3, 7, 14, 4, 4, 9, 12, 1, 6, 14, 10, 8, 6, 4, 11, 1, 2, 11, 3, 9]
#print(get_list_indices(list))
Thanks for any help!
Quote
#2
(May-18-2017, 10:26 AM)reidybwoykeon Wrote: 1. Is this the right format to manipulate the numbers? Should I remove the quotation marks using 'strip'?

Most probably no and No.
At the moment what you show is list of str objects. It's not produced by your code. You need to convert list elements to numbers by using float() built-in function. It looks like you plan to read these from file, so this could be done at the time you read from the file.
Your input and final goal are unclear.
You may arrange your data as list of lists and process them with trivial python code. Or you may also use pandas and create pandas.dataframe, then rearrange columns of the dataframe.


(May-18-2017, 10:26 AM)reidybwoykeon Wrote: 2. I want to keep the first entry, then reverse the order of the indices, so instead of having 1, 2, 3, 4, 5, I would have 1, 5, 4, 3, 2. For a set of 24 numbers in the list.
3. There's 24 sets of 24. I want to perform strategy 2, then conduct the same procedure on the global list of 576, which means after rearranging I would have the first set of 24, then the 24th set, 23rd set and so on.

if you stick to list of lists, what you need to do is to create function that rearrange any list that you pass as argument in the desired format. Then you may loop trough your list of lists and rearrange each individual list in place. Finally you can pass the entire list of lists to same function to rearrange it too.
Quote
#3
Hi buran,

I follow what you're saying. I'll take a stab at it and regain.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  TypeError: list indices must be integers or slices, not str TheDovah7 4 8,494 Mar-06-2018, 03:00 PM
Last Post: TheDovah7
  Making a list for positive vs negative reviews based on rating fancy_panther 1 1,363 Mar-22-2017, 11:30 PM
Last Post: zivoni

Forum Jump:


Users browsing this thread: 1 Guest(s)