Here is the code itself:
j = len(list1) for j in range(len(list1),0): slovo += list1[j] j -= 1 Python does not issue errors, it just stops at the moment between
j = len(list1) и for j in range(len(list1,0): Here is the code itself:
j = len(list1) for j in range(len(list1),0): slovo += list1[j] j -= 1 Python does not issue errors, it just stops at the moment between
j = len(list1) и for j in range(len(list1,0): In general, the list is expanded:
print (list1[::-1]) print 's code and look at them as the algorithm works :) - gil9redDo a coup through the for loop:
items = [1, 2, 3, 4, 5, 6] new_items = [] for i in range(len(items)): new_items.append(items[len(items) - i - 1]) # OR: # for i in range(len(items), 0, -1): # new_items.append(items[i - 1]) # OR: # for i in reversed(range(len(items))): # new_items.append(items[i]) print(new_items) # [6, 5, 4, 3, 2, 1] Or done through while :
items = [1, 2, 3, 4, 5, 6] new_items = [] index = len(items) - 1 while index >= 0: item = items[index] new_items.append(item) index -= 1 print(new_items) # [6, 5, 4, 3, 2, 1] In addition to the algorithm through the cycle and reversal through the slice ( [::-1] ), there are specific functions:
list1.reverse() list1 = reversed(list1) # Чтобы получить именно тип список list1 = list(reversed(list1)) reverse expands the items in the list itself
reversed returns a list iterator with expanded items
Source: https://ru.stackoverflow.com/questions/712726/
All Articles
jkept the length of 'list1', when the loop is executed, its value changes according to what therangereturns and there’s no point in trying to change it - it will beforwill take the next value fromrange- gil9red