【问题标题】:Store results of nested for loop as single concatenated string将嵌套 for 循环的结果存储为单个连接字符串
【发布时间】:2023-02-07 04:41:59
【问题描述】:

我正在尝试将下面函数的值存储到单个字符串中,我可以将其输入到利用 F 字符串的查询中。输出看起来是正确的,但实际上只是一些单独的打印语句。

如何将下面的输出存储到单个字符串中?

import pandas as pd
view_dict = [{'id':'168058','viewtime_min':'2023-01-26 21:00:59.435 -0600','viewtime_max':'2023-01-26 21:59:59.435 -0600'},
                 {'id':'167268','viewtime_min':'2023-01-26 21:59:59.435 -0600','viewtime_max':'2023-01-26 21:59:59.435 -0600'},
                 {'id':'167268','viewtime_min':'2023-01-26 21:59:59.435 -0600','viewtime_max':'2023-01-26 21:59:59.435 -0600'}] 

def get_where_clause(view_dictionary: dict):
    where_clause = " "
    for index in range(len(view_dictionary)): 
        if index != max(range(len(view_dictionary))):
            print(f'''(b.id = {view_dictionary[index]['id']}
                and b.viewed_at between coalesce({view_dictionary[index]['viewtime_min']},published_at) and {view_dictionary[index]['viewtime_max']})
                or''')
        else:
            print(f'''(b.id = {view_dictionary[index]['id']}
                and b.viewed_at between coalesce({view_dictionary[index]['viewtime_min']},published_at) and {view_dictionary[index]['viewtime_max']})''')

x = get_where_clause(view_dict)

x

我期望这会存储到一个值,但是在访问该值时不会存储任何内容。

【问题讨论】:

  • get_where_clause 不返回任何内容,也不更改字典
  • 输出如下...我试图将其保存为单个字符串而不是 3 个单独的打印语句。 ```(b.id = 168058 和 b.viewed_at 在合并(2023-01-26 21:00:59.435 -0600,published_at)和 2023-01-26 21:59:59.435 -0600 之间)或(b.id = 167268 和 b.viewed_at 之间 coalesce(2023-01-26 21:59:59.435 -0600,published_at) 和 2023-01-26 21:59:59.435 -0600) or (b.id = 167268 and b.viewed_at between合并(2023-01-26 21:59:59.435 -0600,published_at)和 2023-01-26 21:59:59.435 -0600)```

标签: python python-3.9


【解决方案1】:

print 命令只是将文本写入屏幕,它不返回值。您的函数需要返回一个值才能在变量中“存储”。

我稍微重写了你的代码,它现在返回一个输出列表:

view_dict = [{'id':'168058','viewtime_min':'2023-01-26 21:00:59.435 -0600','viewtime_max':'2023-01-26 21:59:59.435 -0600'},
                 {'id':'167268','viewtime_min':'2023-01-26 21:59:59.435 -0600','viewtime_max':'2023-01-26 21:59:59.435 -0600'},
                 {'id':'167268','viewtime_min':'2023-01-26 21:59:59.435 -0600','viewtime_max':'2023-01-26 21:59:59.435 -0600'}] 

def get_where_clause(view_dictionary: dict) -> list:
    outputs = []
    where_clause = " "
    for index in range(len(view_dictionary)): 
        if index != max(range(len(view_dictionary))):
            output = (f'''(b.id = {view_dictionary[index]['id']}
                and b.viewed_at between coalesce({view_dictionary[index]['viewtime_min']},published_at) and {view_dictionary[index]['viewtime_max']})
                or''')
        else:
            output = (f'''(b.id = {view_dictionary[index]['id']}
                and b.viewed_at between coalesce({view_dictionary[index]['viewtime_min']},published_at) and {view_dictionary[index]['viewtime_max']})''')
        print(output)
        outputs.append(output)
    return outputs

x = get_where_clause(view_dict)

【讨论】:

  • 这非常有帮助,但我还有一个问题。输出返回 ' ' 和一些 dict 格式的随机空间。在下面复制+粘贴。您知道如何使输出的 SQL 清晰易读吗? ``` "['(b.id = 168058\n 和 b.viewed_at 介于 coalesce(2023-01-26 21:00:59.435 -0600,published_at) 和 2023-01-26 21:59:59.435 -0600 之间) \n or', '(b.id = 167268\n and b.viewed_at between coalesce(2023-01-26 21:59:59.435 -0600,published_at) and 2023-01-26 21:59:59.435 -0600) \n```
  • 这个sn-p中的where_clause有什么用?而不是返回 outputs 返回 "".join(outputs) 。如果您需要新行中的字符串,您可以将空字符串替换为您想要加入字符串的任何内容,例如" ".join(outputs)
  • @SahilYadav 在此 sn-p 中没有使用 where_clause,它只是原始代码的残余。事实上,您可以不以列表的形式返回输出,而是以连接的字符串形式返回。随意编辑我的答案,或者我会自己编辑。
猜你喜欢
  • 2021-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多