簡體   English   中英

Python3中的動態字典遞歸

[英]Dynamic dictionary recursion in Python3

我已經為此工作了很長時間,需要一些幫助。 我正在嘗試使用fakerr創建字典。 如果只是那么簡單。 最初,字典是平的。 鑰匙和物品。 如果鍵的第一個字母為'B'或'M',它將把該字符串轉換為包含5個鍵的字典,並繼續執行此操作,直到找不到以這兩個字母中的任何一個開頭的字母為止。 我知道,現在沒有遞歸發生。 這就是為什么我需要幫助。 我試圖找出如何正確遞歸而不是對深度進行硬編碼。

Starting Dictionary:
{
     "Marcia": "https://www.skinner.biz/categories/tags/terms.htm",
     "Nicholas": "https://scott-tran.com/",
     "Christopher": "https://www.ellis.com/",
     "Paul": "https://lopez.com/index/",
     "Jennifer": "https://www.sosa.com/wp-content/main/login.php"
}

Marcia應該擴展到這個...

Example:
    "Marcia": {
        "Alexander": "http://hicks.net/home.html",
        "Barry": {
            "Jared": "https://www.parker-robinson.com/faq.html",
            "Eddie": "https://www.smith-thomas.com/",
            "Ryan": "https://www.phillips.org/homepage/",
            "Mary": {
               "Alex": "http://www.perry.com/tags/explore/post.htm",
               "Joseph": "https://www.hansen.com/main/list/list/index/",
               "Alicia": "https://www.tran.biz/wp-content/explore/posts/",
               "Anna": "http://lee-mclaughlin.biz/search/login/",
               "Kevin": "https://blake.net/main/index/"
            }
           "Evan": "http://carroll.com/homepage.html"
        }
        "Sharon": "https://www.watson.org/categories/app/login/",
        "Hayley": "https://www.parks.com/",
        "William": "https://www.wyatt-ware.com/"
    }

我的代碼比動態代碼更具手動性,因為我必須明確地知道現在字典中有很多層次,而不是動態地找出它。

這是我可以在2級深度下工作的東西,但是我想找到任何以'B'或'M'開頭並對其進行操作的鍵。

import json
from build_a_dictionary import add_dic
from faker import Faker

dic = add_dic(10)
dic1 = {}
dic2 = {}

def build_dic(dic_len):
  dic1 = {}
  fake = Faker()
  if len(dic1) == 0:
    dic1 = add_dic(dic_len)
  print(json.dumps(dic1, indent=4))
  for k, v in dic1.items():
    dic2[k] = add_dic(dic_len)
    for key in dic2[k].keys():
      for f in key:
        if f == 'B' or f == 'M':
          dic2[k][key] = add_dic(dic_len)
  return dic2

這是我寫的add_dic()中的代碼:

import string, time
from faker import Faker  #had to install with pip
fake = Faker()
dic = {}
dics = {}
key = ""
def add_dic(x):
  dic={}
  start = time.time()
  if x > 690:
    print("Please select a value under 690")
    sys.exit()
  for n in range(x):
    while len(dic) < x:
      key = fake.first_name()
      if key in dic.keys():
        break
      val = fake.uri()
      dic[key] = val
  end = time.time()
  runtime = end - start
  return dic

您只是在做錯事情,如果您希望它是遞歸的,則將該函數編寫為遞歸函數。 本質上,它是字典的自定義(遞歸)映射函數。 至於您期望的字典,我不確定您是如何讓Faker每次確定性地給您同樣的輸出的。 這是隨機的...

注意:對此沒有任何“動態”,它只是一個遞歸映射函數。

from faker import Faker
import pprint

pp = pprint.PrettyPrinter(indent=4)
fake = Faker()

def map_val(key, val):
    if key[0] == 'M' or key[0] == 'B':
        names = [(fake.first_name(), fake.uri()) for i in range(5)]
        return {k : map_val(k, v) for k,v in names}
    else:
        return val

#uncomment below to generate 5 initial names
#names = [(fake.first_name(), fake.uri()) for i in range(5)]
#initial_dict = {k : v for k,v in names}

