简体   繁体   中英

Trying to filter a dictionary using list comprehension

Using a single statement, print a dictionary containing only the atomic symbols and their corresponding weights for those elements in wts (my dictionary) which have only a single letter in their atomic symbols. Ie, include 'H' but omit 'He'. My dictionary is set up as {'H':'1.00794','He':'4.002602','Li':'6.941','Be':'9.012182','B':'10.811','C':'12.0107','N':'14.0067','O':'15.9994'}

[for element in wts if len(element) == 1]

I was thinking a list comprehension would would work but, how would i have it look only at the element symbol. This returns an error of :

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_45.py", line 10, in <module>
    exec compile(u"print _support_.syseval(python, u'[for element in wts if len(element) == 1]', __SAGE_TMP_DIR__)" + '\n', '', 'single')
  File "", line 1, in <module>

  File "/sagenb/sage_install/sage-5.3-sage.math.washington.edu-x86_64-Linux/devel/sagenb-git/sagenb/misc/support.py", line 487, in syseval
    return system.eval(cmd, sage_globals, locals = sage_globals)
  File "/sagenb/sage_install/sage-5.3-sage.math.washington.edu-x86_64-Linux/local/lib/python2.7/site-packages/sage/misc/python.py", line 53, in eval
    eval(compile(s, '', 'exec'), globals, globals)
  File "", line 3
    [for element in wts if len(element) == 1]
       ^
SyntaxError: invalid syntax

You have a syntax error (as noted by Python). Use:

[element for element in wts if len(element) == 1]

A list comprehension must start with an expression before the for . With this syntax you can apply further operations, such as uppercasing for example:

[element.upper() for element in wts if len(element) == 1]

Because you have to repeat the iteration variable name so much, you will often see comprehensions written with short variable names. I might write that using x as:

[x for x in wts if len(x) == 1]

You can use a list-comp:

>>> dts = {'H':'1.00794','He':'4.002602','Li':'6.941','Be':'9.012182','B':'10.811','C':'12.0107','N':'14.0067','O':'15.9994'}
>>> [(el, weight) for el, weight in dts.iteritems() if len(el) == 1]
[('C', '12.0107'), ('B', '10.811'), ('N', '14.0067'), ('H', '1.00794'), ('O', '15.9994')]

Alternatively filter :

>>> filter(lambda (k, v): len(k) == 1, dts.iteritems())
[('C', '12.0107'), ('B', '10.811'), ('N', '14.0067'), ('H', '1.00794'), ('O', '15.9994')]

Since you're being asked to " print a dictionary containing only the atomic symbols and their corresponding weights..." I would think the answer should use a dictionary comprehension as in:

>>> print {el: wt for el, wt in wts.iteritems() if len(el) == 1}
{'H': '1.00794', 'C': '12.0107', 'B': '10.811', 'O': '15.9994', 'N': '14.0067'}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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