Python Forum
Compile list of dictianories out of another list of dictianories by certain keys
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Compile list of dictianories out of another list of dictianories by certain keys
#3
def selectFromDictianories(
    dictianories
  , compareKeys
  , compareValues
  , returnKeys
  , booleanPop = False
  , booleanVerbose = True
):
  dictianoriesToReturn = []
  for intDic, currentDictianory in enumerate(dictianories):
    if booleanVerbose:
      print(
          "Durchsuche:", dictianories, "\n"
        , "Vergleiche Schlüssel:", ", ".join(compareKeys)
        , "von:", currentDictianory, "Soll:", compareValues
      )
    try:
      listComparison = [
        currentDictianory[key] for key in compareKeys
      ]
    except KeyError as errorKeyMissing:
      if booleanVerbose:
        print(
            "Warnung! Diese Funktion nimmt an, dass alle Schlüssel"
          , compareKeys
          , "im zu durchsuchenden Wörterbuch"
          , currentDictianory
          , "tatsächlich vorkommt, was nicht der Fall ist.\n"
          , "Es fehlt:", errorKeyMissing
        )
      listComparison = []
    if booleanVerbose:
      print("Vergleiche:", listComparison, "mit:", compareValues)
    # TODO: Kann es passieren, dass
    # ENTEWEDER listComparison und compareValues zufällig gleich sind
    # ODER sie prinzipiell gleich sind, aber die Reihenfolge nicht stimmt.
    if listComparison != [] and listComparison == compareValues:
      print("Treffer für:", currentDictianory)
      dictianoryToReturn = {}
      for k in returnKeys:
        dictianoryToReturn[k] = currentDictianory[k]
      dictianoriesToReturn.append(dictianoryToReturn)
      if booleanPop:
        dictianories.pop(intDic)
  return dictianoriesToReturn
Reply


Messages In This Thread
RE: Compile list of dictianories out of another list of dictianories by certain keys - by CatorCanulis - Jun-05-2021, 04:11 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Strange behavior list of list mmhmjanssen 2 167 May-01-2024, 07:16 AM
Last Post: Gribouillis
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,222 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,576 May-01-2023, 09:06 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 942 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 1,876 Oct-26-2022, 04:03 PM
Last Post: deanhystad
Question Keyword to build list from list of objects? pfdjhfuys 3 1,595 Aug-06-2022, 11:39 PM
Last Post: Pedroski55
  Split a number to list and list sum must be number sunny9495 5 2,331 Apr-28-2022, 09:32 AM
Last Post: Dexty
  Updating nested dict list keys tbaror 2 1,299 Feb-09-2022, 09:37 AM
Last Post: tbaror
  How to check if a list is in another list finndude 4 1,862 Jan-17-2022, 05:04 PM
Last Post: bowlofred
  Different out when using conda list and pip list Led_Zeppelin 1 4,080 Jan-14-2022, 09:30 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020