0

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])
4
  • first up, you want to tuck i = i.replace('/n', '') and arr.append(i) under the for loop -- i.e., indent them four spaces. Commented Apr 6, 2015 at 3:43
  • second, when you make arr for the second time, you don't need a for loop. you can just set arr = range(40, 81). Commented Apr 6, 2015 at 3:45
  • why the 40 to 80 range? Commented Apr 6, 2015 at 3:48
  • Python doesn't support a sparse list (arbitrary indexes), so you will need to think about a different data structure, e.g. dict Commented Apr 6, 2015 at 3:56

5 Answers 5

2

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))
Sign up to request clarification or add additional context in comments.

Comments

1

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])

Comments

1

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]))

Comments

1

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])

Comments

0

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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.