【问题标题】:Lambda function for multiplying each element of list with random number between 1 and 10?用于将列表的每个元素与 1 到 10 之间的随机数相乘的 Lambda 函数?
【发布时间】:2021-02-15 04:54:57
【问题描述】:

我被困在这个问题上,因为我对 python 中的函数式编程相当陌生。任何帮助将不胜感激!

注意:我知道使用 map 更简单,因为我们可以使用可迭代对象,但我需要一些方法来仅使用 lambda 函数来完成此操作。

Given the following list, ages of 25 people:
ages = [79, 91, 25, 22, 95, 69, 47, 87, 87, 2, 13, 94, 50, 73, 29, 87, 81, 51, 32, 69, 10, 91, 45, 7,
51]
Q: Make a new list in which you multiply each age with a random number between 1 to 10
(Generate a random number for each age and multiply them together). Only use Lambda
Function for the multiplication. (You can use random generator provided by in python to
generate the random numbers)

这是我目前所拥有的:

import random

inconsistent = []
ages = [79, 91, 25, 22, 95, 69, 47, 87, 87, 2, 13, 94, 50, 73, 29, 87, 81, 51, 32, 69, 10, 91, 45, 7,
51]

for i in range(25):
    inconsistent.append(random.randint(1,10))
print(inconsistent)
    
x = lambda a,b: a*b
x(ages, inconsistent)

以下是我得到的错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-24-646727e92091> in <module>
     10 
     11 x = lambda a,b: a*b
---> 12 x(ages, inconsistent)

<ipython-input-24-646727e92091> in <lambda>(a, b)
      9 print(inconsistent)
     10 
---> 11 x = lambda a,b: a*b
     12 x(ages, inconsistent)

TypeError: can't multiply sequence by non-int of type 'list'

【问题讨论】:

    标签: python python-3.x lambda functional-programming


    【解决方案1】:

    使用map。 Map 可以将lambda 作为第一个参数,将iterable 作为第二个参数。

    ages = [79, 91, 25, 22, 95, 69, 47, 87, 87, 2, 13, 94, 50, 73, 29, 87, 81, 51, 32, 69, 10, 91, 45, 7,
    51]
    
    print(list(map(lambda x: x * random.randint(1,10), ages)))
    
    [553, 91, 175, 198, 285, 345, 470, 174, 87, 10, 91, 752, 50, 730, 261, 261, 567, 204, 160, 552, 90, 364, 135, 70, 51]
    

    你的情况

    import random
    
    inconsistent = []
    ages = [79, 91, 25, 22, 95, 69, 47, 87, 87, 2, 13, 94, 50, 73, 29, 87, 81, 51, 32, 69, 10, 91, 45, 7,
    51]
    
    for i in range(25):
        inconsistent.append(random.randint(1,10))
    print(inconsistent)
        
    x = lambda a,b: a*b
    
    output = []
    
    for i in range(25):
        output.append(x(ages[i], inconsistent[i]))
        
    print(output)
    
    [7, 7, 5, 5, 3, 5, 5, 10, 10, 9, 9, 7, 3, 8, 7, 5, 1, 10, 5, 2, 5, 5, 2, 9, 3]
    [553, 637, 125, 110, 285, 345, 235, 870, 870, 18, 117, 658, 150, 584, 203, 435, 81, 510, 160, 138, 50, 455, 90, 63, 153]
    

    【讨论】:

    • 问题是我不能使用地图。必须想办法用 lambda 完成这个!
    • 这正是我需要的,谢谢!
    【解决方案2】:

    您可以使用列表推导:

    import random
    
    ages = [79, 91, 25, 22, 95, 69, 47, 87, 87, 2, 13, 94, 50, 73, 29, 87, 81, 51, 32, 69, 10, 91, 45, 7, 51]
    
    p = lambda x,y: x*y
    
    [p(a, random.randint(1,10)) for a in ages]
    
    

    【讨论】:

      猜你喜欢
      • 2020-08-14
      • 1970-01-01
      • 2021-12-22
      • 2023-03-19
      • 1970-01-01
      • 2016-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多