initial_dict = {
     "Marcia": "https://www.skinner.biz/categories/tags/terms.htm",
     "Nicholas": "https://scott-tran.com/",
     "Christopher": "https://www.ellis.com/",
     "Paul": "https://lopez.com/index/",
     "Jennifer": "https://www.sosa.com/wp-content/main/login.php"
}

dict_2 = {k : map_val(k,v) for k,v in initial_dict.items()}

pp.pprint(dict_2)

輸出:

rpg711$ python nested_dicts.py 

{   'Christopher': 'https://www.ellis.com/',
    'Jennifer': 'https://www.sosa.com/wp-content/main/login.php',
    'Marcia': {   'Chelsea': 'http://francis.org/category.jsp',
                  'Heather': 'http://www.rodgers.com/privacy.jsp',
                  'Jaime': 'https://bates-molina.com/register/',
                  'John': 'http://www.doyle.com/author.htm',
                  'Kimberly': 'https://www.harris.org/homepage/'},
    'Nicholas': 'https://scott-tran.com/',
    'Paul': 'https://lopez.com/index/'
}

謝謝大家的幫助。 我設法弄清楚了。 現在,它可以根據需要構建動態字典或動態json。

import sys, json
from faker import Faker
fake = Faker()

def build_dic(dic_len, dic):
  if isinstance(dic, (list, tuple)):
    dic = dict(dic)
  if isinstance(dic, dict):
    for counter in range(len(dic)):
      for k,v in dic.items():
        if k[0] == 'B' or k[0] == "M":
          update = [(fake.first_name(), fake.uri()) for i in range(5)]
          update = dict(update)
          dic.update({k: update})
  return dic

def walk(dic):
  for key, item in dic.items():
      #print(type(item))
      if isinstance(item, dict):
        build_dic(5, item)
        walk(item)
  return dic

a = build_dic(10, ([(fake.first_name(), fake.uri()) for i in range(10)]))
walk(a)
print(json.dumps(a, indent=4))

遞歸是函數調用自身時; 設計遞歸函數時,牢記退出條件(即遞歸何時停止)很重要。

讓我們考慮一個人為設計的示例,它增加一個數字直到達到某個值:

def increment_until_equal_to_or_greater_than_value(item, target):
    print 'item is', item,
    if item < target:
        print 'incrementing'
        item += 1
        increment_until_equal_to_or_greater_than_value(item, target)
    else:
        print 'returning'
        return item


increment_until_equal_to_or_greater_than_value(1, 10)

和輸出

item is 1 incrementing
item is 2 incrementing
item is 3 incrementing
item is 4 incrementing
item is 5 incrementing
item is 6 incrementing
item is 7 incrementing
item is 8 incrementing
item is 9 incrementing
item is 10 returning

您可以看到我們已經在if語句中定義了遞歸部分,並在else定義了退出條件。

我整理了一個在嵌套數據結構上顯示遞歸函數的代碼段。

它不能完全解決您的問題,因此您可以通過剖析並使其適合您的用例來學習。

# our recursive method
def deep_do_something_if_string(source, something):
    # if source is a dict, iterate through it's values
    if isinstance(source, dict):
        for v in source.itervalues():
            # call this method on the value
            deep_do_something_if_string(v, something)

    # if source is a list, tuple or set, iterate through it's items
    elif isinstance(source, (list, tuple, set)):
        for v in source:
            deep_do_something_if_string(v, something)

    # otherwise do something with the value
    else:
        return something(source)


# a test something to do with the value
def print_it_out(value):
    print value


# an example data structure
some_dict = {
    'a': 'value a',
    'b': [
        {
            'c': 'value c',
            'd': 'value d',
        },
    ],
    'e': {
        'f': 'value f',
        'g': {
            'h': {
                'i': {
                    'j': 'value j'
                }
            }
        }
    }
}

deep_do_something_if_string(some_dict, print_it_out)

和輸出

value a
value c
value d
value j
value f

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM