2

I have a file with its contents like this:

1   257.32943114
10  255.07893867
100     247.686049588
1000    248.560238357
101     250.673715233
102     250.150281581
103     247.076694596
104     257.491337952
105     250.804702983
106     252.043717069
107     253.786482488
108     255.588547067
109     251.253294801
...

What I want to do is create an array from this list with the numbers in the first column as index. For example, the 1st element of the array will be 257.32943114 which corresponds to 1 in the list, the 109th element of the array will be 251.253294801 which corresponds to number 109 in the list, and so on. How can I achieve this in Python?

4
  • 1
    And what about the value where there is no explicit index? Like what is the value at [5]? Commented May 19, 2015 at 16:33
  • 2
    A dictionary might work well here Commented May 19, 2015 at 16:37
  • What do you mean by "indexed list"? Commented May 19, 2015 at 16:38
  • @Cyber This is just a sample from the file. It's a reduce output with the key and value outputted in this way. There's a value for 1 to 1000. Commented May 19, 2015 at 18:05

3 Answers 3

1

If you insist on using list, here is another more pythonic solution:

with open('test.in', 'r') as f:
    r = []
    map(lambda (a,b): [0, [r.append(0) for i in xrange(a - len(r))]] and r.append(b), sorted([(int(l.split(' ')[0]), float(l.split(' ')[-1])) for l in f], key=lambda (a,b): a))

And r is what you are looking for.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a great deal @skies457 After I run this solution and print r, the first element of the array is 0. I think it's from r.append(0). I have tried to change/remove the 0 to no avail. Do you know a way I can remove the 0 as the first element of the array?
@Amanda I'm assuming the index starts from 0.... You can simply take r=r[1:], that is, keep all items in r except the first.
1

Separator: you can use tab or spaces in split line

file = open(location, 'r')
dictionary = {}
for line in file.readlines():
    aux = line.split('  ') #separator
    dictionary[aux[0]] = aux[1]
print dictionary

If you have something like '257.32943114\n' like your values, you can use instead dictionary[aux[0]] = aux[1][:-1] to evade the char of new line.

4 Comments

From the question, it doesn't look like the number of spaces is equal throughout. I think it's a good idea to split using the re module for this: aux = re.split(r' +', line)
Hi @Carlos, thanks for your help. When I 'print dictionary[aux[0]]' I just get only the numbers in the second column, but it's unsorted. I want to sort the numbers in the second columns based on the corresponding index in the first column. Please, how can I achieve this?
i used tab key and not spaces, and it works... maybe all depends from file format... TY for your support @AlokShankar
@Amanda if you wanna sort, you cannot do it on a dictionary...you can sort in a list... You can get all keys from dictionary in a list, sort them, and show values using sorted keys list.. Need sort by 1st or 2nd column?
1

Likely you want a dictionary, not a list, but if you do want a list:

def insert_and_extend(lst, location, value):
   if len(lst) <= location:
      lst.extend([None] * (location - len(lst) + 1))
   lst[location] = value

mylist = []
insert_and_extend(mylist, 4, 'a')
insert_and_extend(mylist, 1, 'b')
insert_and_extend(mylist, 5, 'c')
print mylist

To do it as dictionary:

dict = {}
dict[4] = 'a'
dict[1] = 'b'
dict[5] = 'c'
print dict

2 Comments

I agree. I think you're looking for a dictionary. If you are using the first number only as an index, then a dictionary would be perfect. Check out this code snippet if a dictionary solution makes sense for you application
Hi MK, thanks for your comment. I didn't know Python had dictionary for this. Can you give me an idea how to implement it? Thanks again.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.