时间:2018-04-07 20:39:14

标签: python python-3.5

例如,我有两个数字列表:

List_numbers_1 = [3, 54, -30]
List_numbers_2 = [65, 8, 800]

我想创建一个运行以下sums表的函数:

  3 +  65 =  68
 54 +   8 =  62
-30 + 800 = 770

桌子排成一列,这是我的目标。为了创建该功能,我创建了其他3个可能对我有用的功能:

'''it returns the width of a number '''
def max_width(List_numbers_1):
    string_List_numbers_1 = map(str, List_numbers_1)
    width_List_numbers_1 = map(len, string_List_numbers_1)
    return max(width_List_numbers_1)

Output: 3
'''it returns the padd for a number'''

def left_padded(number, width):
    return str(number).rjust(width)

left_padded(54, 5)
'   54'
left_padded(-56, 5)
'  -56'
'''It returns a padd for all the numbers of the list'''
def all_left_padded(List_numbers_1, width):
    return list(map(lambda number: left_padded(number, width), List_numbers_1))

all_left_padded(List_numbers_1, 5)
['    3', '   54', '  -30']

我认为上面的函数对我的上一个函数很有用。不过,我真的很感激任何其他想法。如果有可能我更喜欢使用return语句的函数,但print()就可以了。

事实上,我认为这个功能必须包含返回和打印。

谢谢你

4 个答案:

答案 0 :(得分:1)

如果没有numpy,您可以将列表压缩在一起并添加它们:

[sum(i) for i in zip(lst1,lst2)]

使用列表推导而不是map

更容易

为了格式化数字,很自然地使用str.format()

由于您事先并不知道数字的宽度,所以首先要创建 格式字符串,最简单的方法是使用format

# maxlen returns the length of the longest element
def maxlen(l):
  return max([len(str(i)) for i in l])

# sumtable returns a formatted multiline string containing the sums
# written in a human readable form.
def sumtable(l1,l2):
  #sums contains the answers, and table is the full table numbers in
  #the calculations as a list of tuples
  sums = [sum(i) for i in zip(l1,l2)]
  table = list(zip(l1,l2,sums))

  width1 = maxlen(l1)
  width2 = maxlen(l2)
  widthsum = maxlen(sums)

  # formatstring has a form like "{:3d} + {:5d} = {:5d}\n" 
  formatstring = "{{:{}d}} + {{:{}d}} = {{:{}d}}\n".format(width1,width2,widthsum)

  # unpack the values from the table into the format string and return.
  return (formatstring.format(*table[0])
         + formatstring.format(*table[1])
         + formatstring.format(*table[2]))

print(sumtable([3,54,-30],[65,8,800]))

#   3 +  65 =  68
#  54 +   8 =  62
# -30 + 800 = 770

答案 1 :(得分:0)

如果你可以使用第三方库,那么numpy

是微不足道的
import numpy as np

lst1 = [3, 54, -30]
lst2 = [65, 8, 800]

res = np.sum([lst1, lst2], axis=0)

# array([ 68,  62, 770])

答案 2 :(得分:0)

`in order to display the table of sums''' 
>def table(L1, L2):
    Sum = list(map(lambda x, y: x + y, L1, L2))
    widthL1 = max_width(L1)
    widthL2 = max_width(L2)
    widthSum = max_width(Sum)
    first_column = all_left_padded(L1, widthL1)
    second_column = all_left_padded(L2, widthL2)
    third_column = all_left_padded(Sum, widthsum)
    triplets = zip(first_column, second_column, third_column)
    return list(map(lambda triplet: str_sum(triplet), triplets))

  # for displaying the table
  >def printable(L1, L2):
           t = table(L1, L2)
           for row in t:
                 print(row)

#str_sum函数是:

  

def str_sum(元组):                  返回'{} + {} = {}'。format(元组)

  > str_sum((2,3,5))
      '2 + 3 = 5'

答案 3 :(得分:0)

  # I was thinking: why instead summing two Lists, I will sum n lists?
  # I would create two functions
   >L1 = [1, 2]
   >L2 = [2, 3]
   > L3 = [4, 5]
   > Group = (L1, L2, L3)
   > Sum = [sum(l) for l in zip(*Group)]
   >Sum
    [7, 10]

  # and for creating a Tuple with n elements because if the table has n Lists the Tuple must have n elements
   > def str_sum(Tuple):
             n = len (Tuple)
             f = ((n -2)*'{ } + ' + '{ } = { }')
             return f.format(*Tuple)

      >str_sum((1, 2, 3, 8, 7, 21))
      ('1 + 2 + 3 + 8 + 7 = 21')