Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Help with Dictionaries and List of Lists
#1
Hello all!

I have been stuck for far too long on this issue and appreciate any and all help.

I have a global dictionary and I'm trying to add to a key a value which is a list of lists.

So like this:

my_dict = { key1 : [ [name, file, withinOH], [name, file, something], ... ], key2 : [ [...], [...] ] }

I do not know how many lists there will be for a certain key in advance so I need to add them as I parse them.

As I parse my input I will get groups of items which I want to put together into a list. Each of these lists are associated with a single key. BUT they cannot all go into the same list because the groupings of items in each list is important for permissions. Thus the need for list of lists.

My plan is to search my dict for a specific key. Then search all lists inside that dict for a specific value. If found, search that list specifically for another value which should be there, if it's not, then I do not grant permissions.

I think I understand part of the problem with my code because when I create the list I want to place inside my dict of lists, it is a local variable. Somehow that forces it to override even when I use .append to my dict.

This is the code:

PA_dict = {}
def addPA(paLine):

    entries = paLine.split("-")

    for entry in entries:
        parts = entry.split(":")
        n_iter = iter(parts)
        attributesLine = next(n_iter).strip()
        permissionName = next(n_iter).strip() # PR or PW
        attributes = attributesLine.split(";")
        PA_dict[permissionName] = []
        my_list = []

        for attribute in attributes:
            attribute = attribute.strip()
            attribute = attribute.strip("<")
            attribute = attribute.strip(">")
            attributeParts = attribute.split(",")
            name = attributeParts[0].strip()
            value = attributeParts[1].strip()
            # print(name + " " + value + " " + permissionName)
            my_list.append(value)

        print(my_list) #this prints out exactly what I need to go into my PA_dict list of lists
        PA_dict[permissionName] = my_list
augh why isn't it indenting my code
ichabod801 wrote Feb-14-2019, 04:09 AM:
Please use python and output tags when posting code and results. The icode tags are for inline code.
Quote
#2
I'm confused. value is a single item, which is appended to my_list. You then set PA_dict[parmissionName] to my_list, but it's just a list of single items, not a list of lists like you were saying. Should you be appending to PA_dict[parmissionName], to make a list of lists?

If that's not the problem, can you provide some sample input, with expected output if possible?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#3
You create a new_list each time and then set the dictionary key to that single list, so any previous lists are lost. Do instead
        for attribute in attributes:
                    .....
            my_list.append(value)
 
        print(my_list) #this prints out exactly what I need to go into my PA_dict list of lists
        ## adds new list, and keeps previous lists
        PA_dict[permissionName].append(my_list) 
Quote
#4
Thank you for your reply woooee and ichabod801,

I have tried:

PA_dict[permissionName].append(my_list)

but it overrides what is already there and I'm left with only the last parsed list appearing in PA_dict when I exit the function. (in my case it's {'PR': [['"Carlos"']], 'PW': [['"Carlos"']]} )

Sample input would be the string:

<fileName, "grades.txt">; <role, "Student">; <currentTime,WithinOH> : PR
- <fileName, "grades.txt">; <role, "Professor">; <currentTime, WithinOH> : PR
- <fileName, "grades.txt">; <role,"Professor">; <currentTime, WithinOH> : PW
- <fileName, "grades.txt>; <role, "Dean">; <currentTime, WithinOH> : PW
- <fileOwnerName,"Carlos"> : PR
- <fileOwnerName, "Carlos"> : PW
Quote
#5
You're resetting PA_dict[permissionName] each time through the loop. Put PA_dict[permissionName] = [] before the loop.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#6
Thank you ichabod801!

I needed the permissionName to create the new key so I couldn't see how to take it out of the loop. It kind of looks like a mess, maybe there's a better way to do it but I used a flag of sorts to see if PermissionName is a new one, if it is then I create the new list, else I append.


def addPA(paLine):
    entries = paLine.split("-")
    firstTime = 1
    for entry in entries:
        parts = entry.split(":")
        n_iter = iter(parts)
        attributesLine = next(n_iter).strip()
        permissionName = next(n_iter).strip() # PR or PW
        firstTime = 1
        for key in PA_dict:
            if key == permissionName:
                firstTime = 0
        attributes = attributesLine.split(";")
        # PA_dict[permissionName]
        my_list = []
        for attribute in attributes:
            attribute = attribute.strip()
            attribute = attribute.strip("<")
            attribute = attribute.strip(">")
            attributeParts = attribute.split(",")
            name = attributeParts[0].strip()
            value = attributeParts[1].strip()
            # print(name + " " + value + " " + permissionName)
            my_list.append(value)
        print(my_list)
        if(firstTime):
            PA_dict[permissionName] = [my_list]
            firstTime = 0
        else:
            PA_dict[permissionName].append(my_list)
Quote
#7
Have you ever used collections.defaultdict?

import collections

PA_dict = collections.defaultdict(list)

def addPA(paLine):
    entries = paLine.split("-")
    firstTime = 1
    for entry in entries:
        parts = entry.split(":")
        n_iter = iter(parts)
        attributesLine = next(n_iter).strip()
        permissionName = next(n_iter).strip() # PR or PW
        attributes = attributesLine.split(";")
        my_list = []
        for attribute in attributes:
            attribute = attribute.strip()
            attribute = attribute.strip("<")
            attribute = attribute.strip(">")
            attributeParts = attribute.split(",")
            name = attributeParts[0].strip()
            value = attributeParts[1].strip()
            # print(name + " " + value + " " + permissionName)
            my_list.append(value)
        print(my_list)
        PA_dict[permissionName].append(my_list)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#8
import collections
 
PA_dict = collections.defaultdict(list)
That is so beautiful. My code looks so much better, thank you.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Passing Values of Dictionaries to Function & Unable to Access Them firebird 3 158 Aug-03-2019, 10:25 PM
Last Post: firebird
  Dictionaries help Rayaan 5 140 Aug-02-2019, 06:21 AM
Last Post: Malt
  nested dictionaries to CSV mart79 9 349 Jul-29-2019, 04:59 AM
Last Post: mart79
  Why does this list of lists end up with the same values alehak 2 145 Jul-18-2019, 12:34 PM
Last Post: perfringo
  How can I save Python dictionaries in Matlab? jlostinco 1 200 Jul-04-2019, 11:35 PM
Last Post: scidam
  Transform simplified dictionary to nested dictionaries bhojendra 1 141 Jul-02-2019, 02:05 PM
Last Post: ichabod801
  Store a python list of lists in a database to use for searches later on klllmmm 3 389 Jun-20-2019, 07:54 AM
Last Post: buran
  List of dictionaries: use key-value instead of index? dn237 6 375 Jun-03-2019, 05:52 PM
Last Post: Yoriz
  Search a List of Dictionaries by Key-Value Pair; Return Dictionary/ies Containing KV dn237 19 583 May-29-2019, 02:27 AM
Last Post: heiner55
  creating an 'adress book' in python using dictionaries? apollo 6 394 May-06-2019, 12:03 PM
Last Post: snippsat

Forum Jump:


Users browsing this thread: 1 Guest(s)