Home > Mobile >  How can I find the number of elements from a list of degrees in Python3?
How can I find the number of elements from a list of degrees in Python3?

Time:10-19

Basically, I want to extract the average number of elements from a system which have a folding degree less than 45. To do this, I extract my data from the configuration files deposited in my folders. Then, I calculate the angles (written as "angolo" in the code) but I actually do not know how to extract the number of elements from that angle. Particularly, I do not know how to isolate the number of elements which have an angle less than 45 degree. Does anybody know a python3 function or maybe a script to find these numbers?

import sys,os
import numpy as np
fn='listadir.txt' # <=== lista_dir
with open(fn,'r') as f:
  lines=f.readlines()
for l in lines:
os.chdir('C:\\Users\\simone\\anaconda3\\Ex_Files_Python_Statistics_EssT\\'   l.strip('\n'))
os.system('dir /b cnf-* > listacnf.txt')
with open('listacnf.txt','r') as f:
    linescnf=f.readlines()
    Q=np.matrix([[0.0,0.0,0.0],[0.0,0.0,0.0],[0.0,0.0,0.0]])
    cc = 1
for ll in linescnf:
    #print('ll=', ll)
    with open(ll.strip('\n')) as ff:
        cnft=ff.readlines()
    cnf=cnft[1:]
    for lc in cnf:
        
        lv = lc.strip('\n').split()
        #print('lc =', lc)
        
        ux = float(lv[3])
        uy = float(lv[4])
        uz = float(lv[5])
        Qxx = ux*ux
        Qxy = ux*uy
        Qxz = ux*uz
        Qyx = uy*ux
        Qyy = uy*uy
        Qyz = uy*uz
        Qzx = uz*ux
        Qzy = uz*uy
        Qzz = uz*uz
        Q  = np.matrix([[Qxx, Qxy, Qxz],[Qyx, Qyy, Qyz],[Qzx,Qzy, Qzz]])
        cc  = 1
        angolo = np.arccos(ux*uy*uz)
        #print(ux)
    #....
Q = Q/cc

# ....
os.chdir('..')
#print(l.strip('\n'))
N= 1000
mediangolo = angolo.mean()
frazione = mediangolo/N
print('frazione=', mediangolo)

CodePudding user response:

I think this is what you want:

import numpy as np
l = np.array([10,20,30,40,50,60])
l[ l < 45] # returns: array([10, 20, 30, 40])

l < 45 will return a list that is as long as l and True where the element is less than 45 and False otherwise which you can then use to select the values from the original array.

Or is you just need the count np.count_nonzero( l < 45) will do the trick.

  • Related