Dave
Dave

Reputation: 417

Add specific columns to a single row list (might be a little basic)

I am designing a password recovery program, and I want to make it "right"/modular enough to be plugged into other programs, so I am trying to avoid ugly hacks.

Here's the rundown: I have a list with a string

myString = "Hey what's up?"
myString2DList = [
    myString,
    ]

Now I have a 2D list with a single row. All I want to do is add columns (could be lists themselves) under the users-specified index

So, there could be say 3 indexes (that correspond to a column) like: 0,4,6 (H,w,a) and now I just want to dynamically append anything to those columns. I have done searches and not much has helped (I did see some promising posts that mentioned that the Dict data type might be better to use here), and I feel totally stuck...

Edit/To clarify:

Basically, the first row will be representing a password that the user wants to recover. Let's say the user can't remember their password but can remember at least a few characters. I want the columns to represent each possible alternative for each character, then my script will brute force the password with constraints. I already coded an ugly script that does the same thing, I just have to recode the thing for every password, I want to make it dynamic because it REALLY came in handy.

Thanks!

Upvotes: 1

Views: 4335

Answers (2)

senderle
senderle

Reputation: 151007

It's not immediately clear to me what you're trying to do. The closest thing to a 2D array that Python has is a list of lists. What you have now is single list, though, not a 2D list. A 2D list would look like this (replace these names with more meaningful ones):

list_of_lists = [[header0,  header1,  header2 ],
                 [r1c0data, r1c1data, r1c2data],
                 [r2c0data, r2c1data, r2c2data]]

To append a row, you just add a list (i.e. list_of_lists.append(new_list)). To append a column, you'd have to add an item to the end of teach list like so:

c4data = [header3, r1c3data, r2c3data]
for i, row in enumerate(list_of_lists):
    row.append(c4data[i])

If you really want 2D arrays, you might be better off using numpy.array.

But is your desire to index individual rows by column heading? If so, you'd be better off using a list of dictionaries:

list_of_dicts = [{'column0':r0c0data, 'column1':r0c1data, 'column2':r0c2data},
                 {'column0':r1c0data, 'column1':r1c1data, 'column2':r1c2data}]

You could even cut that down to one dict, using tuples to address individual elements:

tuple_key_dict = {(0, 0):r0c0data, (0, 1):r0c1data, (0, 2):r0c2data,
                  (0, 1):r0c1data, (1, 1):r1c1data, (1, 2):r1c2data}

Each of these methods are suited to different tasks. You might even need to use a database. We need to know more about what you're doing to tell you.


Ok, to do what you want, there's no need for a list of lists at all. Just create a list of strings, each of which represents the possible characters at the corresponding index of the password string. So for example, say the user used a password that was a combination of the German and English words for 'appletree', but can't remember which combination:

>>> char_list = [''.join(set((a, b))) for a, b in zip('apfelbaum', 'appletree')]
>>> char_list
['a', 'p', 'pf', 'el', 'el', 'bt', 'ar', 'eu', 'em']

char_list now contains all possible letters at each index. To generate all possible passwords, all you need is the cartesian product of these strings:

>>> import itertools
>>> password_list = [''.join(tup) for tup in itertools.product(*char_list)]
>>> print 'appletree' in password_list
True
>>> print 'apfelbaum' in password_list
True
>>> print 'apfletrum' in password_list
True

Upvotes: 2

Adam Lewis
Adam Lewis

Reputation: 7257

If I under stand your question properly. Take a look at this to see if it will work for you.

str_1 = 'Hey What\'s Up?'
str_2 = 'Hey, Not Much!'
my2dlist = [[str_1]] #Note the double brackets
my2dlist.append([str_2]) # Note that you are appending a List to the other list.
print my2dlist # Yields: [["Hey What's Up?"], ['Hey, Not Much!']]
my2dlist[0].append(-1)
print my2dlist # Yields: [["Hey What's Up?", -1], ['Hey, Not Much!']]

Note that the list of lists gives you the ability to use whatever python type you need to associate with. If you need more explanation let me know and I can go into more detail.

Cheers!

Upvotes: 2

Related Questions