joshkmartinez
joshkmartinez

Reputation: 664

Python: Rearrange a list by preconceived index

So I want to take an input file in with 3 lines. First line, is the number of items in the list Second line, the indexes on how I want to rearrange the list. (see below for more info) Third line, the list of numbers itself

So from the second line in the in.txt are the indexes for the rearranged list. Hard to explain but this is how it would work (see in.txt for reference) The first number stays the same, the second number becomes the third, the fifth number becomes the second, the forth becomes the fifth, etc.

Here some sample input: in.txt

5
1 3 4 5 2
12 33 96 84 74

output.txt

12
74
33
96
84

Here is the program itself:

fin = open('in.txt','r') #file in
fout = open('output.txt','w') #file out

indata = fin.readlines()
cownum = int(indata[0])
print(cownum)
posmove = indata[1]
cid = indata[2]

poss=posmove.split()
ids=cid.split()

print(poss)
print("")

i=0
while i != int(cownum):
  print(poss[i])
  ids.insert((int(poss[i])-1), ids.pop(i))
  i=i+1
  print(i)
  print(ids)


newids = str(ids)
fout.write(newids)


fin.close()
fout.close()

just for some background info, the numbers in the list are going to be somewhat like product id's

The problem now is that I rearrange items twice.

Sorry if I am a bit unclear (lol and for the weird variable names)

Upvotes: 0

Views: 374

Answers (3)

AlanK
AlanK

Reputation: 9833

Once you read your files into lists you can use the following code to sort them:

>>> indexes = [1, 3, 4, 5, 2]
>>> values = [12, 33, 96, 84, 74]
>>> newlist = [0, 0, 0, 0, 0]
...
...
>>> for x, y in zip(indexes, values):
...     newlist[x-1] = y
... 
>>> for x in newlist:
...     print(x)
... 
12
74
33
96
84

Upvotes: 2

plamut
plamut

Reputation: 3206

Assuming that you already managed to extract the lists of positions and list of product ids (you will still need to convert data to integers):

positions = [1, 3, 4, 5, 2]
product_ids = [12, 33, 96, 84, 74]

you can sort product IDs by given positions using a pattern called decorate-sort-undecorate:

>>> [prod_id for (pos, prod_id) in sorted(zip(positions, product_ids))]
[12, 74, 33, 96, 84]

Step by step explanation:

We first decorate the list of product IDs with their corresponding positions:

>>> zip(positions, product_ids)
[(1, 12), (3, 33), (4, 96), (5, 84), (2, 74)]

We then sort the decorated list - this will give us a correct order, because tuples are sorted item-wise, and since the first item is a position, this gives us the order we want.

>>> sorted(zip(positions, product_ids))
[(1, 12), (2, 74), (3, 33), (4, 96), (5, 84)]

Finally, we undecorate (unpack) the now sorted list of tuples to obtain a list of product IDs in desired order (while discarding their position info):

>>> [prod_id for (pos, prod_id) in sorted(zip(positions, product_ids))]
[12, 74, 33, 96, 84]

(used Python 2 for the demo)

Upvotes: 1

Ajax1234
Ajax1234

Reputation: 71451

You can try this:

file_data = [map(int, i.strip('\n').split()) for i in open('filename.txt')]
new_data = [i[-1] for i in sorted(zip(file_data[1], file_data[-1]), key=lambda x:x[0])]

Output:

[12, 74, 33, 96, 84]

Upvotes: 2

Related Questions