Python Forum
Efficiency with regard to nested conditionals or and statements
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Efficiency with regard to nested conditionals or and statements
#11
(Apr-28-2022, 09:11 PM)deanhystad Wrote: Maybe you should use pandas or numpy. This does not look like the optimal way to do anything.

I do end up using pandas to manage the results, although I export those results row by row to a .csv file. I'll want to develop further to track average winning trade, average loss, and lots of other things. Pandas should be very useful for all that.
Reply
#12
Quote:The program generates a results file with different trade statistics and parameters. That is where I got a lot of unnecessary decimal output that I figured I could clean up by converting to int so there would be no decimals.

But you are not doing that here. Calling int(float(datalist[2])) does not replace the contents of datalist[2] with an integer, it creates an integer object using the float value of datalist[2]. The integer object is used in the comparison and deleted. datalist[2] is unchanged.
Reply
#13
(Apr-29-2022, 08:10 PM)deanhystad Wrote:
Quote:The program generates a results file with different trade statistics and parameters. That is where I got a lot of unnecessary decimal output that I figured I could clean up by converting to int so there would be no decimals.

But you are not doing that here. Calling int(float(datalist[2])) does not replace the contents of datalist[2] with an integer, it creates an integer object using the float value of datalist[2]. The integer object is used in the comparison and deleted. datalist[2] is unchanged.

I'm getting the chance today to go back and take a closer look at this.

I imported a line from the data file with this:
import glob, os

num_lines = 0

for file in glob.glob("*.csv"):
    barfile = open(file, "r")  
    for line in barfile:
        if num_lines < 1:
            stats = line.split(",") 
            num_lines +=1
        else: break  #not just break (need the else)
    break
print(f'Successfully exited iteration loop with num_lines = {num_lines}')
Now I have a list of elements as stats:

['2527373', '17169.0', '1081.0', '2257.83', 'SPX 191220P02125000', '18250.0', '2125.0', '239.8', '0.201915', '-0.355646', '13.874128', '0.000455', '-0.129162\n']

They're all <class 'str'> . Some are numbers. Some numbers I can leave as integers (e.g. stats[2], stats[5], stats[6]). For others, I need the decimal part (e.g. stats[7:]).

If I try this:
for i in range(5,len(stats)):
    conv_element = int(stats[i]) 
    print(f'Element #{i} is {conv_element} and data type {type(conv_element)}.')  
I get: ValueError: invalid literal for int() with base 10: '18250.0'

If I use:
conv_element = int(float(stats[i]))
Then it successfully converts to integer. I think that's why I felt the need to use int(float()). The numbers are used for computation and then sometimes printed to a results file. It's then onto the next row of the data file. I'm not looking to make any changes to the original data file.
Reply
#14
The biggest (only?) problem seems to be trying to convert numbers ending in .0 to integer. I'm not sure what is causing the .0 because those aren't showing up in the data file. I need to figure out how the numbers are being changed.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Doubt about conditionals in Python. Carmazum 6 1,614 Apr-01-2023, 12:01 AM
Last Post: Carmazum
  Numpy Structure and Efficiency garynewport 2 694 Oct-19-2022, 10:11 PM
Last Post: paul18fr
  conditionals based on data frame mbrown009 1 905 Aug-12-2022, 08:18 AM
Last Post: Larz60+
  Nested conditionals vs conditionals connected by operators dboxall123 8 3,076 Feb-18-2022, 09:34 PM
Last Post: dboxall123
  Nested Conditionals shen123 3 2,642 Jul-28-2021, 08:24 AM
Last Post: Yoriz
  How to use vectorization instead of for loop to improve efficiency in python? PJLEMZ 4 2,417 Feb-06-2021, 09:45 AM
Last Post: paul18fr
  Invalid syntax using conditionals if - else jperezqu 1 2,345 Jan-13-2021, 07:32 PM
Last Post: bowlofred
  conditionals with boolean logic?? ridgerunnersjw 3 2,008 Sep-26-2020, 02:13 PM
Last Post: deanhystad
  two conditionals with intermediate code Skaperen 5 2,792 Jul-12-2020, 07:18 PM
Last Post: Skaperen
  Conditionals, while loops, continue, break (PyBite 102) Drone4four 2 2,993 Jun-04-2020, 12:08 PM
Last Post: Drone4four

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020