Python rows How to fill 5 even if the length is less than 5

Asked 2 years ago, Updated 2 years ago, 51 views

Hello, masters! I'm asking a question because it doesn't work out well after trying it for a few days alone.

    def make_news_cnts(self):
        rows = self.get_news_issue()
        row_html = get_row_html(rows)
        news_cnts = get_html(row_html)
        return {"news_cnts": news_cnts}

Here, rows is in dictionary format in the list as shown below.

rows= [
  {'stkname': 'Silla S.G.'; 'ratio': 29.7, 'header': 'Food company in the seafood processing and livestock distribution business'}; 
  {'stkname': 'CJ Seafood', 'ratio': 3.13; 'header': 'CJ Group-affiliated seafood manufacturing company'};
  {'stkname': 'Foodwell', 'ratio': -1.06, 'header': 'A product processing company that produces and sells fruit processing products'}
] 

When the length of rows is less than 5, I want to mark the column values stkname, ratio, and header as -, what should I do?

Desired Results

 [
  {'stkname': 'Silla S.G.'; 'ratio': 29.7, 'header': 'Food company in the seafood processing and livestock distribution business'}; 
  {'stkname': 'CJ Seafood', 'ratio': 3.13; 'header': 'CJ Group-affiliated seafood manufacturing company'}; 
  {'stkname': 'Foodwell', 'ratio': -1.06, 'header': 'A product processing company that produces and sells fruit processing products',  
  {'stkname':'-', 'ratio':'-', 'header:'-'},
  {'stkname':'-', 'ratio':'-', 'header:'-'}
]

It's like this! I would really appreciate your reply.

python list dictionary

2022-09-20 17:04

1 Answers

def make_news_cnts(self):
    rows = self.get_news_issue()
    if len(rows)<5:
        for i in range(5-len(rows)):
            row.append({'stkname':'-', 'ratio':'-', 'header':'-'})
    row_html = get_row_html(rows)
    news_cnts = get_html(row_html)
    return {"news_cnts": news_cnts}

Wouldn't this work?


2022-09-20 17:04

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.