Posts

Showing posts with the label loops

Iterate over every nth element in string in loop - python

Iterate over every nth element in string in loop - python How can iterate over every second element in string ? One way to do this would be(If I want to iterate over nth element): sample = "This is a string" n = 3 # I want to iterate over every third element i = 1 for x in sample: if i % n == 0: # do something with x else: # do something else with x i += 1 Is thery any "pythonic" way to do this? (I am pretty sure my method is not good) for i,x in enumerate(sample,1): – Jean-François Fabre Jul 1 at 9:11 for i,x in enumerate(sample,1): 3 Answers 3 If you want to do something every nth step, and something else for other cases, you could use enumerate to get the index, and use modulus: enumerate sample = ...

printing data a specific number of times with respect to a const and string length

Image
printing data a specific number of times with respect to a const and string length Let there be a sring $str with any number of characters and $cons=25 , also $rest=$cons-strlen( $str ) . $str $cons=25 $rest=$cons-strlen( $str ) Now i wanted to print the string inside boxes so i made a loop where it catches the characters inside the string and echo it inside a div with class w3-border Css: .w3-border { border: 1px solid #f1e1d4 !important; width:16px !important; height:25px; border-collapse: collapse; float: left; } Php: echo'<div class="w3-container" style="margin-bottom:5px !important">'; $str = $row['address']; $strlen = strlen( $str ); $rest=$cons-$strlen; echo'<div class="w3-left" style="width:150px">Address</div>'; for( $i = 0; $i <= $strlen; $i++ ) { $char = substr( $str, $i, 1 ); echo '<div class="w3-border w3-center">'.strtoupper($char).'</div>'; } if...

JAVA - how to modify SWT UI while looping in thread

JAVA - how to modify SWT UI while looping in thread I'm trying to implement a Desktop app that loops trough a function and sets a textfield on the UI every 1 sec. But I either get org.eclipse.swt.SWTException: Invalid thread access when I don't use the display or the UI is really sluggish when I do display.asyncExec(new Runnable() { My code looks like this: public void open() { Display display = Display.getDefault(); shell = new Shell(); shell.setSize(486, 322); shell.setText("SWT Application"); Button btnStartLoop = new Button(shell, SWT.NONE); btnStartLoop.addMouseListener(new MouseAdapter() { @Override public void mouseDown(MouseEvent e) { SwingUtilities.invokeLater(new Runnable() { public void run() // updates displayArea { while (true) { try { text.setText("Text has been set"); ...

PHP For Each loop with radio buttons as results

Image
PHP For Each loop with radio buttons as results ( Just FYI this function is being used on a Wordpress website in my custom plugin ) This function is supposed to display results as radio buttons based on certain parameters. My problem is, I'm not sure how to insert my ID into a radio button. And I'm not sure how to get multiple radio buttons based on the amount of results I get. There should be one radio button per result. To put this situation into context, this is for an (RPG) Role Playing Game similar to "World of Warcraft", or "EverQuest". A user makes a character by entering a name, race and class into a form. This data is inserted into my database table called "wp_ml_character". On a separate page, this function "ml_skill_select" is called, and is supposed to display all of the user's characters and then display skills to choose from based on the user's selection. Here's how It's supposed to work: It's almost lik...

How to add increasing cummulative value in existing decrement loops [on hold]

Image
How to add increasing cummulative value in existing decrement loops [on hold] How would i add increasing cummulative value (Wxj) in my existing loop codes Wx = W/noOfStorey SumWxHx = W*noOfStorey storey = 30 while (storey >= 0): Fx = (( V - Ft ) * Wx * storey )/ SumWxHx SumHx = storey * 3 wh = SumHx *Wx vdf = wh / SumWxHx print("Storey (" + str(storey) + ") / " + str(SumHx) + " / " + str(Wx) + " / Fx = " + str(Fx) + " KN / " + str(wh) + " / " + str(round(vdf,2))) storey = storey - 1 these are results of my existing codes: Planning add other table beside Wx, Wxj (decrementing or cummulative) the result should be when i insert another column: Added column Storey (30) / 90 / 8904.0 / 8904 / Fx .... Storey (29) / 87 / 8904.0 / 817808 / Fx .... Storey (28) / 84 / 8904.0 / 826712 / Fx ... and so on Please clarify your specific problem or add additional details to hi...