0

I have list of objects and I want to convert it to sparse matrix in python.

I want the matrix column to be tags and the rows to be titles (There may be duplicate tags between different titles)

I don't know what should I do?

data = [
  {
    'title': 'title1', 'tags': ['tag1', 'tag2', 'tag3']
  },
  {
    'title': 'title2', 'tags': ['tag1']
  }
]

I want something like this: enter image description here

user12217822
  • 292
  • 1
  • 5
  • 16
  • How many tags are there? Do you know this in advance? Are you using Pandas? If not, why not? What about numpy? – ddejohn Oct 05 '21 at 00:22

1 Answers1

0

For 0/1 matrix you can use next example:

data = [
    {"title": "title1", "tags": ["tag1", "tag2", "tag3"]},
    {"title": "title2", "tags": ["tag1"]},
]

# using sorted for having tag1 first, tag3 last:
tags = sorted({t for d in data for t in d["tags"]})

matrix = [[int(tt in d["tags"]) for tt in tags] for d in data]
print(matrix)

Prints:

[[1, 1, 1], 
 [1, 0, 0]]

For "pretty" print the matrix:

data = [
    {"title": "title1", "tags": ["tag1", "tag2", "tag3"]},
    {"title": "title2", "tags": ["tag1"]},
]

tags = sorted({t for d in data for t in d["tags"]})

print(("{:<10}" * (len(tags) + 1)).format("", *tags))
for d in data:
    print(
        ("{:<10}" * (len(tags) + 1)).format(
            d["title"], *[int(tt in d["tags"]) for tt in tags]
        )
    )

Prints:

          tag1      tag2      tag3      
title1    1         1         1         
title2    1         0         0         
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
  • Thanks, But I have another question I couldn't transpose this matrix. can you help me what should I do? matrix.transpose() doesn't work – user12217822 Oct 05 '21 at 13:03