英文:
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"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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论