Python Forum
QUERY on Looping and creating lists as items within dictionaries
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
QUERY on Looping and creating lists as items within dictionaries
#1
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. Wall
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 Idea
Reply


Messages In This Thread
QUERY on Looping and creating lists as items within dictionaries - by ajayachander - Mar-23-2020, 01:34 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help please. Looping the same SQL query over different databases and combine them danlin123 3 2,177 Sep-23-2020, 02:31 PM
Last Post: danlin123
  Dictionaries in Lists szbeco 6 2,841 Nov-07-2019, 10:16 AM
Last Post: szbeco
  Help with Dictionaries and Lists of Lists Nate5800 5 96,434 Apr-23-2018, 06:42 PM
Last Post: Nate5800
  Trouble displaying items from lists Liquid_Ocelot 8 6,642 May-18-2017, 01:37 PM
Last Post: Ofnuts
  Creating lists or arrays as the input nickalvar 4 4,334 May-03-2017, 04:46 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020