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

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

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

问题

我正在编写一个代码它会扫描所有的文件夹并计算满足条件的所有文件的平均值:```min(numbers) <= 0.5 * max(numbers)```。然而我还想对满足条件的所有文件夹进行总计数我展示了当前和预期的输出

当前输出是

```python
File numbers which meet the criterion = 1
File numbers which meet the criterion = 2
File numbers which meet the criterion = 7
File numbers which meet the criterion = 8

预期输出是

File numbers which meet the criterion = 1
File numbers which meet the criterion = 2
File numbers which meet the criterion = 7
File numbers which meet the criterion = 8

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.

import os

NFILES = 10 #Scans -1 named files

values = [[] for _ in range(NFILES)]
Folder_name='220 nodes_seed896_var5_100stepsize_0.45initial'


for i in range(NFILES):
    filepath = rf"C:\Users\User\OneDrive - Technion\Research_Technion\Python_PNM\Surfactant A-D\{Folder_name}\{i}\len_J.txt"
    if os.path.isfile(filepath):
        with open(filepath) as data:
            lines = data.readlines()
            numbers = [int(line.strip()) for line in lines if line.strip().isdigit()]
            if (min(numbers) <= 0.5 * max(numbers)):
                values[i].extend(numbers)
                print("File numbers which meet the criterion =",i)


valid_values = [v for v in values if v]  # Remove empty lists



for t in zip(*valid_values):
    with open(rf"C:\Users\User\OneDrive - Technion\Research_Technion\Python_PNM\Surfactant A-D\{Folder_name}\Averaged.txt", 'a') as f:
        #writer = csv.writer(f)   
        #print("J =",len(J[0])) 
        f.writelines('\n')
        # if i < len(J[0]) - 1 and len(J[0][t + 1]) >= len(J[0][t]):
        #     f.writelines('\n')
        #     f.write(str(J[0]))
        
        f.write(str(sum(t) // len(t)))

The current output is

File numbers which meet the criterion = 1
File numbers which meet the criterion = 2
File numbers which meet the criterion = 7
File numbers which meet the criterion = 8

The expected output is

File numbers which meet the criterion = 1
File numbers which meet the criterion = 2
File numbers which meet the criterion = 7
File numbers which meet the criterion = 8

Total number of files which meet the criterion = 4

答案1

得分: 1

    count=0
    for i in range(NFILES):
    filepath = rf"C:\Users\User\OneDrive - Technion\Research_Technion\Python_PNM\Surfactant A-D\{Folder_name}\{i}\len_J.txt"
    if os.path.isfile(filepath):
        with open(filepath) as data:
            lines = data.readlines()
            numbers = [int(line.strip()) for line in lines if line.strip().isdigit()]
            if (min(numbers) <= 0.5 * max(numbers)):
                values[i].extend(numbers)
                print("File numbers which meet the criterion =",i)
                count+=1
    print('Total number of files which meet the criterion =',count)

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

确定