Mar-23-2020, 01:34 PM
I would appreciate some help with this, please...
To summarize my scenario -
My input is an array of numbers wherein
Row1 - [1,2,3]
Row2 - [4,5,6]
Row3 - [7,8,9]
And I am trying to output columns looping through the above rows and need
Col1 - [1,4,7]
Col2 - [2,5,8]
Col3 - [3,6,9]
This is half-way through a program and I am stuck trying to get past this part.
I have some extra code in there as I am currently checking for 9 numbers instead of the above 3 shown for illustration and I am also checking for invalid values, duplicate numbers entered,etc
I have tried to use append but got
To summarize my scenario -
My input is an array of numbers wherein
Row1 - [1,2,3]
Row2 - [4,5,6]
Row3 - [7,8,9]
And I am trying to output columns looping through the above rows and need
Col1 - [1,4,7]
Col2 - [2,5,8]
Col3 - [3,6,9]
This is half-way through a program and I am stuck trying to get past this part.
I have some extra code in there as I am currently checking for 9 numbers instead of the above 3 shown for illustration and I am also checking for invalid values, duplicate numbers entered,etc
rowDict = {} colDict = {} def checkIfDuplicates(listOfElems): ''' Check if given list contains any duplicates ''' for elem in listOfElems: if listOfElems.count(elem) > 1: return True return False for x in range(1,10): rowDict["row{0}".format(x)] = input("Enter a row of numbers (1-9) : ") if len(rowDict["row{0}".format(x)]) > 9: print("Longer than 9 digits!") break try: rowDict["row{0}".format(x)] = [int (y) for y in (rowDict["row{0}".format(x)])] print(rowDict["row{0}".format(x)]) except ValueError: print ("Invalid value found!") break result = checkIfDuplicates(rowDict["row{0}".format(x)]) if result: print('Row contains duplicate vlaues!') break for x in range(0,9): for y in range(1,10): colDict["column{0}".format(y)] = rowDict["row{0}".format(y)][x] print (rowDict) print (colDict)And my output currently looks like this -
Output:Enter a row of numbers (1-9) : 123456789
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Enter a row of numbers (1-9) : 123456789
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Enter a row of numbers (1-9) : 123456789
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Enter a row of numbers (1-9) : 123456789
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Enter a row of numbers (1-9) : 123456789
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Enter a row of numbers (1-9) : 123456789
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Enter a row of numbers (1-9) : 123456789
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Enter a row of numbers (1-9) : 123456789
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Enter a row of numbers (1-9) : 123456789
[1, 2, 3, 4, 5, 6, 7, 8, 9]
{'row1': [1, 2, 3, 4, 5, 6, 7, 8, 9], 'row2': [1, 2, 3, 4, 5, 6, 7, 8, 9], 'row3': [1, 2, 3, 4, 5, 6, 7, 8, 9], 'row4': [1, 2, 3, 4, 5, 6, 7, 8, 9], 'row5': [1, 2, 3, 4, 5, 6, 7, 8, 9], 'row6': [1, 2, 3, 4, 5, 6, 7, 8, 9], 'row7': [1, 2, 3, 4, 5, 6, 7, 8, 9], 'row8': [1, 2, 3, 4, 5, 6, 7, 8, 9], 'row9': [1, 2, 3, 4, 5, 6, 7, 8, 9]}
{'column1': 9, 'column2': 9, 'column3': 9, 'column4': 9, 'column5': 9, 'column6': 9, 'column7': 9, 'column8': 9, 'column9': 9}
>>>
I see that I am not able to create a list out of the columns and they are single elements and being over-written as a result.I have tried to use append but got
Error:colDict["column{0}".format(y)].append(rowDict["row{0}".format(y)][x])
AttributeError: 'int' object has no attribute 'append'
I would like to understand how I can create a list that contains numbers similar to how I have with 'rows'. Appreciate you reading through the long post and if you can help, that would be great