Python Forum
Thread Rating:
  • 2 Vote(s) - 1.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List logic
#1
I am practicing with lists and can't figure out this logic... Could someone show me the way to the light :P. Here is what I want to do:

Copy the grid value and write code that uses it to print the image in an upright facing heart...

I know that it's going to use a loop inside a loop in order to print grid[0][0], then grid[1][0], then grid[2][0], and so on up to grid[8][0]. Which will finish the first row, so then print a new line. Then the program should print grid[0][1], then grid[1][1], then grid[2][1], and so on. The last thing it should print would be grid[8][5]. Passing the 'end' keyword argument to print() to avoid newlines printed automatically after each print() call.

Here is the grid:

grid = [['.','.','.','.','.','.'],
        ['.','O','O','.','.','.'],
        ['O','O','O','O','.','.'],
        ['O','O','O','O','O','.'],
        ['.','O','O','O','O','O'],
        ['O','O','O','O','O','.'],
        ['O','O','O','O','.','.'],
        ['.','O','O','.','.','.'],
        ['.','.','.','.','.','.']]
Reply
#2
If anyone knows where I can go to practice list, and dictionary logic for newbies please refer me towards that direction?
Reply
#3
I'm not sure what you need, you've got it all right there. Your paragraph starting with "I know it's going to..." is exactly what I would tell you to do.

There are list and dictionary tutorials in the tutorials sub-forum of this site. I'm sure that you could web search on "python list tutorial" and find more.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
You could print the heart as it is atm, but rotating it.. not so sure

grid = [['.','.','.','.','.','.'],
        ['.','O','O','.','.','.'],
        ['O','O','O','O','.','.'],
        ['O','O','O','O','O','.'],
        ['.','O','O','O','O','O'],
        ['O','O','O','O','O','.'],
        ['O','O','O','O','.','.'],
        ['.','O','O','.','.','.'],
        ['.','.','.','.','.','.']]

for row in grid:
    print ''.join(row)
Reply
#5
What you're trying to do is called a "pivot". It is kind of tricky; rather than trying to roll your own I suggest you take a look at pandas.pivot_table
Reply
#6
(Apr-22-2017, 10:50 AM)ichabod801 Wrote: I'm not sure what you need, you've got it all right there. Your paragraph starting with "I know it's going to..." is exactly what I would tell you to do.

There are list and dictionary tutorials in the tutorials sub-forum of this site. I'm sure that you could web search on "python list tutorial" and find more.

