Python „stack overflow” (104 if statements). Is def(x) the only solution to optimise code?

2024/7/4 16:05:26

Today have tried to check files with path directory name. Previously it worked, until I tried to create 104 if/else statements.

How to dispose of this overflow error? More specific question: Does def(x) for several checks the only one solution to optimise such cases in Python?enter image description here

import turicreate as turiurl = "images/"
data = turi.image_analysis.load_images(url)data["moneyType"] = data["path"].apply(lambda path: "ARS_1" if "ARS_1" in pathelse("ARS_2" if "ARS_2" in pathelse("ARS_5" if "ARS_5" in pathelse("ARS_10" if "ARS_10" in pathelse("ARS_20" if "ARS_20" in pathelse("ARS_50" if "ARS_50" in pathelse("ARS_100" if "ARS_100" in pathelse("ARS_200" if "ARS_200" in pathelse("ARS_500" if "ARS_500" in pathelse("ARS_1000" if "ARS_1000" in pathelse("AED_5" if "AED_5" in path
else("AED_10" if "AED_10" in path
else("AED_20" if "AED_20" in path
else("AED_50" if "AED_50" in path
else("AED_100" if "AED_100" in path
else("AED_200" if "AED_200" in path
else("AED_500" if "AED_500" in path
else("AED_1000" if "AED_1000" in pathelse("BRL_1" if "BRL_1" in path
else("BRL_2" if "BRL_2" in path
else("BRL_5" if "BRL_5" in path
else("BRL_10" if "BRL_10" in path
else("BRL_20" if "BRL_20" in path
else("BRL_50" if "BRL_50" in path
else("BRL_100" if "BRL_100" in pathelse("CLP_1000" if "CLP_1000" in path
else("CLP_2000" if "CLP_2000" in path
else("CLP_5000" if "CLP_5000" in path
else("CLP_10000" if "CLP_10000" in path
else("CLP_20000" if "CLP_20000" in pathelse("COP_1000" if "COP_1000" in path
else("COP_2000" if "COP_2000" in path
else("COP_5000" if "COP_5000" in path
else("COP_10000" if "COP_10000" in path
else("COP_20000" if "COP_20000" in path
else("COP_50000" if "COP_50000" in path
else("COP_100000" if "COP_100000" in pathelse("CUC_1" if "CUC_1" in path
else("CUC_3" if "CUC_3" in path
else("CUC_5" if "CUC_5" in path
else("CUC_10" if "CUC_10" in path
else("CUC_20" if "CUC_20" in path
else("CUC_50" if "CUC_50" in path
else("CUC_100" if "CUC_100" in path
else("CUC_200" if "CUC_200" in path
else("CUC_500" if "CUC_500" in path
else("CUC_1000" if "CUC_1000" in pathelse("EGP_1" if "EGP_1" in path
else("EGP_5" if "EGP_5" in path
else("EGP_10" if "EGP_10" in path
else("EGP_20" if "EGP_20" in path
else("EGP_25pt" if "EGP_25pt" in path
else("EGP_50" if "EGP_50" in path
else("EGP_50pt" if "EGP_50pt" in path
else("EGP_100" if "EGP_100" in path
else("EGP_200" if "EGP_200" in pathelse("INR_1" if "INR_1" in path
else("INR_2" if "INR_2" in path
else("INR_5" if "INR_5" in path
else("INR_10" if "INR_10" in path
else("INR_20" if "INR_20" in path
else("INR_50" if "INR_50" in path
else("INR_100" if "INR_100" in path
else("INR_200" if "INR_200" in path
else("INR_500" if "INR_500" in path
else("INR_2000" if "INR_2000" in pathelse("JOD_1" if "JOD_1" in path
else("JOD_5" if "JOD_5" in path
else("JOD_10" if "JOD_10" in path
else("JOD_20" if "JOD_20" in path
else("JOD_50" if "JOD_50" in pathelse("KHR_50" if "KHR_50" in path
else("KHR_100" if "KHR_100" in path
else("KHR_500" if "KHR_500" in path
else("KHR_1000" if "KHR_1000" in path
else("KHR_2000" if "KHR_2000" in path
else("KHR_5000" if "KHR_5000" in path
else("KHR_10000" if "KHR_10000" in path
else("KHR_20000" if "KHR_20000" in path
else("KHR_50000" if "KHR_50000" in path
else("KHR_100000" if "KHR_100000" in pathelse("LAK_500" if "LAK_500" in path
else("LAK_1000" if "LAK_1000" in path
else("LAK_2000" if "LAK_2000" in path
else("LAK_5000" if "LAK_5000" in path
else("LAK_10000" if "LAK_10000" in path
else("LAK_20000" if "LAK_20000" in path
else("LAK_50000" if "LAK_50000" in path
else("LAK_100000" if "LAK_100000" in pathelse("LKR_10" if "LKR_10" in path
else("LKR_20" if "LKR_20" in path
else("LKR_50" if "LKR_50" in path
else("LKR_100" if "LKR_100" in path
else("LKR_200" if "LKR_200" in path
else("LKR_500" if "LKR_500" in path
else("LKR_1000" if "LKR_1000" in path
else("LKR_2000" if "LKR_2000" in path
else("LKR_5000" if "LKR_5000" in pathelse("MAD_20" if "MAD_20" in path
else("MAD_50" if "MAD_50" in path
else("MAD_100" if "MAD_100" in path
else("MAD_200" if "MAD_200" in pathelse("MMK_1" if "MMK_1" in path
else("MMK_5" if "MMK_5" in path
else("MMK_10" if "MMK_10" in path
else("MMK_20" if "MMK_20" in path
else("MMK_50" if "MMK_50" in path
else("MMK_100" if "MMK_100" in path
else("MMK_200" if "MMK_200" in path
else("MMK_500" if "MMK_500" in path
else("MMK_1000" if "MMK_1000" in path
else("MMK_5000" if "MMK_5000" in path
else("MMK_10000" if "MMK_10000" in pathelse("MNT_1" if "MNT_1" in path
else("MNT_5" if "MNT_5" in path
else("MNT_10" if "MNT_10" in path
else("MNT_20" if "MNT_20" in path
else("MNT_50" if "MNT_50" in path
else("MNT_100" if "MNT_100" in path
else("MNT_500" if "MNT_500" in path
else("MNT_1000" if "MNT_1000" in path
else("MNT_5000" if "MNT_5000" in path
else("MNT_10000" if "MNT_10000" in path
else("MNT_20000" if "MNT_20000" in pathelse("MXN_20" if "MXN_20" in path
else("MXN_50" if "MXN_50" in path
else("MXN_100" if "MXN_100" in path
else("MXN_200" if "MXN_200" in path
else("MXN_500" if "MXN_500" in path
else("MXN_1000" if "MXN_1000" in pathelse("USD" if "USD" in pathelse("NPR_1" if "NPR_1" in path
else("NPR_2" if "NPR_2" in path
else("NPR_5" if "NPR_5" in path
else("NPR_10" if "NPR_10" in path
else("NPR_20" if "NPR_20" in path
else("NPR_25" if "NPR_25" in path
else("NPR_50" if "NPR_50" in path
else("NPR_100" if "NPR_100" in path
else("NPR_200" if "NPR_200" in path
else("NPR_250" if "NPR_250" in path
else("NPR_500" if "NPR_500" in path
else("NPR_1000" if "NPR_1000" in pathelse("QAR_1" if "QAR_1" in path
else("QAR_5" if "QAR_5" in path
else("QAR_10" if "QAR_10" in path
else("QAR_50" if "QAR_50" in path
else("QAR_100" if "QAR_100" in path
else("QAR_500" if "QAR_500" in pathelse("RUB_5" if "RUB_5" in path
else("RUB_10" if "RUB_10" in path
else("RUB_50" if "RUB_50" in path
else("RUB_100" if "RUB_100" in path
else("RUB_200" if "RUB_200" in path
else("RUB_500" if "RUB_500" in path
else("RUB_1000" if "RUB_1000" in path
else("RUB_2000" if "RUB_2000" in path
else("RUB_5000" if "RUB_5000" in pathelse("THB_20" if "THB_20" in path
else("THB_50" if "THB_50" in path
else("THB_100" if "THB_100" in path
else("THB_500" if "THB_500" in path
else("THB_1000" if "THB_1000" in pathelse("VND_100" if "VND_100" in path
else("VND_200" if "VND_200" in path
else("VND_500" if "VND_500" in path
else("VND_1000" if "VND_1000" in path
else("VND_2000" if "VND_2000" in path
else("VND_5000" if "VND_5000" in path
else("VND_10000" if "VND_10000" in path
else("VND_20000" if "VND_20000" in path
else("VND_50000" if "VND_50000" in path
else("VND_100000" if "VND_100000" in path
else("VND_200000" if "VND_200000" in path
else("VND_500000" if "VND_500000" in pathelse "NONE")))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) )))))))) )))))))))) ))))) )))))))))) ))))))))) )))))))))) ))))))) ))))) ))))))) )))))))) ))))))))))data.save("money.sframe")
data.explore()dataBuffer = turi.SFrame("money.sframe")
trainingBuffers, testingBuffers = dataBuffer.random_split(0.8)model = turi.image_classifier.create(trainingBuffers, target="moneyType", model="resnet-50")evaluations = model.evaluate(testingBuffers)
print evaluations["accuracy"]model.save("money.model")model.export_coreml("MoneyClassifier.mlmodel")

Generally saying there is turi machine learning module that helps to train object classification MLmodel. I've collected 15000 images and now I try to make this learn these categories (200+). Previously I worked with 5-10 only. However, now I wonder how to implement 100+ categories, because right now for if/else statement technically it is impossible?

As I understood: the Good solution is a loop, but Internet resources use mostly lists for files with extensions. Moreover, I agree that it should be done with DataFrames, but how? I don't understand their possibilities and usefulness.

Answer

I have found the optimal solution: To use def(x) it's the best solution now. (loop)

list = ['enfj','enfp','entj','entp','esfj','esfp','estj','estp','infj','infp','intj','intp','isfj','isfp','istj','istp']def get_label(path, list=list):for psychoType in list:if psychoType in path:return psychoTypedata["psychoType"] = data["path"].apply(get_label)
https://en.xdnf.cn/q/120230.html

Related Q&A

How to fix Python restarting whenever I start program on IDLE environment?

import randomwinning_conditon = 0 no_of_guesses = 0 comp_guess = random.randint(1,100)while (no_of_guesses == 11) or (winning_conditon == 1):user_guess = int(input("What is your guess? "))if…

Fetching Lawyers details from a set of urls using bs4 in python

I am an absolute beginner to Web Scraping using Python and know very little about programming in Python. I am just trying to extract the information of the lawyers in the Tennessee location. In the web…

Having trouble with python simple code running in console [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

.upper not working in python

I currently have this codenum_lines = int(input()) lines = [] tempy = ctr = 1 abc = {a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z } for i in range(0, num_lines):tempy = input()lines.append([])l…

Python SKlearn fit method not working

Im working on a project using Python(3.6) and Sklearn.I have done classifications but when I try to apply it for reshaping in order to use it with fit method of sklearn it returns an error. Heres what …

extracting n grams from huge text

For example we have following text:"Spark is a framework for writing fast, distributed programs. Sparksolves similar problems as Hadoop MapReduce does but with a fastin-memory approach and a clean…

Python: Input validate with string length

Ok so i need to ensure that a phone number length is correct. I came up with this but get a syntax error.phone = int(input("Please enter the customers Phone Number.")) if len(str(phone)) == 1…

Mergesort Python implementation

I have seen a lot of mergesort Python implementation and I came up with the following code. The general logic is working fine, but it is not returning the right results. How can I fix it? Code: def me…

Use variable in different class [duplicate]

This question already has answers here:How to access variables from different classes in tkinter?(2 answers)Closed 7 years ago.I am a beginner in python. I have a problem with using variable in differ…

Embedded function returns None

My function returns None. I have checked to make sure all the operations are correct, and that I have a return statement for each function.def parameter_function(principal, annual_interest_rate, durati…