making a dictionary from a list, one key with multiple values in a list within a list - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: making a dictionary from a list, one key with multiple values in a list within a list (/thread-13553.html) |
making a dictionary from a list, one key with multiple values in a list within a list - rhai - Oct-20-2018 i have an assignment: and basically here it is and i get stuck on this. write = W, read = R, execute= X. The first line contains the number N — the number of files contained in the filesystem. The following N lines contain the file names and allowed operations with them, separated by spaces. The next line contains an integer M — the number of operations to the files. In the last M lines specify the operations that are requested for files. One file can be requested many times. You need to recover the control over the access rights to the files. For each request your program should return OK if the requested operation is valid or Access denied if the operation is invalid. the input is: 4 helloworld.exe R X pinglog W R nya R goodluck X W R 5 read nya write helloworld.exe execute nya read pinglog write pinglog the output: OK Access denied Access denied OK OK i am having trouble with making a dictionary. Just making a dictionary, the rest I think i can figure that out. here is my code so far: N = int(input()) lst =[] W = "write" R = "read" X = "execute" for lines in range(N): lines = str(input()).split() print(lst)this is not much. but it prints [['Helloworld', 'W', 'R'], ['pinglog', 'W'], ['data', 'X', 'W', 'R']] and now i get stuck on how am i going to make it a dictionary. sorry for my last posts with no BB code... RE: making a dictionary from a list, one key with multiple values in a list within a list - stullis - Oct-20-2018 Are you intending to turn the file data into a dictionary or something else? If so, I'm thinking of something like this: file = { "name": "helloworld.exe", "read": True, "write": False, "execute": True }This way, you could easily retrieve the file permissions based on the requested action and run it through a conditional test. Of course, you'll need a function to adequately parse the file permissions and generate the dictionary for each file. RE: making a dictionary from a list, one key with multiple values in a list within a list - marienbad - Oct-20-2018 This will make the "Helloworld", "W", "R" parts into a dictionary, with Helloworld as the key and "W" and "R" as the values. lst = [['Helloworld', 'W', 'R'], ['pinglog', 'W'], ['data', 'X', 'W', 'R']] mydict = {} for item in lst: vals = [] for c in range(1, len(item)): print item vals.append(item[c]) mydict[item[0]] = vals print mydict["Helloworld"] # access an items values in the dict RE: making a dictionary from a list, one key with multiple values in a list within a list - rhai - Oct-20-2018 Wow!!!! amazing! thank you very much! RE: making a dictionary from a list, one key with multiple values in a list within a list - LeSchakal - Oct-24-2018 If you're using Python 2.7 or higher, PEP 274 -- Dict Comprehensions may help you. lst = [['Helloworld', 'W', 'R'], ['pinglog', 'W'], ['data', 'X', 'W', 'R']] my_dict = {item[0] : item[1:] for item in lst} |