Yeah, I guess I should change the subject of the thread to list comprehension lol. I know the logic behind it but I do not know how to write it... :(

I have another question as well pertaining to dicts / lists...

say I have a Inventory lists such as:

stuff = {'rope' : 1, 'torch' : 6, 'gold coin' : 42, 'dagger' : 1, 'arrow' : 12}

def displayInventory(inventory):
    print('Inventory:')
    item_total = 0
    for k, v in inventory.items():
        print(str(v) + ' ' + k)
        item_total += v
    print('Total number of items: ' + str(item_total))
displayInventory(stuff)
and I want to add a list of loot to the inventory dict... the list of loot being:

def addToInventory(inventory, addedItems):
    # code to add dragonLoot to inventory goes here
    
dragonLoot = ['gold coin', 'dagger', 'gold coin', 'ruby']
how would I go about doing so?
Reply
#7
for item in dragonLoot:
    stuff[item] = stuff.get(item, 0) + 1
This works better with a defaultdict, then you can just add one to the key directly, without having to do the get with a default value of zero.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
(Apr-22-2017, 05:44 PM)ichabod801 Wrote:
for item in dragonLoot:
    stuff[item] = stuff.get(item, 0) + 1
This works better with a defaultdict, then you can just add one to the key directly, without having to do the get with a default value of zero.

tried it and this happens:
stuff = {'rope' : 1, 'torch' : 6, 'gold coin' : 42, 'dagger' : 1, 'arrow' : 12}

def displayInventory(inventory):
    print('Inventory:')
    item_total = 0
    for k, v in inventory.items():
        print(str(v) + ' ' + k)
        item_total += v
    print('Total number of items: ' + str(item_total))

def addToInventory(inventory, addedItems):
    for item in addedItems:
        inventory[item] = inventory.get(item, 0) + 1


dragonLoot = ['gold coin', 'dagger', 'gold coin', 'ruby']
stuff = addToInventory(stuff, dragonLoot)
displayInventory(stuff)
Error:
/usr/bin/python3.5 /root/PythonProjects/AutomateTheBoringStuff/inventory.py Inventory: Traceback (most recent call last):   File "/root/PythonProjects/AutomateTheBoringStuff/inventory.py", line 18, in <module>     displayInventory(stuff)   File "/root/PythonProjects/AutomateTheBoringStuff/inventory.py", line 6, in displayInventory     for k, v in inventory.items(): AttributeError: 'NoneType' object has no attribute 'items' Process finished with exit code 1
Something like this is what I was thinking???


def addToInventory(inventory, addedItems):
    for item in addedItems:

        if item in inventory.keys():
            inventory[item] += 1
            print(inventory)
            return inventory

        elif item not in inventory.keys():
            inventory[item] = 1
            print(inventory)
            return inventory




dragonLoot = ['gold coin', 'dagger', 'gold coin', 'ruby']
stuff = addToInventory(stuff, dragonLoot)
Huh

When i add return inventory to the function it doesn't add all the inventory in dragonLoot:

stuff = {'rope' : 1, 'torch' : 6, 'gold coin' : 42, 'dagger' : 1, 'arrow' : 12}

def displayInventory(inventory):
    print('Inventory:')
    item_total = 0
    for k, v in inventory.items():
        print(str(v) + ' ' + k)
        item_total += v
    print('Total number of items: ' + str(item_total))

def addToInventory(inventory, addedItems):
    for item in addedItems:
        inventory[item] = inventory.get(item, 0) + 1
        return inventory


dragonLoot = ['gold coin', 'dagger', 'gold coin', 'ruby']
stuff = addToInventory(stuff, dragonLoot)
displayInventory(stuff)
Output:
/usr/bin/python3.5 /root/PythonProjects/AutomateTheBoringStuff/inventory.py Inventory: 1 rope 43 gold coin 12 arrow 6 torch 1 dagger Total number of items: 63 Process finished with exit code 0
Reply
#9
You want your return statement in addToInventory to be at the same indentation level as the for loop. That way, it won't execute until the for loop is completely done. As it is now, it executes each time through the loop, so the first time through the loop, it executes and the function ends.

Note that you don't actually need the return statement. Dictionaries are mutable, so when you change inventory in addToInventory, it's also changing stuff in the main program. Basically, stuff and inventory are pointing to the same dictionary. If your next to last line was just:
addToInventory(stuff, dragonLoot)
then it would work without the return statement in addToInventory.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#10
(Apr-22-2017, 09:24 PM)ichabod801 Wrote: You want your return statement in addToInventory to be at the same indentation level as the for loop. That way, it won't execute until the for loop is completely done. As it is now, it executes each time through the loop, so the first time through the loop, it executes and the function ends.

Note that you don't actually need the return statement. Dictionaries are mutable, so when you change inventory in addToInventory, it's also changing stuff in the main program. Basically, stuff and inventory are pointing to the same dictionary. If your next to last line was just:
addToInventory(stuff, dragonLoot)
then it would work without the return statement in addToInventory.

Ok so that worked perfectly, I took out the return statement and changed the 18th line to what you said. It worked great. Thank you very much. I do apologize I'm trying to practice with lists and dicts because i'm real terrible with them. I am getting better with the basic concepts and logics in python but lists and dicts are more difficult... Like my first question about drawing the heart... I know the logic but I have no clue how to write it. Lol.

I wish I could find some kind of worksheets to do with basic terms in python. Like online workshops or something to grade efficiency and terminology used.
Reply


Forum Jump:

User Panel Messages

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