List if-else not giving the expected result

Related searches

I have the following list of list:

[['3', 2], ['2370447', 282], ['5300058', 610], ['81615', 615], ['3294332', 624], ['3078798', 624], ['1804986', 643]]

Please note that in index 5 and 6, the second item is identical. I am trying to turn this list ordered on the second item in each list into a ranking.

I used the following if-else to correct for time when values were the same:

for i in range(len(sortedCounts)):
   if i == 0:
          sortedCounts[i][1] = 0
   elif sortedCounts[i][1] == sortedCounts[i-1][1]:
          sortedCounts[i][1] = i-1
   else: 
          sortedCounts[i][1] = i

However, when I print the list, I am getting different rankings even when the numbers are the same:

[['3', 0], ['2370447', 1], ['5300058', 2], ['81615', 3], ['3294332', 4], ['3078798', 5], ['1804986', 6]]

Expected output is:

[['3', 0], ['2370447', 1], ['5300058', 2], ['81615', 3], ['3294332', 4], ['3078798', 4], ['1804986', 6]]

Appreciate any advice, or if there is a better way, please advise.

rank, last_value = -1, -1
for i, e in enumerate(sortedCount):
    if last_value < e[1]:
        rank = i
    last_value = e[1]
    sortedCount[i][1] = rank

if / else errors, Depending on what you are checking for, the else if and else blocks may not be needed. Any code following that will be processed without regard for the outcome of that if condition. else or your else if ( ) , doing that will also end your if statement sooner than you had planned. These are both going to give you errors: Excel formula not giving the expected answer. I started with H1=2.00. Then H2=H1+0.01. Copied this formula 71 times in the H column below H2; that is in cells H3-H73. As a result H73 contains the value 2.72. So far so good. Then I enter the formula in H74. The formula :- =IF(H73=2.72,1,"N"). The answer in H74 is "N" which is wrong. It should be

I think this is the code you expected

sortedCounts=[['3', 2], ['2370447', 282], ['5300058', 610], ['81615', 615], ['3294332', 624], ['3078798', 624], ['1804986', 643]]
i=0
while i<len(sortedCounts):
    if(sortedCounts[i][1]==sortedCounts[i+1][1]):
        sortedCounts[i][1]=sortedCounts[i+1][1]=i
        i=i+1
    elif(i==len(sortedCounts)):
        sortedCounts[i][1]=i
    else:
        sortedCounts[i][1]=i
    i=i+1

if else in jq is not giving expected output, You can use the jq function select : <file jq -r '.[] | select(.realm == "internal") | . name'. The first .[] gets the array elements. The select() applies to� Re: "IF" statement not giving expected results I am not saying that the MOD() doesn't work, but if it does, than so does the CHOOSE() given. They both gave same results for me.

Sorting by index 1 using sorted:

input = [['3', 2],['2370447', 282],['5300058', 610],['81615', 615],['3294332', 624],['3078798', 624],['1804986', 643]]
output = []
for i, x in enumerate(sorted(input, key=itemgetter(1))):
    x[1] = i
    output.append(x)

Short and sweet.

[['3', 0], ['2370447', 1], ['5300058', 2], ['81615', 3], ['3294332', 4], ['3078798', 5], ['1804986', 6]]

Conditional branching: if, '?', statement evaluates a condition in parentheses and, if the result is true It's not recommended to use the question mark operator in this way. In this ArticleVBA If StatementIf ThenEnd IfElseIF – Multiple ConditionsElseIf-ElseNested IFsIF – Or, And, Xor, NotIf OrIf AndIf XorIf NotIf ComparisonsIf – Boolean FunctionComparing TextVBA If LikeIf LoopsIf Else ExamplesCheck if Cell is EmptyCheck if Cell Contains Specific TextCheck if cell contains textIf GotoDelete Row if Cell is BlankIf MessageBox Yes / No VBA If…

