|
本帖最后由 fwqman 于 2021-11-6 11:48 编辑
import random
def data_yield(count):
i = 0
while i < count:
if i == 0 or i == count - 1:
yield str(random.randrange(1,3)) + ' '
i += 1
else:
yield str(random.randrange(0,3)) + ' '
i += 1
class ListHelper:
@staticmethod
def select(lst, func):
i = 0
for obj in lst:
if func(obj):
i += 1
return i
@staticmethod
def count_zero(lst, func):
i = 0
for obj in lst:
if func(obj):
i += 1
continue
if i:
yield str(i + 1) + ' '
i = 0
def print_result(row, lst):
select_result = ListHelper.select(lst, lambda obj: obj != '0 ')
zero = ListHelper.count_zero(lst, lambda obj: int(obj.replace(' ', '')) == 0)
print('第{}行大于零的有:{}个 '.format(row+1,select_result), end='')
[print(_, end='') for _ in zero]
print()
def main(rows,count):
file = open('12345.txt', 'a')
for row in range(rows):
lst = [num for num in data_yield(count)]
file.writelines(lst)
file.write('\n')
print_result(row, lst)
file.close()
if __name__ == '__main__':
rows = 211876
count = 3000
main(rows,count)
|
|