【问题标题】:Why does one of my strings get repeated over and over again after I try为什么我的一根弦在我尝试后一遍又一遍地重复
【发布时间】:2021-01-01 21:19:53
【问题描述】:

我正在尝试将各种字符串连接在一起以形成一条消息。

理想的输出如下所示:

The following stocks have changed with more than 0.9%:\
MSFT: 0.05\
TSLA: 0.012\
AAPL: 0.019

下面是我的代码:

stocks = {
    "MSFT":0.05,
    "TSLA":0.012,
    "AAPL":0.019,
}

subject = 'Opening Price Warnings'
body = 'The following stocks have changed with more than 0.9%: \n'.join([f'{key}: {value}' for key, value in stocks.items()])
msg = f'Subject: {subject}\n\n{body}'
print(msg)

我的代码一遍又一遍地重复第一行。
我收到这样的消息:\

MSFT: 0.05The following stocks have changed with more than 0.9%:\
TSLA: 0.012The following stocks have changed with more than 0.9%:\
AAPL: 0.019The following stocks have changed with more than 0.9%:

为什么会这样?

【问题讨论】:

  • 最初,您提供了stocks 字典的英文描述。为了回答您的问题,我们需要能够运行您的代码,并查看错误是什么。即使您的代码是错误的,也不要忽略生成测试数据所需的代码。否则,我们必须自己编写代码来生成stocks 字典,这只是延迟问题。修剪你的代码,这样例子就很短了,但是如果你需要一个名为 stocks 的字典,那么请向我们展示代码来制作一个 stocks 字典的小有效示例。
  • 我编辑了您的问题以包含生成示例 stocks 字典的代码。但是,下次您在 stack-overflow 上发帖时,请包含我们可以自己运行您的代码的所有必要代码。我们需要能够运行它并自己查看控制台输出。如果stocks 字典丢失,我们就不能这样做。
  • @SamuelMuldoon 谢谢,我还在学习 python 所以这对我来说是全新的,我想我是怎么写的就足够了。我将在以后的问题中添加更多信息:)

标签: python string dictionary email formatting


【解决方案1】:

因为在body 中,您将相同 初始字符串加入到所有 for 循环字符串的条目中。

只有body = 'The following stocks have changed with more than 0.9%: 并创建一个仅包含 dict 元素的 new 字符串。

stocks = {'MSFT': 0.05, 'TSLA': 0.012}
body1 = 'The following stocks have changed with more than 0.9%: \n'
body2 = '\n'.join([f'{key}: {value}' for key, value in stocks.items()])
body1 + body2
# 'The following stocks have changed with more than 0.9%: \nMSFT: 0.05\nTSLA: 0.012\nAAPL: 0.019'

【讨论】:

    【解决方案2】:

    str.join() 不是一个连接两个字符串的函数。

    join 函数用于插入分隔符。
    分隔符分隔字符串容器中的元素。

    一个例子如下所示:

    eldest_cookie_jar = [
        "red",
        "blue",
        "green"
    ]
    
    s = "-".join(eldest_cookie_jar)
    print(s)
    

    控制台输出为:

    red-blue-green
    

    在上面的示例中,我使用连字符 (-) 作为分隔符。
    所有颜色都用连字符分隔。

    模式是:

    output = delimiter.join(non_delimiters)

    非分隔符是字符串形式的实际数据。

    如果您想用逗号分隔数字,则将数字转换为字符串,然后调用str.join(),如下所示:

    stryng = ",".join(numbers)     
    

    如果您想制作三明治,那么我们有:

    buns = ["(top bun)", "(bottom bun)"]
    meat = "(meat)"
    
    result = meat.join(buns)
    print(result)
    # "(top bun)(meat)(bottom bun)"
    

    下面还有更多示例;包括为了荒谬地详尽:

    eldest_cookie_jar = [
        "red",
        "blue",
        "green"
    ]
    
    # It is important to convert data to strings
    # before using `str.join()` to delimit the data
    #
    # `map(str, data)` will convert each element
    # of `data` into a string.
    #
    
    youngest_cookie_jar = tuple(map(str, [0.1, 0.2, 0.3]))
    
    meta_container = [
        eldest_cookie_jar,
        youngest_cookie_jar
    ]
    
    delims = [
        ", ",
        "...",
        ":--:",
        " ",
        "   ¯\_(ツ)_/¯   "
    ]
    
    for container in meta_container:
        for delimiter in delims:
            result = delimiter.join(container)
            beginning = "\n----------\n"
            print(result, end=beginning)
    

    控制台输出为:

    red, blue, green
    ----------
    red...blue...green
    ----------
    red:--:blue:--:green
    ----------
    red blue green
    ----------
    red   ¯\_(ツ)_/¯   blue   ¯\_(ツ)_/¯   green
    ----------
    0.1, 0.2, 0.3
    ----------
    0.1...0.2...0.3
    ----------
    0.1:--:0.2:--:0.3
    ----------
    0.1 0.2 0.3
    ----------
    0.1   ¯\_(ツ)_/¯   0.2   ¯\_(ツ)_/¯   0.3
    ----------
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-19
      • 2015-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多