2016-06-25 21 views
2
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 
+0

爲什麼不使用嵌套類型的字典,所以你可以通過按鍵做查詢? –

+0

這是我的練習題〜*^_^* so question can not edit〜:p – user3715701

+0

所以你得到這樣的數據? –

回答

3

我想這是你想要什麼:

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

使用Python字典解析

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

這是最pythonic的解決方案。 'set'標識符可以從輸出中刪除:http://stackoverflow.com/questions/15328788/removing-set-identifier-when-printing-sets-in-python – noumenal

0

你可以試試這個方式,與任意數量的工作和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])) 
相關問題