我有一些UserInvites,其中邀请包含from
和to
用户属性。
,例如,
UserInvite1.from = User1
UserInvite1.to = User2
UserInvite1b.from = User1
UserInvite1b.to = User4
UserInvite2.from = User2
UserInvite2.to = User3
UserInvite3.from = User2
UserInvite3.to = User5
因此,User1邀请了User2和User4; User2邀请了User3和User5。
鉴于这些邀请的列表,例如[UserInvite1, UserInvite2, ... ]
,或其他迭代方法(?),如何生成表示这些邀请的“分层”(嵌套)列表邀请?
例如,从“root”User1开始,我想要一个嵌套列表:
>>> make_nest_list_from_invites([invites])
[User1, [User2, [User3, User5], User4]]
如果您熟悉Django,我正试图从我的“邀请”中获取可以提供给Django模板标签unordered_list
的层次结构。显然,这就像树遍历,但此刻我很难过。我尝试了一些递归的东西,但最终还是在一些可以放弃东西的地方进行了额外的嵌套。
def tree_from_here(user):
children = get_children(user)
if children:
return [user, [tree_from_here(c) for c in children]]
else:
return user
给出了:
>>> tree_from_here(User1)
[<User: 1>, [[<User: 2>, [<User: 3>, <User: 5>]], <User: 4>]]
对于我当前的用户组来说几乎是正确的,除了,嵌套在第二个元素中过深。
我正在努力:
[<User: 1>, [<User: 2>, [<User: 3>, <User: 5>], <User: 4>]]
我觉得它正在盯着我,但我不知道如何在此刻回归正确的事情。
答案 0 :(得分:1)
我很难理解你对嵌套结构的这种重新定位的想法,但经过大量的思考和编写代码后我相信我理解,所以我想这就是你所追求的 - 代码中的描述和在线演示:https://repl.it/repls/EarlyWeeklyThings - 这是一个很好的挑战,thx:)
from pprint import pprint
# dummy user class
class User(object):
def __init__(self, user_id):
self.user_id = user_id
def __repr__(self):
return "User{}".format(self.user_id)
# dummy invite class
class UserInvite(object):
def __init__(self, from_user, to_user):
self.from_user = from_user
self.to_user = to_user
def __repr__(self):
return "From {} to {}".format(self.from_user, self.to_user)
# create 10 dummy users to create invites
users = [User(user_id) for user_id in range(1,11)]
# use natural numbers to reflect invites
# adjust in list comprehension
invite_map = (
(1, 3), (1, 5),
(2, 4), (2, 6),
(3, 7), (3, 8),
(4, 9),
(9, 10)
)
# create invitations based on the invite_map, fix natural numbers
example_invites = [
UserInvite(users[inviter-1], users[invitee-1]) for inviter, invitee in invite_map
]
pprint(example_invites)
# =>
# [From User1 to User3,
# From User1 to User5,
# From User2 to User4,
# From User2 to User6,
# From User3 to User7,
# From User3 to User8,
# From User4 to User9,
# From User9 to User10]
def get_nested_invites(invites, invited_by=None):
result = []
if not invited_by:
# Assume that initial inviters were not invited by anyone
# Use set comprehensions to avoid duplicates and for performance
invitees = {invite.to_user for invite in invites}
inviters = {invite.from_user for invite in invites if invite.from_user not in invitees}
else:
# Get the next potential inviters given their inviter
# Use set comprehension to avoid duplicates and for performance
inviters = {invite.to_user for invite in invites if invite.from_user == invited_by}
for inviter in inviters:
# Add the invited user/potential inviter
result.append(inviter)
# Let's get nesty
invitees = get_nested_invites(invites, inviter)
if invitees:
result.append(invitees)
return result
pprint(get_nested_invites(example_invites))
# =>
# [User1,
# [User3, [User7, User8], User5],
# User2,
# [User6, User4, [User9, [User10]]]]
pprint(get_nested_invites(example_invites, users[1]))
# =>
# [User6, User4, [User9, [User10]]]