【问题标题】:How to use a list with the format function如何使用带有格式功能的列表
【发布时间】:2019-11-22 12:16:36
【问题描述】:

提供的是有关商店库存的数据列表,其中列表中的每个项目都代表项目的名称、库存量以及成本。使用 .format 方法(不是字符串连接)以相同的格式打印出列表中的每个项目。例如,第一个打印语句应为商店有 12 只鞋子,每只 29.99 美元。

下面是我尝试使用的代码。

inventory = ["shoes, 12, 29.99", "shirts, 20, 9.99", "sweatpants, 25, 
15.00", "scarves, 13, 7.75"]

for person in inventory:
    item = person[0]
    quantity = person[1]
    amount = person[2]
    print("The store has {} {}, each for {} USD.".format(item, quantity, 
    amount))

我不明白为什么它只抓住单词的第一个字母。下面是我的输出。 输出:

The store has s h, each for o USD.
The store has s h, each for i USD.
The store has s w, each for e USD.
The store has s c, each for a USD.

【问题讨论】:

    标签: python arrays regex python-3.x string


    【解决方案1】:

    试试这个,

    inventory = ["shoes, 12, 29.99", "shirts, 20, 9.99", "sweatpants, 25, 15.00", "scarves, 13, 7.75"]
    
    for person in inventory:
        person = person.split(',')
        item = person[0]
        quantity = person[1]
        amount = person[2]
        print("The store has {} {}, each for {} USD.".format(item, quantity, amount))
    
    # output
    # The store has shoes  12, each for  29.99 USD.
    # The store has shirts  20, each for  9.99 USD.
    # The store has sweatpants  25, each for   15.00 USD.
    # The store has scarves  13, each for  7.75 USD.
    

    只需以这种方式拆分列表中的每个项目,

    当你拆分"shoes, 12, 29.99", "shirts, 20, 9.99".split(',') 时,你会得到["shoes, "12", "29.99"] 类型的列表。因此无需更改代码,只需在 for 循环后添加 person = person.split(',') 行。当你循环它时,每次你得到一个字符串。这就是每次循环时都会发生的情况。

    >>> 1st_str = "shoes, 12, 29.99"
    >>> 1st_str[0]
    's'
    >>> 1st_str[1]
    'h'
    >>> 1st_str[2]
    'o'
    

    我所做的只是拆分字符串,然后您可以轻松访问它。

    >>> 1st_str = "shoes, 12, 29.99"
    >>> split_str = 1st_str.split(',')
    >>> split_str
    ["shoes", "12", "29.99"]
    >>> split_str[0]
    'shoes'
    >>> split_str[1]
    '12'
    >>> split_str[2]
    '29.99'
    

    【讨论】:

    • @Dwr73244,我刚刚更新了代码,让您更了解代码!
    【解决方案2】:

    其他人的答案都很完美,但我建议拆分“,”,特别是对于 OP 给出的示例。与仅在“,”或“”上拆分相比,它将避免出现多余的空格或逗号。

    item, quantity, amount = person.split(", ")

    person_data = person.split(", ")
    item = person_data[0]
    quantity = person_data[1]
    amount = person_data[2]```
    

    【讨论】:

      【解决方案3】:

      变量person 将是列表中的每个元素,它是一个字符串。

      person[0] 将返回person 的第一个字符

      person[1] 将返回person 的第二个字符

      你要做的是用逗号分割字符串。

      您可以在字符串上使用.split() 方法来做到这一点

      item, quantity, amount = person.split(",")

      【讨论】:

        【解决方案4】:

        你的格式化函数没有错;您解析商品、数量和金额的方式是错误的。如果您仔细观察,inventorystrings 的列表,而不是列表的列表。这就是为什么当您打印item 时,它只打印出"s",因为这是字符串中的第一个字符。

        试试这个:

        inventory = [["shoes", "12", "29.99"], ["shirts", "20", "9.99"], ["sweatpants", "25", "15.00"], ["scarves", "13", "7.75"]]
        
        for person in inventory:
            item = person[0]
            quantity = person[1]
            amount = person[2]
            print("The store has {} {}, each for {} USD.".format(item, quantity, 
            amount))
        

        【讨论】:

          【解决方案5】:

          这是因为,您指向特定的字符,即person[0]person 字符串的第一个字符。您必须拆分人员然后格式化字符串,以下将在不更改 inventory 变量的情况下执行此操作:

          for person in inventory:
              person_data = person.split(" ")
              item = person_data[0]
              quantity = person_data[1]
              amount = person_data[2]
              print("The store has {} {}, each for {} USD.".format(item, quantity, 
              amount))
          

          【讨论】:

            猜你喜欢
            • 2017-07-26
            • 1970-01-01
            • 1970-01-01
            • 2023-04-01
            • 2021-09-18
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多