方法一:
''' 统计在一个队列中的数字,有多少个正数,多少个负数,如[1, 3, 5, 7, 0, -1, -9, -4, -5, 8] ''' lists = [1, 3, 5, 7, 0, -1, -9, -4, -5, 8] positive = 0 negative = 0 for number in lists: if number > 0: positive += 1 elif number < 0: negative += 1 else: pass print('正数的个数是:' + str(positive)) print('负数的个数是:' + str(negative))
方法二:
''' 统计在一个队列中的数字,有多少个正数,多少个负数,如[1, 3, 5, 7, 0, -1, -9, -4, -5, 8] ''' lists = [1, 3, 5, 7, 0, -1, -9, -4, -5, 8] positive = [i for i in lists if i > 0] print('正数的个数是:' + str(len(positive))) negative = [i for i in lists if i < 0] print('负数的个数是:' + str(len(negative)))