简体   繁体   English

检查项目是否在列表列表中

[英]Checking if an item is in a list of lists

I am making a password saver and I have a pre-determined list of lists for testing purposes which is:我正在制作密码保护程序,并且我有一个用于测试目的的预先确定的列表列表,它是:

passwords = [["yahoo","XqffoZeo"],["google","CoIushujSetu"]]

I give the user a number of choices.我给用户多种选择。 Choice #2 gives them the option to lookup a password选项 #2 为他们提供了查找密码的选项

if choice == '2':  # Lookup a password
    print("Which website do you want to lookup the password for?")
    for keyvalue in passwords:
        print(keyvalue[0])
    passwordToLookup = input()

What I need to figure out is:我需要弄清楚的是:

  1. Setting up a loop that loops through all items in the list of lists using a FOR loop.设置一个循环,使用 FOR 循环遍历列表列表中的所有项目。 I was told that the best way to think about a list of lists is like an Excel spreadsheet.有人告诉我,考虑列表列表的最佳方式就像 Excel 电子表格。 In the case of my passwords list, I start with 2 rows and 2 columns of data (not considering any passwords that might get added by the user).对于我的密码列表,我从 2 行 2 列数据开始(不考虑用户可能添加的任何密码)。 So, passwords[0][0] will be equal to the item in row 1, column 1 (in my case, "yahoo").因此,passwords[0][0] 将等于第 1 行第 1 列中的项目(在我的情况下为“雅虎”)。 I was told to use a combination of range() and len() so I can iterate through each "row", regardless of how long the list may be.有人告诉我使用 range() 和 len() 的组合,这样我就可以遍历每个“行”,无论列表有多长。 But, I am not exactly sure how to accomplish that.但是,我不确定如何做到这一点。
  2. Once I get the FOR loop set up correctly, I need to then iterate through each "row" and compare the user's passwordToLookup input to the corresponding website name using conditional logic (eg "if...").一旦我正确设置了 FOR 循环,我需要遍历每个“行”并使用条件逻辑(例如“if...”)将用户的 passwordToLookup 输入与相应的网站名称进行比较。 Since I know that the website names are stored in the first "column" that it might look something like this: passwords[i][0].因为我知道网站名称存储在第一个“列”中,所以它可能看起来像这样:passwords[i][0]。 But once again, all my attempts fail and I need guidance.但再一次,我所有的尝试都失败了,我需要指导。

Using filter , it is possible to select the matching element(s) in the list.使用filter ,可以在列表中选择匹配的元素。

Using a list comprehension, select the second element (password) of each.使用列表理解,选择每个元素的第二个元素(密码)。

passwords = [["yahoo","XqffoZeo"],["google","CoIushujSetu"]]
website_to_lookup = input()
[e[1] for e in filter(lambda i:i[0]==website_to_lookup,passwords)]

And if you have more than one password per website, use slicing to return them all: [e[1:] for e in filter(lambda i:i[0]==website_to_lookup,passwords)]如果每个网站有多个密码,请使用切片返回所有密码: [e[1:] for e in filter(lambda i:i[0]==website_to_lookup,passwords)]

Python makes many things easy and one of them is iterating through a list(or Iterator) Python 使许多事情变得简单,其中之一是遍历列表(或迭代器)

Suppose this is your list of list:假设这是您的列表列表:

passwords = [["yahoo","XqffoZeo"],["google","CoIushujSetu"], ['StackOverflow','uesodsiom'], ['Facebook', 'sdhf9wk']]

You can iterate through a list like this:您可以遍历这样的列表:

for lst in passwords:
        print(lst)
['yahoo', 'XqffoZeo']
['google', 'CoIushujSetu']
['StackOverflow', 'uesodsiom']
['Facebook', 'sdhf9wk']

You did not need range or len in this case, although they could be useful in other cases.在这种情况下,您不需要 range 或 len,尽管它们在其他情况下可能很有用。

What if you only wanted a single element of the nested list that is your website?如果您只想要嵌套列表中的一个元素,即您的网站,该怎么办? Then you could just index:然后你可以索引:

for lst in passwords:
        print(lst[0])

yahoo
google
StackOverflow
Facebook

If you wanted the passwords, use lst[1].如果需要密码,请使用 lst[1]。 The indexing starts from 0.索引从 0 开始。

Note the structure of your list should remain the same: first element is the website name and the second element is the password.请注意,您的列表结构应保持不变:第一个元素是网站名称,第二个元素是密码。

Now, from my understanding you want to check if given a website, it matches a password现在,根据我的理解,您想检查是否给定了一个网站,它是否与密码匹配

First method:第一种方法:

website = 'Facebook'
passwordToLookup = 'sdhf9wk'

for lst in passwords:
    if lst[0] == website and lst[1] == passwordToLookup:
        print(True)
True

Second Method where you are unpacking each element in the list of passwords:解压密码列表中的每个元素的第二种方法:

for websi, password in passwords:
    if websi == website and password == passwordToLookup:
        print(True)

True

Unpacking example:开箱示例:

x, y =  [1, 2]
print(x)
1
print(y)
2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM