将特定列添加到单行列表(可能有点基本)

发布于 2024-11-07 17:07:14 字数 543 浏览 0 评论 0原文

我正在设计一个密码恢复程序,我想让它“正确”/足够模块化以插入其他程序,所以我试图避免丑陋的黑客攻击。

概要如下: 我有一个包含字符串的列表

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抠脚大汉 2024-11-14 17:07:14

我暂时不清楚你想做什么。 Python 中最接近二维数组的是列表的列表。不过,您现在拥有的是单个列表,而不是二维列表。二维列表如下所示(将这些名称替换为更有意义的名称):

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

要追加一行,只需添加一个列表(即 list_of_lists.append(new_list))。要附加一列,您必须将一个项目添加到教学列表的末尾,如下所示:

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

如果您确实想要二维数组,那么最好使用 numpy.array

但是您是否希望按列标题对各个行建立索引?如果是这样,您最好使用字典列表:

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

您甚至可以将其缩减为一个字典,使用元组来处理单个元素:

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

这些方法中的每一种都适合不同的任务。您甚至可能需要使用数据库。我们需要更多地了解您正在做什么才能告诉您。


好吧,要做你想做的事,根本不需要列表的列表。只需创建一个字符串列表,每个字符串代表密码字符串相应索引处的可能字符。例如,假设用户使用的密码是“appletree”的德语和英语单词的组合,但不记得哪个组合:

>>> 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 现在包含每个索引处的所有可能的字母。要生成所有可能的密码,您需要的只是这些字符串的笛卡尔积:

>>> 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

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
空心空情空意 2024-11-14 17:07:14

如果我正确理解你的问题。看看这个,看看它是否适合你。

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!']]

请注意,列表列表使您能够使用需要关联的任何 python 类型。如果您需要更多解释,请告诉我,我可以提供更多详细信息。

干杯!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文