【问题标题】:How to iterate a list of list and updating the elements in the list using only list comprehension如何仅使用列表理解来迭代列表列表并更新列表中的元素
【发布时间】:2021-03-11 09:43:27
【问题描述】:

我有一个书店订单清单:

orders = [ [1, ("5464", 4, 9.99), ("8274",18,12.99), ("9744", 9, 44.95)],
           [2, ("5464", 9, 9.99), ("9744", 9, 44.95)],
           [3, ("5464", 9, 9.99), ("88112", 11, 24.99)],
           [4, ("8732", 7, 11.99), ("7733", 11,18.99), ("88112", 5, 39.95)] ]

数字1,2,3,4是订单号,元组中的元素是“书号”、“数量”和“价格”。如果总价小于 100“总价 + 10”,我想将“数量”和“价格”合并为 1 个元素,即“总价”。

m1_list = []
for row in orders:
    lst = [row[0]]
    for column in row[1:len(row)]:
        order_num, quantity, price = column
        product = float(quantity) * float(price)
        if product < 100:
            total_price = product + 10
        else:
            total_price = product
        tpl = (order_num, total_price)
        lst.append(tpl)
    m1_list.append(lst)
print(m1_list)

这是输出:

[[1, ('5464', 49.96), ('8274', 233.82), ('9744', 404.55)], 
 [2, ('5464', 99.91), ('9744', 404.55)], 
 [3, ('5464', 99.91), ('88112', 274.89)], 
 [4, ('8732', 93.93), ('7733', 208.89), ('88112', 199.75)]]

由于不允许使用迭代/循环,我如何仅使用列表推导来完成与上述相同的操作?

【问题讨论】:

  • 您的问题得到解答了吗?

标签: python-3.x list iteration list-comprehension


【解决方案1】:

以下代码有效。试试看。

new_lst2 = [[y if type(y) != tuple else (y[0], y[1]*y[2]+10) if y[1]*y[2] < 100 else (y[0], y[1]*y[2]) for y in x] for x in orders]

【讨论】:

    【解决方案2】:
    import math as m
    
    orders =  [[line[0]] + [(tuples[0], (m.prod(tuples[1:]) if m.prod(tuples[1:]) > 100 else m.prod(tuples[1:]) + 10)) for tuples in line[1:]] for line in orders]
    
    
    print(orders)
    
    >>> orders =  [[1, ('5464', 49.96), ('8274', 233.82), ('9744', 404.55)], [2, ('5464', 99.91), ('9744', 404.55)], [3, ('5464', 99.91), ('88112', 274.89)], [4, ('8732', 93.93), ('7733', 208.89), ('88112', 199.75)]]]
    

    您可以使用pprint 打印得更好

    from pprint import pprint
    
    pprint(orders)
    
    >>> [[1, ('5464', 49.96), ('8274', 233.82), ('9744', 404.55)],
         [2, ('5464', 99.91), ('9744', 404.55)],
         [3, ('5464', 99.91), ('88112', 274.89)],
         [4, ('8732', 93.93), ('7733', 208.89), ('88112', 199.75)]]
    
    

    【讨论】:

      【解决方案3】:

      我能想到的最好的方法是:

      orders = [ [1, ("5464", 4, 9.99), ("8274",18,12.99), ("9744", 9, 44.95)],
                 [2, ("5464", 9, 9.99), ("9744", 9, 44.95)],
                 [3, ("5464", 9, 9.99), ("88112", 11, 24.99)],
                 [4, ("8732", 7, 11.99), ("7733", 11,18.99), ("88112", 5, 39.95)] ]
      
      def total(z):
          if type(z) == tuple:
              if z[1]*z[2] < 100:
                  return (z[0], z[1]*z[2]+10)
              return (z[0], z[1]*z[2])
          return z
      
      new_lst = [ list(map(total, x)) for x in orders]
      
      print(new_lst)
      

      【讨论】:

      • 操作说只使用列表理解
      • 对不起,我也是 python 新手,只是尽力提供帮助。
      猜你喜欢
      • 2018-08-13
      • 2020-12-03
      • 2021-02-14
      • 2011-08-17
      • 1970-01-01
      • 2019-09-11
      • 1970-01-01
      • 2020-05-14
      • 1970-01-01
      相关资源
      最近更新 更多