Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dictionary of Lists

Tags:

c#

I'm trying to build a dictionary of lists, but i'm reading in a string and need to make the lists name the string and add them to the dictionary as a key.

IE read in "hello"

Create the list with what was read in

List<string> (insert read string here ) = new List<string>();

Then add that lists name as the key to a dictionary.

Dictionary.Add(readstring, thatlist);

All I can find is a hard code implementation of this.

Turing.Add("S", S);

My goal: create a Universal Turing Machine, so I read in an input from a text file that is the next step that looks like this, (q0 a) -> q1 X R.

Then use the all the steps that I read in to end in a final state with this on the virtual tape "tape = XXYYZZBB"

I have the the pseudo code written for this but I just cant get the dictionary to work.

EDIT: Adding some more info for less confusion. Im given the start and the end state for the first 2 lines of the text file. Then im given the transitions.

q0 //start state q5 //end state q0 a q1 X R //transitions

Ive stripped the first two lines of input to give me 0 and 5 then have created a for loop to create a lists of each state.

for (int i = 0; i <= endState; i++)
{
List<string> i = new List<string>();
}

I then want to add each lists name as a key to the dictionary of lists I am creating.

Dictionary.Add(listname, thatlist);

I need help on implementing the code for the above as its giving errors.

like image 562
MechaMan Avatar asked Nov 28 '12 08:11

MechaMan


People also ask

What is dictionary of lists in Python?

In Python, the dictionary of lists means we have to assign values as a list. In Python dictionary, it is an unordered and immutable data type and can be used as a keys element.

Can dictionary values be a list?

It definitely can have a list and any object as value but the dictionary cannot have a list as key because the list is mutable data structure and keys cannot be mutable else of what use are they.

How do I create a dictionary in multiple lists?

Use zip() and dict() to create a dictionary from two lists Call zip(iter1, iter2) with one list as iter1 and another list as iter2 to create a zip iterator containing pairs of elements from the two lists. Use dict() to convert this zip iterator to a dictionary of key-value pairs.

Can you make a dictionary of list Python?

You can convert a Python list to a dictionary using the dict. fromkeys() method, a dictionary comprehension, or the zip() method. The zip() method is useful if you want to merge two lists into a dictionary.


1 Answers

It doesn't matter whether you create your list as

List<string> insertReadStringHere = new List<string>();

or

List<string> foo = new List<string>();

or even

List<string> shellBeComingRoundTheMountain = new List<string>();

What's important is that once you've done

MyDictionary.Add(theInputString, shellBeComingRoundTheMountain);

you can then access that particular list via

MyDictionary[theInputString]

wherether the initial list was "called" insertReadStringHere or foo or shellBeComingRoundTheMountain.

You don't even need to hold the list in a named variable like this. For example,

Console.WriteLine("Input a string to create a list:");
var createListName = Console.ReadLine();
// As long as they haven't used the string before...
MyDictionary.Add(createListName, new List<string>());

Console.WriteLine("Input a string to retrieve a list:");
var retrieveListName = Console.ReadLine();
// As long as they input the same string...
List<string> retrievedList = MyDictionary[retrieveListName];

Edit: If you want a certain number of lists, use a dictionarym apping from int to string, not string to string:

int maxNumberOfLists = 5; // or whatever you read from your text file.
Dictionary<int, List<string>> myLists =
            new Dictionary<int, List<string>> (maxNumberOfLists);
for (int i = 1; i <= maxNumberOfLists; i++)
    myLists[i] = new List<string>();

Then you can access your lists as e.g.

var firstList = myLists[1];

Ordinarily I'd recommend an array, but this gives you lists from 1 to 5 rather than from 0 to 4 and it seems that's what you want.

like image 174
Rawling Avatar answered Sep 28 '22 08:09

Rawling