How to skip certain array indexes?
Use this code instead:
public static int[] sortArray(int[] array) {
int length = array.length;
for (int i = 0; i < length - 1; i++) {
int k = -1; // The index of last odd element
for (int j = 0; j < length - i; j++)
if (array[j] % 2 != 0) {
if (k >= 0 && array[k] > array[j]) {
int temp = array[k];
array[k] = array[j];
array[j] = temp;
}
k = j;
}
}
return array;
}
How to skip specific indexes in an array?
There are a few issues with your algorithm, but it boils down to this: by doing y = L.index(1)
you find the first index where a 1
appears. So by doing L[y] = 0
, all you can do is update the first occurence of a 1
.
There is no builin to find the nth appearance and so you will have to write it.
To be consistent with list.index
, I made the following index
function raise a ValueError
when the item is not found.
Code
def index(lst, obj, n=1):
count = 0
for index, item in enumerate(lst):
if item == obj:
count += 1
if count == n:
return index
raise ValueError('{} is not in list at least {} times'.format(obj, n))
L = [1, 2, 3, 4, 1, 2, 1, 3, 0, 4]
index = index(L, 1, n=3)
L[index] = 0
print(L)
Output
[1, 2, 3, 4, 1, 2, 0, 3, 0, 4]
Using list-comprehensionAlternatively, if all you want to do is replace the nth occurence, but do not care about its actual index, you can generate a new list with a list-comprehension and an itertools.count
object.
Code
from itertools import count
def replace(lst, obj, repl, n=1):
counter = count(1)
return [repl if x == obj and next(counter) == n else x for x in lst]
L = [1, 2, 3, 4, 1, 2, 1, 3, 0, 4]
new_list = replace(L, 1, 0, n=3)
print(new_list)
Output
[1, 2, 3, 4, 1, 2, 0, 3, 0, 4]
How to exclude an array of indexes from loop
You could use the Array.includes() method.
var list = [1,2,3,4];var skipIndexes = [1,3];
for (var i = 0; i< list.length; i++) { if (! skipIndexes.includes(i)) { console.log(list[i]); }}
How can I skip a specific Index in an array in a for loop javascript
I want to skip index 0. How do I do that? Further I want to replace
index 0 with something else.
Just start the loop from 1
instead of 0
sportsArr[0] = "Something else"; // set the first element to something else
for(var i = 1; i < sportsArr.length; i++){
// do something
}
Skip key index from a range of array's key in php
You can get the slice of array from 5th index to rest,
$result = array_slice($array,5,count($array)-5, true);
array_slice — Extract a slice of the array
Note:
array_slice() will reorder and reset the integer array indices by
default. This behaviour can be changed by setting preserve_keys to
TRUE. String keys are always preserved, regardless of this parameter.
Demo.
How to avoid the skipping of the array index in a second array?
Loop through the Arrays as you do and increment a second counter for when a fruit matches one of the case statements. Use the counter to get the year number from the array:
var fruits, text, year, i;fruits = ["Banana", "Orange", "Apple", "Mango"];year = ["0", "1", "2", "3"];text = "<ul>";let counter = 0; // <-- define a second counterfor (i = 0; i < fruits.length; i++) { switch (fruits[i]) { case 'Mango': text += "<li>" + fruits[i] + "</li>"; text += "<li><b>" + year[counter] + "</b></li>"; counter++; // <-- that only get's incremented when a case is matched break; case 'Pine apple': text += "<li>" + fruits[i] + "</li>"; text += "<li><b>" + year[counter] + "</b></li>"; counter++; break; case 'Grape': text += "<li>" + fruits[i] + "</li>"; text += "<li><b>" + year[counter] + "</b></li>"; counter++; break; case 'Banana': text += "<li>" + fruits[i] + "</li>"; text += "<li><b>" + year[counter] + "</b></li>"; counter++; break; }}text += "</ul>";document.getElementById("demo").innerHTML = text;
<p id="demo"></p>
Related Topics
Python Pandas - Get Row Based on Previous Row Value
How to Divide Each Column of Pandas Dataframe by a Series
How to Kill a While Loop With a Keystroke
How to Run Two Python Scripts Simultaneously from a Master Script
Finding Out Who Got the Highest Mark Among the Students
How to Check Whether a Number Is Divisible by Another Number
How to Constantly Run Python Script in the Background on Windows
How to Install Python Packages from the Tar.Gz File Without Using Pip Install
Making a Dictionary from Each Line in a File
Replacing All Negative Values in Certain Columns by Another Value in Pandas
Numpy Array Typeerror: Only Integer Scalar Arrays Can Be Converted to a Scalar Index
Save Variables in Every Iteration of for Loop and Load Them Later
How to Name Dataframes Dynamically in Python
How to Remove Zeros After Decimal from String Remove All Zero After Dot
How to Get Text from Span Tag in Beautifulsoup
How to Sort a Single String Output in Ascii Descending Order Through a Function