user4488000
user4488000

Reputation:

Index a list python

I have a text file with tuples in it that I would like to convert to a list with indices as follows:

2, 60;
3, 67;
4, 67;
5, 60;
6, 60;
7, 67;
8, 67;

Needs to become:

60, 2 5 6
67, 3 4 7 8

And so on with many numbers... I've made it as far as reading in the file and getting rid of the punctuation and casting it as ints, but I'm not quite sure how to iterate through and add multiple items at a given index of a list. Any help would be much appreciated!

Here is my code so far:

with open('cues.txt') as f:
    lines = f.readlines()

arr = []   
for i in lines:
    i = i.replace(', ', ' ')
    i = i.replace(';', '')
i = i.replace('\n', '')
arr.append(i)

array = []   
for line in arr: # read rest of lines
    array.append([int(x) for x in line.split()])

arr = []
#make array of first values 40 to 80
for i in range(40, 81):
    arr.append(i)
print arr

for j in range(0, len(array)):
    for i in array:
        if (i[0] == arr[j]):
            arr[i[0]].extend(i[1])

Upvotes: 0

Views: 64

Answers (5)

thinkerou
thinkerou

Reputation: 1877

You should use dict to save text data, the following code:

d = {}
with open('cues.txt') as f:
    lines = f.readlines()
    for line in lines:
        line = line.split(',')
        key = line[1].strip()[0:-1]
        if d.has_key(key):
            d[key].append(line[0])
        else:
            d[key] = [line[0]]

for key, value in d.iteritems():
    print "{0}, {1}".format(key, " ".join(value))

Upvotes: 0

Anthony Kong
Anthony Kong

Reputation: 40624

1) This code is wrong at many level. See inline comment

arr = []   
for i in lines:
    i = i.replace(', ', ' ')
    i = i.replace(';', '')
i = i.replace('\n', '')  # Wrong identation. You will only get the last line in arr
arr.append(i)

You can simply do

arr = []   
for i in lines:
    i = i.strip().replace(';', '').split(", ")
    arr.append(i)

It will remove newline character, remove ; and nicely split a line into a tuple of (index, value)

2) This code can be simplified to one line

arr = [] # It should not be named `arr` because it destroyed the arr created in stage 1
for i in range(40, 81):
    arr.append(i)
print arr

becomes:

result = range(40, 81)

But it is not an ideal data structure for your problem. You should use dictionary instead. In the other word, you can lose this bit of code altogether

3) Finally you are ready to iterate arr and build the result

  result = defaultdict(list)
  for a in arr:
    result[a[1]].append(a[0])

Upvotes: 1

AChampion
AChampion

Reputation: 30258

Do you need it in a list you can simply collect them into a dict:

i = {}
with open('cues.txt') as f:
    for (x, y) in (l.strip(';').split(', ') for l in f):
        i.setdefault(y, []).append(x)
for k, v in i.iteritems():
    print "{0}, {1}".format(k, " ".join(v))

Upvotes: 2

Rain Lee
Rain Lee

Reputation: 531

You can use a dict to store the index:

results = {}
with open("cues.txt") as f:
    for line in f:
        value, index = line.strip()[:-1].split(", ")
        if index not in results:
            results[index] = [value]
        else:
            results[index].append(value)

for index in results:
    print("{0}, {1}".format(index, " ".join(results[index]))

Upvotes: 1

Avinash Raj
Avinash Raj

Reputation: 174696

You could use defaultdict function from collections module.

from collections import defaultdict
with open('file') as f:
  l = []
  for line in f:
    l.append(tuple(line.replace(';','').strip().split(', ')))
  m = defaultdict(list)
  for i in l:
    m[i[1]].append(i[0])
  for j in m:
    print j+", "+' '.join(m[j])

Upvotes: 1

Related Questions