looking for some solutions? You are welcome.

SOLVED: How to flip the dataset in most efficient way?

smhquery:

Assume we have a dataset like:

a: 1,2,3,5,6
b: 4,1,2
c: 1,4

Now we want to transform this dataset to:

1: a,b,c
2: a,b
3: a
4: b,c
5: a
6: a

This transform could be done by a dictionary but is there a way to do this more efficiently

Currently I do as below:

uFile = open("t/u.txt","r")
uDic = dict()
for cnt1, line in enumerate(uFile):
    lineAr = line.strip().split(' ')
    for item in lineAr:
        if item not in uDic.keys():
           uDic[item] = []
        uDic[item].append(cnt1)

And then save the output.



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
Share:

No comments:

Recent