using inbuilt sort function can solve this issue easily

input_data = [['3', 2], ['2370447', 282], ['5300058', 610], ['81615', 615], ['3294332', 624], ['3078798', 624], ['1804986', 643]]

input_data.sort(key = lambda x: int(x[0]))
result = input_data.copy()
for i in range(len(result)):
    result[i][1] = i

print(result)

"""
output 
[['3', 0], ['81615', 1], ['1804986', 2], ['2370447', 3], ['3078798', 4], ['3294332', 5], ['5300058', 6]]
"""

ifelse, The if statement executes a statement if a specified condition is truthy. If the condition is expected output: "NOT positive". 13. . Run › Reset. Expected results: As the location and currency are selected in the header (France- EUR), products presented in Euros would be expected. In the example above, Summary / Actual Results / Expected

As far as I understood, the main reason you are not getting same index for elements at 5 and 6 is because your code is not going into elif statement because on updating the list

with value 
    sortedCounts[i][1] = i-1

which changes the list.

So, instead of directly changing the list you can create a variable change it and append it to a new list.

    newList = []
    for i in range(len(sortedCounts)):
        if i == 0:
            variable = 0
        elif sortedCounts[i][1] == sortedCounts[i-1][1]:
            variable = i-1
        else: 
            variable = i
        newList.append([sortedCounts[i][0], variable])

3.1. If Statements — Hands-on Python Tutorial for Python 3, As you an see, you get an extra result, depending on the input. If the condition is not true, then skip the indented statements. for both the functions range and randrange , the end stated is past the last value actually desired: def printShort( strings): '''Given a list of strings, print the ones with at most three characters. @shihon When 'link' in links.keys() is False, a Python list comprehension skips over the expression to add {'albums': links['link']} to the list. Your code expanded would behave the same way as [x+1 for x in l if x >= 45] in my answer above. – arboc7 Oct 19 '13 at 18:44

results are projected. results are suspected. performances are expected. results are predicted. results are protected. results enable expected. findings are expected. results are hoped. results suggest expected. results support expected. results are suggested. results are documented. results are gossamer. results are attained. results are fed

Hi guys, i am new to this thread, hopefully can get some help from here. i have an excel file that contains 2000 records. so lets say year 1- 2 they have a score of 1, and year 3-4 score of 2 and year 5-6 score of 3, year 7-8 score of 4 and lastly, year 9-11 score of 5 N2 is year 11, M2 is year 10 and L2 is year 9.

Show Formulas is mean to give you a quick way to see all formulas in a worksheet. However, if you accidentally trigger this mode, it can be quite disorienting. With Show Formulas enabled, columns are widened, and every formula in a worksheet is displayed with no results anywhere in sight, as shown in the screens below.

Comments
  • What’s the expected output?
  • what is your expected output?
  • Sorry - updated
  • sortedCounts[i][1] == sortedCounts[i-1][1] is not doing what you want, because you overwrote the right side of that expression with i or i-1 on the previous iteration of the loop.
  • I see what you are saying now. I am trying a solution where I just move everything to a new list of lists but have a tough time with the indexing.
  • This almost worked. I expected to get a 6 in the last list of lists, but this elegant solution gives me 5: [['3', 0], ['2370447', 1], ['5300058', 2], ['81615', 3], ['3294332', 4], ['3078798', 4], ['1804986', 5]]
  • Sorry, i missed that point. I edited my answer, replaced rank += 1 into rank = i. It may works fine now.
  • I was working on something similar per my note above but by using another list. Your code almost work,[['3', 0], ['2370447', 1], ['5300058', 2], ['81615', 3], ['3294332', 4], ['3078798', 4], ['1804986', 643]] but the last item in the list is not changing:
  • I have some mistake because it should be i< len(sortedCounts). It recorrect that. Now try that code
  • This works as well, it's closer to the solution I was trying but was messing up the indexing - thank you for your help