logo
down
shadow

What's the most concise way to include multiple statements in a Python switch case?


What's the most concise way to include multiple statements in a Python switch case?

Content Index :

What's the most concise way to include multiple statements in a Python switch case?
Tag : python , By : Vasiliy
Date : November 25 2020, 07:06 PM


Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Switch statement > possible to include multiple case matches in a single case?


Tag : php , By : Ron
Date : March 29 2020, 07:55 AM
will be helpful for those in need I'd like to pass multiple values in a single switch case. I realize its not possible they way I'm trying to do it. Is there another way, short of placing each case on its on line?
switch(get_option('my_template')) {
    case 'test1':
    case 'test2':
        return 850;
        break;    
    default:
        return 950;
}

Can someone give an example when to let switch statements use multiple case statements?


Tag : javascript , By : mobi phil
Date : March 29 2020, 07:55 AM
I hope this helps . I'm reading Javascript: the Definitive Reference, and it says on page 93: , One useful case is when several values should have the same result:
switch (value) {
  case 1:
  case 2:
    action1();
    break;
  case 3:
  case 4:
  case 5:
    action2();
    break;
}
switch (value) {
  case 1:
    action1();
    break;
  case 2:
    action2();
    // continue with action3
  case 3:
    action3();
    break;
}

Multiple Switch-Case statements


Tag : javascript , By : Ir0nh1de
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Intrigued I decided to make a very quick test. It seems there's very little difference performance-wise if the case is random.. (strangely on IE the double switch seems to perform worse compared to chrome.) Smallswitch does have the extra math.floor and dividing of course..
Maybe significant differences are visible as the switch gets much larger, but I'd say on average it would still tend to be down to luck (which cases are being hit, an earlier one or a later one) which is faster.
function bigSwitch(value){
    switch(value){
        case 1:
            break;
        case 2:
            break;
        case 3:
            break;
        case 4:
            break;
        case 5:
            break;
        case 6:
            break;
        case 7:
            break;
        case 8:
            break;
        case 9:
            break;
        case 10:
            break;
        case 11:
            break;
        case 12:
            break;
        case 13:
            break;
        case 14:
            break;
        case 15:
            break;
        case 16:
            break;
        case 17:
            break;
        case 18:
            break;
        case 19:
            break;
        case 20:
            break;    
    }
}

function smallSwitch(value){    
    switch(Math.floor(value/5)+1){
        case 1:
            switch(value%5){
                case 1:
                    break;
                case 2:
                    break;
                case 3:
                    break;
                case 4:
                    break;
                case 5:
                    break;
            }
            break;
        case 2:
            switch(value%5){
                case 1:
                    break;
                case 2:
                    break;
                case 3:
                    break;
                case 4:
                    break;
                case 5:
                    break;
            }
            break;
        case 3:
            switch(value%5){
                case 1:
                    break;
                case 2:
                    break;
                case 3:
                    break;
                case 4:
                    break;
                case 5:
                    break;
            }
            break;
        case 4:
            switch(value%5){
                case 1:
                    break;
                case 2:
                    break;
                case 3:
                    break;
                case 4:
                    break;
                case 5:
                    break;
            }
            break;
    }
}

var runsToDo = 10000000;
var timer = Date.now();
for(var i=0; i<runsToDo; i++){
    var value = Math.floor(Math.random()*20) + 1; 
    bigSwitch(value);     
}
console.log("BigSwitch time = ", Date.now() - timer); 

timer = Date.now();
for(var i=0; i<runsToDo; i++){
    var value = Math.floor(Math.random()*20) + 1; 
    smallSwitch(value); 
}
console.log("smallSwitch time = ", Date.now() - timer);

Switch with multiple statements for same case


