Search code examples
pythonlistappendconditional-statementsdoc

Conditional appending items from a list into multiple documents


I am pretty new to python. My question is related to appending data based on index[0] into a set of documents (e.g. doc1,doc2).

g = [('elsiescdesign', 'flowers', 6),('elsiescdesign', 'running', 6), ('fulviomeloni', 'flowers', 1), ('connor-burrows', 'flowers', 1), ('withoutroots', 'flowers', 1), ('thegreenraven', 'flowers', 2), ('taylor-music', 'flowers', 2), ('eda11y', 'flower', 7), ('2liice', 'flower', 1)]

Above is the sample data: [(blogger,tag,countsoftagusedbyblooger)]

I want to append all the bloggers tags into a document each e.g.

elsiescdesign =['flowers','flowers','flowers','flowers','flowers','flowers','running','running','running','running','running','running']

But not just for one bloggers, all the bloggers here. The reason for this is so that I can build my topic model.


Solution

  • try this

    bloggers = {}
    for b in g:
        bloggers.setdefault(b[0], []).extend([b[1]] * b[2])