繁体   English   中英

如何在字典中查找/搜索列表中的值

[英]how to find/search the values in the list that from dictionary

laptops = {'OIS12345':['192.168.1.10', 'Windows 8', 'i7', '8GB','2.7GHz', '700GB', '15-Dec-14'],
            'OIS23415':['192.168.1.11', 'Windows 8', 'i7', '8GB','2.7GHz', '700GB', '15-Dec-14'],
            'OIS23451':['192.168.1.18', 'Windows 7', 'i5', '4GB','2.6GHz', '600GB', '13-Jan-14']}


names = laptops.keys()

attrs = laptops.values()

如何检查RAM是否小于8GB并打印计算机名称(键)?

我试过了:

for i in attrs:

    match=re.search('(\d)GB',i)

    if match: 
        if match.group(2)<8:
            print names

我想这就是你想要的:

for name, specs in laptops.items():
    if int(specs[3][:-2]) < 8:
        print(name)

使用Python字典理解

print {k for k, v in laptops.iteritems() if int(v[3][:1]) < 8}

您可以尝试这种方式,可以使用任意数量和GB的限制。

for key, value in laptops.items():
    number = value[3]
    end = number.index("GB")
    if int(number[:end]) < 8:
        print(key)

输出:

OIS23451

您还可以使用列表理解功能打印出计算机密钥:

print("".join([k for k, v in laptops.items() if int(v[3][:v[3].index("GB")]) < 8]))

暂无
暂无

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

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