python - apply Operation on multiple variables

后端 未结 6 2031
梦谈多话
梦谈多话 2021-01-27 12:26

I know that this is a rather silly question and there are similar ones already answered, but they don\'t quite fit, so... How can I perform the same operation on multiple variab

相关标签:
6条回答
  • 2021-01-27 12:48

    You may want to look into Python's map function. The following link may be helpful:https://www.w3schools.com/python/ref_func_map.asp

    0 讨论(0)
  • 2021-01-27 12:50

    You can store them in a container, and then use map to apply one function on every element of the container
    For example with a list:

    def function_to_apply(element):
        return element*2
    
    # Define variables and store them in a container
    a,b,c = 1,2,3
    container = (a,b,c)
    
    # Apply the function on every element with map
    container = tuple(map(function_to_apply, container))
    a,b,c = container
    


    This can also be done with lambda functions to avoid defining a new function every time

    # Define variables and store them in a container
    a,b,c = 1,2,3
    container = (a,b,c)
    
    # Apply the function on every element with map
    container = tuple(map(lambda x: x*2, container))
    a,b,c = container
    

    If you have a really large set of variables and you want to retrieve them automatically without having to type each one of them like in a,b,c = container, you can use dict to store them with names or exec function to assign them dynamically.

    map documentation: https://docs.python.org/3/library/functions.html#map
    lambda documentation: https://docs.python.org/3/reference/expressions.html#grammar-token-lambda-expr

    0 讨论(0)
  • 2021-01-27 12:54

    You can use the following generator expression:

    a, b, c = 3, 4, 5
    a, b, c = (2 * i for i in (a, b, c))
    print(a, b, c)
    # 6 8 10
    
    0 讨论(0)
  • 2021-01-27 12:57

    Map is your friend here, but you also need to use 'implicit tuple unpacking':

    >>> a = 3
    >>> b = 4
    >>> c = 5
    >>> d, e, f = map(lambda x: x * 2, [a, b, c])
    >>> d
    6
    >>> e
    8
    >>> f
    10
    

    This way you can get the changed values back without modifying the original values

    0 讨论(0)
  • 2021-01-27 12:58

    You can use numpy or python lambda function combined with map to do the same.

    Using numpy:

    In [17]: import numpy as np
    
    In [18]: a = 3
        ...: b = 4
        ...: c = 5
        ...:
    
    In [19]: a,b,c = np.multiply([a, b, c], 2)
    
    In [20]: a
    Out[20]: 6
    
    In [21]: b
    Out[21]: 8
    
    In [22]: c
    Out[22]: 10
    

    Using lambda:

    In [23]: a, b, c = list(map(lambda x: x*2, [a, b, c]))
    
    In [24]: a
    Out[24]: 12
    
    In [25]: b
    Out[25]: 16
    
    In [26]: c
    Out[26]: 20
    
    
    0 讨论(0)
  • 2021-01-27 13:09

    You can use map to apply a function to every element of a list with a lambda function to perform your operation. Then use list unpacking to overwrite values in orginal variables.

    a = 1
    b = 2
    c = 3
    
    a,b,c = list(map(lambda x: x*2, [a,b,c]))
    
    print(a,b,c)
    
    # prints: (2, 4, 6)
    

    map returns a generator, that's why we need to explicit create the list to unpack.

    0 讨论(0)
提交回复
热议问题