0

I have a problem when I want to extract some labels from file. I'm talking about 2000 labels, and I would like to use them from file and give them some size characteristic.

with open("filename") as f:
    content = f.readlines()

nsize= { "Mary": 1, "John": 1, "Jack": 1, "Ted": 5 }

this is example for 4 labels. I need it for all 2000. What is the easiest way to do that?

3
  • 9
    Sorry - but I don't actually understand what your question is... How do the labels, a line in a file, and a size characteristic link together? More context please? Commented Jan 8, 2013 at 18:03
  • Please copy paste part of the file. Commented Jan 8, 2013 at 18:08
  • Its fine now. I found solution. Commented Jan 8, 2013 at 18:08

1 Answer 1

2

Use a dict comprehension:

with open("filename") as f:
    nsize = {el.strip(): len(el.strip()) for el in f}

This takes each line in f, strips() away whitespace, turns it into the key and the length of the label as the value.

If you meant to count them, use collection.Counter:

from collections import Counter

with open("filename") as f:
    nsize = Counter(el.strip() for el in f)

This takes each label from the file (again, strip()ing away extra whitespace), and the Counter dict will give you a count for each label in the file (so if label foo appears twice, nsize['foo'] is 2).

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

4 Comments

I will note though - shouldn't it just be Counter(el.strip() for el in f) ?
@JonClements: Oopsie, skipped over the documentation a tad too fast there. Corrected.
@MartijnPieters are you psychic? How did you know what he wanted?
@kojiro: Got lucky. There were two options I could think of, put them both in. Not certain still which one it was that helped..

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.