Grouping together a list in python -


i trying group self.l list groups of 5 , reverse both individual groups want output follows

    step1.(group  list 2 section of five)      [[1,3,5,67,8],[90,100,45,67,865]]      step2.(reverse numbers within 2 groups)      [[8,67,5,3,1],[865,67,45,100,90]] 

the code have

        class flip():     def __init__(self,answer):         self.answer = answer         self.matrix = none         self.l = [1,3,5,67,8,90,100,45,67,865,]       def flip(self):         if self.answer == 'h':             def grouping(self):                 in range(0, len(self.l), 5):                    self.matrix.append(self.l[i:i+5])             print(self.matrix)  if __name__ =='__main__':     ans = input("type h horizontal flip")     work = flip(ans)     work.flip() 

when run code output is

    none 

to create nested lists 5 elements in each one, can use list comprehension:

lst = [1,3,5,67,8,90,100,45,67,865] new_list = [lst[i:i+5] in range(0, len(lst), 5)] 

then reverse order of values in nested lists can use .reverse()

for elem in new_list:     elem.reverse() print (new_list) 

output:

[[8, 67, 5, 3, 1], [865, 67, 45, 100, 90]] 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -