第n条评论被删除,第n个用户名在其他评论中被检查,如果第n个用户名不在所有评论中(第n条评论已被删除),它将在warn_usernames中传递/附加,之后,第n条评论获得再次在评论中添加。循环继续
usernames = ["a", "b", "c"]
warn_usernames = []
comments = [["b"], ["a", "c"], ["a", "b"]]
n = 0
while n < len(usernames):
commente = comments[n]
print(commente)
usernamee = usernames[n]
print(usernamee)
comments.pop(n)
if str(usernamee) not in str(comments):
warn_usernames.append(usernamee)
comments.insert(n, commente)
n = n + 1
print(warn_usernames)
结果我得到了
[]
结果我希望得到
[c]
提前致谢,我希望你能得到我的问题。
答案 0 :(得分:0)
我认为你可以这样做:
usernames = ["a", "b", "c"]
warn_usernames = []
comments = [["b"], ["a", "c"], ["a", "b"]]
# use a loop for with enumerate on usernames to get a username and its index
for n, username in enumerate(usernames):
#loop on all others comments than the nth
for comments_to_check in comments[:n]+comments[n+1:]:
#check the username is in comments_to_check and
if username not in comments_to_check:
# append to warn_usernames if not
warn_usernames.append(username)
# stop the loop on comments to go to the next loop on usernames
# to prevent multiple time the same username to be appened
break
print warn_usernames
您将获得预期的输出