Python is expressed in an equivalent sequence.//

Asked 2 years ago, Updated 2 years ago, 19 views

import random
import math
import numpy as np
import pandas as pd
import xlwings as xw
arr1,arr2,arr3 = [],[],[]
arr4,arr5,arr6 = [],[],[]
arr7,arr8,arr9 = [],[],[]
arr10,arr11,arr12 = [],[],[]
arr13,arr14,arr15 = [],[],[]
arr16,arr17,arr18 = [],[],[]
arr19,arr20,arr21 = [],[],[]
arr22,arr23,arr24 = [],[],[]
arr25,arr26,arr27 = [],[],[]
arr28,arr29,arr30 = [],[],[]
arr31,arr32,arr33 = [],[],[]
arr34,arr35,arr36 = [],[],[]
for i in range(70):
        ax = int(random.choice(A1))
        result1 = ax
        arr1.append(result1)
for i in range(20):
        ax = int(random.choice(B1))
        result2 = ax
        arr2.append(result2)
for i in range(10):
        ax = int(random.choice(C1))
        result3 = ax
        arr3.append(result3)

A1 = (1,2,3,4,5,6,7,8,9,10,6,7,8,9,10,11,12,13,14,15)
B1 = (21,22,23,24,25,26,27,28,29,30,26,27,28,29,30,31,32,33,34,35,41,42,43,44,45,46,47,48,49,50)
C1 = (46,47,48,49,50,51,52,53,54,55,61,62,63,64,66,67,68,69,70,66,67,68,69,70,71,72,73,74,75,81,82,83,84,85,86,87,88,89,90,86,87,88,89,90,91,92,93,94,95,101,102,103,104,105,106,107,108,109,110,106,107,108,109,110,111,112,113,114,115)
print('Section A',
          sorted_arr1[69] - sorted_arr1[0] + sorted_arr1[1] + sorted_arr1[3] + sorted_arr1[5] + sorted_arr1[7] +
          sorted_arr1[9] + sorted_arr1[11] + sorted_arr1[13] + sorted_arr1[15] + sorted_arr1[17] + sorted_arr1[19] +
          sorted_arr1[21] + sorted_arr1[23] + sorted_arr1[25] + sorted_arr1[27] + sorted_arr1[29] + sorted_arr1[31] +
          sorted_arr1[33] + sorted_arr1[35] + sorted_arr1[37] + sorted_arr1[39] + sorted_arr1[41] + sorted_arr1[43] +
          sorted_arr1[45] + sorted_arr1[47] + sorted_arr1[49] + sorted_arr1[51] + sorted_arr1[53] + sorted_arr1[55] +
          sorted_arr1[57] + sorted_arr1[59] + sorted_arr1[61] + sorted_arr1[63] + sorted_arr1[65] + sorted_arr1[67] +
          sorted_arr1[69])
 print('B section',
          sorted_arr2[19] - sorted_arr2[0] + sorted_arr2[1] + sorted_arr2[3] + sorted_arr2[5] + sorted_arr2[7] +
          sorted_arr2[9] + sorted_arr2[11] + sorted_arr2[13] + sorted_arr2[15] + sorted_arr2[17] + sorted_arr2[19])
 print('C section',
          sorted_arr3[9] - sorted_arr3[0] + sorted_arr3[1] + sorted_arr3[3] + sorted_arr3[5] + sorted_arr3[7] +
          sorted_arr3[9])

It's roughly like this. I want to express it like sorted_arr1[0t], sorted_arr1[1t], sorted_arr1[2t], sorted_arr1[3t] Sorted_arr1[10t] An error appears when the number of len in the arr data is out. Is there a solution

python

2022-09-20 14:24

2 Answers

I think the question code is different from the question.

You can use the for loop. You can do it while.

import random
import math
arr1,arr2,arr3 = [],[],[]

A1 = (1,2,3,4,5,6,7,8,9,10,6,7,8,9,10,11,12,13,14,15)
B1 = (21,22,23,24,25,26,27,28,29,30,26,27,28,29,30,31,32,33,34,35,41,42,43,44,45,46,47,48,49,50)
C1 = (46,47,48,49,50,51,52,53,54,55,61,62,63,64,66,67,68,69,70,66,67,68,69,70,71,72,73,74,75,81,82,83,84,85,86,87,88,89,90,86,87,88,89,90,91,92,93,94,95,101,102,103,104,105,106,107,108,109,110,106,107,108,109,110,111,112,113,114,115)

for i in range(70):
        ax = int(random.choice(A1))
        result1 = ax
        arr1.append(result1)
for i in range(20):
        ax = int(random.choice(B1))
        result2 = ax
        arr2.append(result2)
for i in range(10):
        ax = int(random.choice(C1))
        result3 = ax
        arr3.append(result3)

arr1.sort() # Sort Ascending
arr2.sort() # Sort Ascending
arr3.sort() # Sort Ascending

def func(arr, n ,t):
    print(arr)
    for i in range(n, len(arr), t):
        print(f"arr[{i}] : {arr[i]}")

func(arr1,1,3)
[1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 14, 15, 15, 15]
arr[1] : 1
arr[4] : 2
arr[7] : 3
arr[10] : 4
arr[13] : 5
arr[16] : 5
arr[19] : 6
arr[22] : 6
arr[25] : 7
arr[28] : 7
arr[31] : 8
arr[34] : 8
arr[37] : 9
arr[40] : 9
arr[43] : 9
arr[46] : 9
arr[49] : 10
arr[52] : 10
arr[55] : 11
arr[58] : 11
arr[61] : 13
arr[64] : 14
arr[67] : 15


2022-09-20 14:24

Slicing with very basic steps

arr[::2]
arr[::3]
arr[::4]

arr[1::2]


2022-09-20 14:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.