USIGN PYTHON:提供公司管理字典,生成一个函数,使用递归以获取打印输出如下

发布于 2025-01-21 21:39:48 字数 631 浏览 0 评论 0原文

Python

Company = {“ name”:“ George”,“ Manages”:[{“ name”:“ James”,“ Manages”:[{“ name”:“ Jill”},{“ name”:“ Jenny”} ]},{“ name”:“ jamila”,“ manages”:[{“ name”:“ jewel”},{“ name”:“ jasmine”},{“ name”:“ jeremey”}]}}}}

乔治经营一家公司。他管理詹姆斯和贾米拉,他们每个人都有一个小团队要管理。詹姆斯的团队是吉尔和珍妮。在贾米拉(Jamila)的团队中,有珠宝,茉莉和杰里米。

结果应该是:

乔治在1级 詹姆斯在2级 吉尔处于3级 珍妮处于3级 贾米拉(Jamila)处于2级 珠宝处于3级 茉莉在3级 耶利米(Jeremey)处于3级

在此处输入图像描述

在此处输入图像描述

Python

company = {"name":"George", "manages":[{"name":"James", "manages":[{"name":"Jill"},{"name":"Jenny"}]}, {"name":"Jamila", "manages":[{"name":"Jewel"}, {"name":"Jasmine"}, {"name":"Jeremey"}]}]}

George runs a company. He manages James and Jamila, who each have a small team to manage. In James' team are Jill and Jenny. In Jamila's team are Jewel, Jasmine and Jeremy.

Result should be:

George is at level 1
James is at level 2
Jill is at level 3
Jenny is at level 3
Jamila is at level 2
Jewel is at level 3
Jasmine is at level 3
Jeremey is at level 3

enter image description here

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

踏月而来 2025-01-28 21:39:48
function* levels({ name, manages = [] }, level = 1) {
  yield [name, level]
  for (const m of manages)
    yield *levels(m, level + 1)
}

const company = {"name":"George", "manages":[{"name":"James", "manages":[{"name":"Jill"},{"name":"Jenny"}]}, {"name":"Jamila", "manages":[{"name":"Jewel"}, {"name":"Jasmine"}, {"name":"Jeremey"}]}]}

for (const [name, level] of levels(company))
  console.log(`${name} is at level ${level}`)

George is at level 1
James is at level 2
Jill is at level 3
Jenny is at level 3
Jamila is at level 2
Jewel is at level 3
Jasmine is at level 3
Jeremey is at level 3

对于Python,答案几乎相同

from json import loads

def levels(node, level = 1):
  yield (node['name'], level)
  try:
    for m in node['manages']:
      yield from levels(m, level + 1)
  except KeyError:
    return

company = loads('{"name":"George", "manages":[{"name":"James", "manages":[{"name":"Jill"},{"name":"Jenny"}]}, {"name":"Jamila", "manages":[{"name":"Jewel"}, {"name":"Jasmine"}, {"name":"Jeremey"}]}]}')

for (name, level) in levels(company):
  print(f"{name} is at level {level}")
George is at level 1
James is at level 2
Jill is at level 3
Jenny is at level 3
Jamila is at level 2
Jewel is at level 3
Jasmine is at level 3
Jeremey is at level 3

function* levels({ name, manages = [] }, level = 1) {
  yield [name, level]
  for (const m of manages)
    yield *levels(m, level + 1)
}

const company = {"name":"George", "manages":[{"name":"James", "manages":[{"name":"Jill"},{"name":"Jenny"}]}, {"name":"Jamila", "manages":[{"name":"Jewel"}, {"name":"Jasmine"}, {"name":"Jeremey"}]}]}

for (const [name, level] of levels(company))
  console.log(`${name} is at level ${level}`)

George is at level 1
James is at level 2
Jill is at level 3
Jenny is at level 3
Jamila is at level 2
Jewel is at level 3
Jasmine is at level 3
Jeremey is at level 3

For python the answer is nearly identical

from json import loads

def levels(node, level = 1):
  yield (node['name'], level)
  try:
    for m in node['manages']:
      yield from levels(m, level + 1)
  except KeyError:
    return

company = loads('{"name":"George", "manages":[{"name":"James", "manages":[{"name":"Jill"},{"name":"Jenny"}]}, {"name":"Jamila", "manages":[{"name":"Jewel"}, {"name":"Jasmine"}, {"name":"Jeremey"}]}]}')

for (name, level) in levels(company):
  print(f"{name} is at level {level}")
George is at level 1
James is at level 2
Jill is at level 3
Jenny is at level 3
Jamila is at level 2
Jewel is at level 3
Jasmine is at level 3
Jeremey is at level 3
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文