About: http://dev.restore.ovi.cnr.it:8890/about/id/entity/https/stackoverflow.com/q/38987#id0x7fe83024ba38   Goto Sponge  NotDistinct  Permalink

An Entity of Type : https://schema.org/Answer, within Data Space : dev.restore.ovi.cnr.it:8890 associated with source document(s)

AttributesValues
type
described by
md:url
md:author
md:dateCreated
  • 2008-09-02 07:49:27Z
  • 2008-09-04 19:08:25Z
md:upvoteCount
  • 158
  • 101
md:commentCount
  • 2
  • 1
md:text
  • I wanted something similar, but with the ability to specify how the values on duplicate keys were merged, so I hacked this out (but did not heavily test it). Obviously this is not a single expression, but it is a single function call. def merge(d1, d2, merge_fn=lambda x,y:y): """ Merges two dictionaries, non-destructively, combining values on duplicate keys as defined by the optional merge function. The default behavior replaces the values in d1 with corresponding values in d2. (There is no other generally applicable merge strategy, but often you'll have homogeneous types in your dicts, so specifying a merge technique can be valuable.) Examples: >>> d1 {'a': 1, 'c': 3, 'b': 2} >>> merge(d1, d1) {'a': 1, 'c': 3, 'b': 2} >>> merge(d1, d1, lambda x,y: x+y) {'a': 2, 'c': 6, 'b': 4} """ result = dict(d1) for k,v in d2.iteritems(): if k in result: result[k] = merge_fn(result[k], v) else: result[k] = v return result
  • x = {'a':1, 'b': 2} y = {'b':10, 'c': 11} z = dict(x.items() + y.items()) print z For items with keys in both dictionaries ('b'), you can control which one ends up in the output by putting that one last. UPDATE For Python 3.x use `dict(list(x.items()) + list(y.items()))`
is md:suggestedAnswer of
Faceted Search & Find service v1.16.102 as of Feb 08 2021


Alternative Linked Data Documents: Sponger | iSPARQL | ODE     Content Formats:       RDF       ODATA       Microdata      About   
This material is Open Knowledge   W3C Semantic Web Technology [RDF Data] Valid XHTML + RDFa
OpenLink Virtuoso version 07.20.3231 as of Jan 19 2021, on Linux (x86_64-generic_glibc25-linux-gnu), Single-Server Edition (7 GB total memory)
Data on this page belongs to its respective rights holders.
Virtuoso Faceted Browser Copyright © 2009-2025 OpenLink Software