PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, October 8, 2022

[FIXED] How can I find the number of elements from a list of degrees in Python3?

 October 08, 2022     data-science, database, histogram, python-3.x, statistics     No comments   

Issue

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)

Solution

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.



Answered By - fbence
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing