question 128 Comprehensions · medium
One row per tag
A question export stores all of a question's tags in one text, separated by |. A reporting database wants one row per tag instead, like a SQL table: {"question_id": …, "tag": …}. Tags are cleaned (spaces at the ends removed, lower case), and empty pieces, which come from a stray | or a question with no tags, are skipped.
Function
tag_rows(questions)questionsa list of dictionaries with
"question_id" and "tags" (text such as "python|dataclasses")Returnsa new list of new dictionaries
{"question_id": …, "tag": …}, questions in order and each question's tags in orderMust notchange the question dictionaries
Examples
calltag_rows([{'question_id': 70015316, 'tags': 'python|dataclasses'}, {'question_id': 70015420, 'tags': ' Pandas |dataframe|'}, {'question_id': 70015577, 'tags': ''}])returns[{'question_id': 70015316, 'tag': 'python'}, {'question_id': 70015316, 'tag': 'dataclasses'}, {'question_id': 70015420, 'tag': 'pandas'}, {'question_id': 70015420, 'tag': 'dataframe'}]
calltag_rows([{'question_id': 1, 'tags': 'sql'}])returns[{'question_id': 1, 'tag': 'sql'}]
calltag_rows([])returns[]