• Places
    • Home
    • Graphs
    • Prefixes
  • Admin
    • Users
    • Settings
    • Plugins
    • Statistics
  • CPACK
    • Home
    • List packs
    • Submit pack
  • Repository
    • Load local file
    • Load from HTTP
    • Load from library
    • Remove triples
    • Clear repository
  • Query
    • YASGUI SPARQL Editor
    • Simple Form
    • SWISH Prolog shell
  • Help
    • Documentation
    • Tutorial
    • Roadmap
    • HTTP Services
  • Login

A.34 library(pairs): Operations on key-value lists
All Application Manual Name SummaryHelp

  • Documentation
    • Reference manual
      • The SWI-Prolog library
        • library(pairs): Operations on key-value lists
          • pairs_keys_values/3
          • pairs_values/2
          • pairs_keys/2
          • group_pairs_by_key/2
          • transpose_pairs/2
          • map_list_to_pairs/3
    • Packages
Availability::- use_module(library(pairs)).(can be autoloaded)
Source[det]group_pairs_by_key(+Pairs, -Joined:list(Key-Values))
Group values with equivalent (==/2) consecutive keys. For example:
?- group_pairs_by_key([a-2, a-1, b-4, a-3], X).

X = [a-[2,1], b-[4], a-[3]]

Sorting the list of pairs before grouping can be used to group all values associated with a key. For example, finding all values associated with the largest key:

?- sort(1, @>=, [a-1, b-2, c-3, a-4, a-5, c-6], Ps),
   group_pairs_by_key(Ps, [K-Vs|_]).
K = c,
Vs = [3, 6].

In this example, sorting by key only (first argument of sort/4 is 1) ensures that the order of the values in the original list of pairs is maintained.

Pairs Key-Value list
Joined List of Key-Group, where Group is the list of Values associated with equivalent consecutive Keys in the same order as they appear in Pairs.
ClioPatria (version V3.1.1-51-ga0b30a5)