Hi!
In every iteration of reading a file I populated a dictionary and appended it to a list .
A part of that list looks like this :
.
.
. Key Doc ID freq line number
{'Emin': ({'file13.txt': 1}, {'Emin': [38]})}

{'Gun': ({'file13.txt': 2}, {'Gun': [43, 71]})}

{'I': ({'file13.txt': 1}, {'I': [47]})}

{'Uday': ({'file13.txt': 3}, {'Uday': [1, 28, 66]})}

{'Uday': ({'file18.txt': 2}, {'Uday': [43, 48]})}

{'ad': ({'file18.txt': 1}, {'ad': [5]})}
.
.
.Now if I want to extract the line numbers for the key "Uday" using the query

for i in grail:
   for k in i.keys():
    if (k == "Uday"):
     print i[k][1]["Uday"]

Now I am having problems separating the line numbers for both the entries.If I use
i[k][1]["Uday"][0] it prints out "[1]"
"[43]"
I know it should have been dealt with in the early stages of implementing the data structure but is there a way now to get around this. What I want is to store all these line numbers in a separate array.
Thanks in advance

Recommended Answers

All 2 Replies

Something like this?

grail = [{'Emin': ({'file13.txt': 1}, {'Emin': [38]})},
{'Gun': ({'file13.txt': 2}, {'Gun': [43, 71]})},
{'I': ({'file13.txt': 1}, {'I': [47]})},
{'Uday': ({'file13.txt': 3}, {'Uday': [1, 28, 66]})},
{'Uday': ({'file18.txt': 2}, {'Uday': [43, 48]})},
{'ad': ({'file18.txt': 1}, {'ad': [5]})}]

print [ linenumber for lines in (item["Uday"][1]["Uday"]
                                 for item in grail
                                 if "Uday" in item)
        for linenumber in lines ]

Awesome! :) You're the man!! Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.