1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
import random nums = [] for _ in range(10): nums.append(random.randrange(21)) print("Origin numbers = {}".format(nums)) print()
length = len(nums) samenums = [] diffnums = [] states = [0] * length
for i in range(length): flag = False if states[i] == 1: continue for j in range(i+1,length): if states[j] == 1: continue if nums[i] == nums[j]: flag = True states[j] = 1 if flag: samenums.append(nums[i]) states[i] = 1 else: diffnums.append(nums[i]) print("Same numbers = {1},Counter = {0}".format(len(samenums),samenums)) print("Different numbers = {1},Counter = {0}".format(len(diffnums),diffnums)) print(list(zip(states,nums))) 输出: Origin numbers = [7, 15, 12, 8, 10, 0, 5, 19, 15, 2]
Same numbers = [15],Counter = 1 Different numbers = [12, 8, 10, 0, 5, 19, 15, 2],Counter = 8 [(0, 7), (1, 15), (0, 12), (0, 8), (0, 10), (0, 0), (0, 5), (0, 19), (1, 15), (0, 2)]
|