print(line_num if line_num % 10000 == 0)
时间: 2024-01-21 16:20:17 浏览: 44
这段代码有语法错误,应该改为:
```
if line_num % 10000 == 0:
print(line_num)
```
这段代码的作用是在处理大量数据时,每处理 10000 行数据时打印一次当前的行数。
相关问题
list_num = [] list_num_count = 0 dict_num ={} #从文件读入,文件第一行为集合中元素的个数,以后每一行为一个元素 list_num_count = int(open('input.txt','r').readline()) for line_num, line in enumerate(open("input.txt",'r')): if line_num > 0: list_num += line.split() #将读到的元素加入的字典中 for item in list_num: if dict_num.has_key(item): dict_num[item] += 1 else: dict_num.setdefault(item,1) pass #找到出现次数最多的那个数,找到重数 dict_sort_by_top = {} top_value = 0 for valus in dict_num.itervalues(): if valus> top_value: top_value = valus pass #根据重数找到众数...这是因为考虑到可能有多个元素有相同多的重数 the_pop_num = 0 the_pop_num_count = 0 for keys,values in dict_num.iteritems(): if values == top_value: print 'the pop num is %s,and the appear num is %s' % (keys,values) the_pop_num = keys the_pop_num_count = values #输出到文件,第一行为从数,第二行为重数 write_line = '%s\n%s' %(the_pop_num, the_pop_num_count) open("output.txt",'w').write(write_line)
这段代码的作用是读取一个文件中的数字集合,然后统计每个数字出现的次数,最后找到出现次数最多的数字,输出到另一个文件中。
具体解释如下:
1. 定义了一个空列表 list_num 用于存储文件中读取到的数字集合,以及一个字典 dict_num 用于存储每个数字出现的次数。
2. 从文件中读取第一行,该行为数字集合的元素个数,将其赋值给 list_num_count。
3. 通过 enumerate() 函数遍历文件中的每一行,当行号大于 0 时,将该行使用 split() 函数分隔开,将得到的数字添加到 list_num 列表中。
4. 遍历 list_num 列表,将其中的数字统计到 dict_num 字典中,如果字典中已有该数字,则将其出现次数加 1,否则将该数字添加到字典中,并将其出现次数设为 1。
5. 遍历 dict_num 字典,找到出现次数最多的数字(众数),将其赋值给 the_pop_num 变量,并将其出现次数赋值给 the_pop_num_count 变量。由于可能有多个数字出现次数相同,因此这里还需要考虑到可能有多个众数的情况。
6. 将找到的众数和其出现次数写入到另一个文件 output.txt 中,第一行为众数,第二行为其出现次数。
请将这两个程序写在一起if __name__ == '__main__': app = QApplication(sys.argv) window = MyWindow() sys.stdout = window # 将标准输出重定向到 print 输出信息框中 print("print 成功") window.show() sys.exit(app.exec_()) if __name__ == '__main__': while (1): cols = ['0', '1', '2', '3', '4', '5', '6', '7'] line_names = ['VitalSigns1', 'VitalSigns2', 'VitalSigns3', 'VitalSign4', 'VitalSign5', 'VitalSign6', 'VitalSign7', 'VitalSigns8'] nowfile = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime()) if excel_name != None and over != 0: df1 = pd.read_excel(excel_name, sheet_name='Sheet1') plt.plot(df1[cols[0]], label=line_names[0]) plt.plot(df1[cols[1]], label=line_names[1]) plt.plot(df1[cols[2]], label=line_names[2]) plt.plot(df1[cols[3]], label=line_names[3]) plt.plot(df1[cols[4]], label=line_names[4]) plt.plot(df1[cols[5]], label=line_names[5]) plt.plot(df1[cols[6]], label=line_names[6]) plt.plot(df1[cols[7]], label=line_names[7]) plt.legend() plt.xlabel('NUM DATA') plt.ylabel('LIFE DATA') plt.title('Title of the plot') plt.savefig(nowfile, dpi=300) plt.show() over = 0 main()
请注意代码缩进和格式,将两个程序整合成一个:
```
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MyWindow()
sys.stdout = window # 将标准输出重定向到 print 输出信息框中
print("print 成功")
window.show()
sys.exit(app.exec_())
while (1):
cols = ['0', '1', '2', '3', '4', '5', '6', '7']
line_names = ['VitalSigns1', 'VitalSigns2', 'VitalSigns3', 'VitalSign4', 'VitalSign5', 'VitalSign6',
'VitalSign7', 'VitalSigns8']
nowfile = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime())
if excel_name != None and over != 0:
df1 = pd.read_excel(excel_name, sheet_name='Sheet1')
plt.plot(df1[cols[0]], label=line_names[0])
plt.plot(df1[cols[1]], label=line_names[1])
plt.plot(df1[cols[2]], label=line_names[2])
plt.plot(df1[cols[3]], label=line_names[3])
plt.plot(df1[cols[4]], label=line_names[4])
plt.plot(df1[cols[5]], label=line_names[5])
plt.plot(df1[cols[6]], label=line_names[6])
plt.plot(df1[cols[7]], label=line_names[7])
plt.legend()
plt.xlabel('NUM DATA')
plt.ylabel('LIFE DATA')
plt.title('Title of the plot')
plt.savefig(nowfile, dpi=300)
plt.show()
over = 0
main()
```