Tag : development , By : Alecsandru Soare
Date : March 29 2020, 07:55 AM
this one helps. You could delete the second case 1 in your code and move statement2 to your first case 1. But this only works if the order of the statements is not important (i.e. statement2 can be executed before statement1):
switch (i)
{
    case 1:
        statement2;
        //Fall through

    case 2:
        statement1;
        break;

    default:
        pleaseDoNotForgetMe();
}
switch (i)
{
    case 1:
        statement1;
        statement2;
        break;

    case 2:
        statement1;
        break;

    default:
        pleaseDoNotForgetMe();
}

C++ Utility to convert long switch statement into the concise function call which encapsulates the switch case ladder


Tag : cpp , By : Carter
Date : March 29 2020, 07:55 AM
Related Posts Related QUESTIONS :
  • Sort strings in pandas
  • How do access my flask app hosted in docker?
  • Replace the sentence include some text with Python regex
  • Counting the most common element in a 2D List in Python
  • logout a user from the system using a function in python
  • mp4 metadata not found but exists
  • Django: QuerySet with ExpressionWrapper
  • Pandas string search in list of dicts
  • Decryption from RSA encrypted string from sqlite is not the same
  • need of maximum value in int
  • a list of several tuples, how to extract the same of the first two elements in the small tuple in the large tuple
  • Display image of 2D Sinewaves in 3D
  • how to prevent a for loop from overwriting a dictionary?
  • How To Fix: RuntimeError: size mismatch in pyTorch
  • Concatenating two Pandas DataFrames while maintaining index order
  • Why does this not run into an infinite loop?
  • Python Multithreading no current event loop
  • Element Tree - Seaching for specific element value without looping
  • Ignore Nulls in pandas map dictionary
  • How do I get scrap data from web pages using beautifulsoup in python
  • Variable used, golobal or local?
  • I have a regex statement to pull all numbers out of a text file, but it only finds 77 out of the 81 numbers in the file
  • How do I create a dataframe of jobs and companies that includes hyperlinks?
  • Detect if user has clicked the 'maximized' button
  • Does flask_login automatically set the "next" argument?
  • Indents in python 3
  • How to create a pool of threads
  • Pandas giving IndexError on one dataframe but not on another similar dataframe
  • Django Rest Framework - Testing client.login doesn't login user, ret anonymous user
  • Running dag without dag file in airflow
  • Filling across a specified dimension of a numpy array
  • Python populating dataframe in pandas from text files
  • How to interpolate a single ("non-piecewise") cubic spline from a set of data points?
  • Divide 2 integers (leetcode 29) - recursion issue
  • Can someone explain why do I get this output in Python?
  • How do I scrape pdf and html from search results without obvious url
  • Is there a way to automatically make a "collage" of plots with matplotlib?
  • How to combine multiple rows in pandas with shared column values
  • How do I get LOAD_CLASSDEREF instruction after dis.dis?
  • Django - How to add items to Bootstrap dropdown?
  • Linear Regression - Does the below implementation of ridge regression finding coefficient term using gradient method is
  • How to drop all rows in pandas dataframe with negative values?
  • Most Efficient Way to Find Closest Date Between 2 Dataframes
  • Execution error when Passing arguments to a python script using os.system. The script takes sys.argv arguments
  • Looping through a function
  • Create a plot for each unique ID
  • a thread python with 'while' got another thread never start
  • Solution from SciPy solve_ivp contains oscillations for a system of first-order ODEs
  • trigger python events driven by selenium controlled browser
  • Passing line-edits to a contextmanager to set validators
  • Python: globals().items() iterations try to change a dict
  • Is it possible to specify starting values for each parameter (instead of bounds) for scipy's differential evolution?
  • why datetime.now() and constructed datetime using all fields(like year,month...) of now has big timedelta?
  • MySQL multiple table UPDATE query using sqlalchemy core?
  • find if a semantic version is superset of of another version python
  • Type checking against dynamically created objects
  • Struggling with simple reverse function
  • Is there a function for finding the midpoint of n points on sklearn.neighbors.NearestNeighbors?
  • How to set max number of tweets to fetch
  • PYTHON 3.7.4 NOT USING SQLITE 3.29.0
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com