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!
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):
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: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:
You could even cut that down to one dict, using tuples to address individual elements:
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_listnow contains all possible letters at each index. To generate all possible passwords, all you need is the cartesian product of these strings: