统计满足特定条件的文件总数在Python中

huangapple go评论104阅读模式
英文:

Counting the total number of files which meet a certain criterion in Python

问题

  1. 我正在编写一个代码它会扫描所有的文件夹并计算满足条件的所有文件的平均值```min(numbers) <= 0.5 * max(numbers)```然而我还想对满足条件的所有文件夹进行总计数我展示了当前和预期的输出
  2. 当前输出是
  3. ```python
  4. File numbers which meet the criterion = 1
  5. File numbers which meet the criterion = 2
  6. File numbers which meet the criterion = 7
  7. File numbers which meet the criterion = 8

预期输出是

  1. File numbers which meet the criterion = 1
  2. File numbers which meet the criterion = 2
  3. File numbers which meet the criterion = 7
  4. File numbers which meet the criterion = 8
  5. Total number of files which meet the criterion = 4
英文:

I am writing a code which scans all the folders and calculates averages of all the files which meet the criterion: min(numbers) <= 0.5 * max(numbers). However, I also want to do a total count of all folders which satisfy the criterion. I present the current and expected output.

  1. import os
  2. NFILES = 10 #Scans -1 named files
  3. values = [[] for _ in range(NFILES)]
  4. Folder_name='220 nodes_seed896_var5_100stepsize_0.45initial'
  5. for i in range(NFILES):
  6. filepath = rf"C:\Users\User\OneDrive - Technion\Research_Technion\Python_PNM\Surfactant A-D\{Folder_name}\{i}\len_J.txt"
  7. if os.path.isfile(filepath):
  8. with open(filepath) as data:
  9. lines = data.readlines()
  10. numbers = [int(line.strip()) for line in lines if line.strip().isdigit()]
  11. if (min(numbers) <= 0.5 * max(numbers)):
  12. values[i].extend(numbers)
  13. print("File numbers which meet the criterion =",i)
  14. valid_values = [v for v in values if v] # Remove empty lists
  15. for t in zip(*valid_values):
  16. with open(rf"C:\Users\User\OneDrive - Technion\Research_Technion\Python_PNM\Surfactant A-D\{Folder_name}\Averaged.txt", 'a') as f:
  17. #writer = csv.writer(f)
  18. #print("J =",len(J[0]))
  19. f.writelines('\n')
  20. # if i < len(J[0]) - 1 and len(J[0][t + 1]) >= len(J[0][t]):
  21. # f.writelines('\n')
  22. # f.write(str(J[0]))
  23. f.write(str(sum(t) // len(t)))

The current output is

  1. File numbers which meet the criterion = 1
  2. File numbers which meet the criterion = 2
  3. File numbers which meet the criterion = 7
  4. File numbers which meet the criterion = 8

The expected output is

  1. File numbers which meet the criterion = 1
  2. File numbers which meet the criterion = 2
  3. File numbers which meet the criterion = 7
  4. File numbers which meet the criterion = 8
  5. Total number of files which meet the criterion = 4

答案1

得分: 1

  1. count=0
  2. for i in range(NFILES):
  3. filepath = rf"C:\Users\User\OneDrive - Technion\Research_Technion\Python_PNM\Surfactant A-D\{Folder_name}\{i}\len_J.txt"
  4. if os.path.isfile(filepath):
  5. with open(filepath) as data:
  6. lines = data.readlines()
  7. numbers = [int(line.strip()) for line in lines if line.strip().isdigit()]
  8. if (min(numbers) <= 0.5 * max(numbers)):
  9. values[i].extend(numbers)
  10. print("File numbers which meet the criterion =",i)
  11. count+=1
  12. print('Total number of files which meet the criterion =',count)
  13. Here, I've used the variable 'count' to keep track of the number of files meeting the criterion.
英文:
  1. count=0
  2. for i in range(NFILES):
  3. filepath = rf&quot;C:\Users\User\OneDrive - Technion\Research_Technion\Python_PNM\Surfactant A-D\{Folder_name}\{i}\len_J.txt&quot;
  4. if os.path.isfile(filepath):
  5. with open(filepath) as data:
  6. lines = data.readlines()
  7. numbers = [int(line.strip()) for line in lines if line.strip().isdigit()]
  8. if (min(numbers) &lt;= 0.5 * max(numbers)):
  9. values[i].extend(numbers)
  10. print(&quot;File numbers which meet the criterion =&quot;,i)
  11. count+=1
  12. print(&#39;Total number of files which meet the criterion =&#39;,count)

Here, I've used the variable 'count' to keep track of the number of files meeting the criterion.

huangapple
  • 本文由 发表于 2023年6月18日 18:55:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76500176.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定