将特定列添加到单行列表(可能有点基本)
我正在设计一个密码恢复程序,我想让它“正确”/足够模块化以插入其他程序,所以我试图避免丑陋的黑客攻击。
概要如下: 我有一个包含字符串的列表
myString = "Hey what's up?"
myString2DList = [
myString,
]
现在我有一个包含单行的 2D 列表。我想做的就是在用户指定的索引下添加列(可以是列表本身)
因此,可以说有 3 个索引(对应于一列) 例如:0,4,6(H,W,A) 现在我只想动态地将任何内容附加到这些列中。我已经完成了搜索,但没有多大帮助(我确实看到了一些有希望的帖子,提到 Dict 数据类型可能更适合在这里使用),我感觉完全陷入困境......
编辑/澄清:
基本上,第一行将代表用户想要恢复的密码。假设用户不记得他们的密码,但至少可以记住几个字符。我希望这些列代表每个字符的每个可能的替代方案,然后我的脚本将通过约束强制破解密码。我已经编写了一个丑陋的脚本来做同样的事情,我只需要为每个密码重新编码,我想让它动态,因为它真的很方便。
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我暂时不清楚你想做什么。 Python 中最接近二维数组的是列表的列表。不过,您现在拥有的是单个列表,而不是二维列表。二维列表如下所示(将这些名称替换为更有意义的名称):
要追加一行,只需添加一个列表(即
list_of_lists.append(new_list)
)。要附加一列,您必须将一个项目添加到教学列表的末尾,如下所示:如果您确实想要二维数组,那么最好使用
numpy.array
。但是您是否希望按列标题对各个行建立索引?如果是这样,您最好使用字典列表:
您甚至可以将其缩减为一个字典,使用元组来处理单个元素:
这些方法中的每一种都适合不同的任务。您甚至可能需要使用数据库。我们需要更多地了解您正在做什么才能告诉您。
好吧,要做你想做的事,根本不需要列表的列表。只需创建一个字符串列表,每个字符串代表密码字符串相应索引处的可能字符。例如,假设用户使用的密码是“appletree”的德语和英语单词的组合,但不记得哪个组合:
char_list
现在包含每个索引处的所有可能的字母。要生成所有可能的密码,您需要的只是这些字符串的笛卡尔积: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_list
now contains all possible letters at each index. To generate all possible passwords, all you need is the cartesian product of these strings:如果我正确理解你的问题。看看这个,看看它是否适合你。
请注意,列表列表使您能够使用需要关联的任何 python 类型。如果您需要更多解释,请告诉我,我可以提供更多详细信息。
干杯!
If I under stand your question properly. Take a look at this to see if it will work for you.
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!