在 3 个列表列表中搜索
Search in 3 lists of lists
我有 3 个列表
user_list = ['Administrator', 'Deepak', 'Bhamar','Akash', 'Harshit']
group_members = [['Harshit', 'randam value', 40], ['Harshit', 'randam value', 40], ['Rohit', 'randam value', 40], ['Bhamar', 'randam value', 40], ['Akash', 'randam value', 40], ['Bhamar', 'randam value', 40], ['Bhamar', 'randam value', 40], ['Akash', 'randam value', 40], ['Harshit', 'randam value', 40]]
project_members = [['Administrator', 'random value', 50], ['Administrator', 'random value', 50], ['Administrator', 'random value', 50], ['Administrator', 'random value', 50], ['Administrator', 'random value', 50], ['Rakesh', 'random value', 30], ['Priyanka', 'random value', 30], ['Sapna', 'random value', 40], ['Amit', 'random value', 40]]
现在,我想在 "project_members" & "group_members" 和 return 上搜索列表 "user_list" 中的名称对应的值。这些列表是通过 api 从 gitlab 获取的,我正在尝试获取用户项目和组的详细信息,我对 python.
完全陌生
我无法理解实现这一点的逻辑。
json 中的预期输出:
Administrator:{
project:
[]
groups:{
[['Administrator', 'random value', 50], ['Administrator', 'random value', 50], ['Administrator', 'random value', 50], ['Administrator', 'random value', 50], ['Administrator', 'random value', 50], ['Rakesh', 'random value', 30]
Bhamar:{
projects:
[['Bhamar', 'randam value', 40],['Bhamar', 'randam value', 40],['Bhamar', 'randam value', 40]]
groups:
[]
代码:
import gitlab
import csv
import os
gl = gitlab.Gitlab('git.gltlab.com', private_token='private_token')
with open("group.csv",'w+') as f:
pass
with open("project.csv",'w+') as f:
pass
group_list = []
project_list = []
group_members = []
project_members = []
user_list = []
def logging(file,data):
with open(os.path.join(file+".csv"),'a') as f:
wr = csv.writer(f, quoting=csv.QUOTE_ALL)
wr.writerow(data)
def main():
for page_no in range(1,4):
groups = gl.groups.list(per_page=5,page=page_no)
for id in groups:
group_list.append(id.name)
projects = gl.projects.list(per_page=9,page=page_no)
for project in projects:
project_list.append(project.id)
users = gl.users.list(per_page=1,page=page_no)
for user in users:
user_list.append(user)
get_group_members()
get_project_members()
def get_group_members():
for group in group_list:
group_1 = gl.groups.get(group)
grp_members = group_1.members.list()
for name in grp_members:
data = [str(name.name),str(group),name.access_level]
logging(file="group",data=data)
group_members.append(data)
def get_project_members():
for project in project_list:
project_1 = gl.projects.get(project)
prjt_memebrs = project_1.members.list()
for name in prjt_memebrs:
test = [str(name.name),str(project_1.name_with_namespace),name.access_level]
logging(file="project",data=test)
project_members.append(test)
if __name__ == "__main__":
main()
以下代码应该能够生成您需要的json。
import json
user_list = ['Administrator', 'Deepak', 'Bhamar','Akash', 'Harshit']
group_members = [['Harshit', 'randam value', 40], ['Harshit', 'randam value', 40], ['Rohit', 'randam value', 40], ['Bhamar', 'randam value', 40], ['Akash', 'randam value', 40], ['Bhamar', 'randam value', 40], ['Bhamar', 'randam value', 40], ['Akash', 'randam value', 40], ['Harshit', 'randam value', 40]]
project_members = [['Administrator', 'random value', 50], ['Administrator', 'random value', 50], ['Administrator', 'random value', 50], ['Administrator', 'random value', 50], ['Administrator', 'random value', 50], ['Rakesh', 'random value', 30], ['Priyanka', 'random value', 30], ['Sapna', 'random value', 40], ['Amit', 'random value', 40]]
data = {}
for user in user_list:
data[user] = {}
data[user]["groups"] = []
for group in group_members:
if user in group:
data[user]["groups"].append(group)
data[user]["project"] = []
for project in project_members:
if user in project:
data[user]["project"].append(project)
with open('data.json', 'w') as f:
json.dump(data, f)
它循环遍历列表,并创建一个字典,其键是用户,其值是另一个字典。嵌套字典有键组和项目,其值是其他用户参与的列表。
我有 3 个列表
user_list = ['Administrator', 'Deepak', 'Bhamar','Akash', 'Harshit']
group_members = [['Harshit', 'randam value', 40], ['Harshit', 'randam value', 40], ['Rohit', 'randam value', 40], ['Bhamar', 'randam value', 40], ['Akash', 'randam value', 40], ['Bhamar', 'randam value', 40], ['Bhamar', 'randam value', 40], ['Akash', 'randam value', 40], ['Harshit', 'randam value', 40]]
project_members = [['Administrator', 'random value', 50], ['Administrator', 'random value', 50], ['Administrator', 'random value', 50], ['Administrator', 'random value', 50], ['Administrator', 'random value', 50], ['Rakesh', 'random value', 30], ['Priyanka', 'random value', 30], ['Sapna', 'random value', 40], ['Amit', 'random value', 40]]
现在,我想在 "project_members" & "group_members" 和 return 上搜索列表 "user_list" 中的名称对应的值。这些列表是通过 api 从 gitlab 获取的,我正在尝试获取用户项目和组的详细信息,我对 python.
完全陌生我无法理解实现这一点的逻辑。
json 中的预期输出:
Administrator:{
project:
[]
groups:{
[['Administrator', 'random value', 50], ['Administrator', 'random value', 50], ['Administrator', 'random value', 50], ['Administrator', 'random value', 50], ['Administrator', 'random value', 50], ['Rakesh', 'random value', 30]
Bhamar:{
projects:
[['Bhamar', 'randam value', 40],['Bhamar', 'randam value', 40],['Bhamar', 'randam value', 40]]
groups:
[]
代码:
import gitlab
import csv
import os
gl = gitlab.Gitlab('git.gltlab.com', private_token='private_token')
with open("group.csv",'w+') as f:
pass
with open("project.csv",'w+') as f:
pass
group_list = []
project_list = []
group_members = []
project_members = []
user_list = []
def logging(file,data):
with open(os.path.join(file+".csv"),'a') as f:
wr = csv.writer(f, quoting=csv.QUOTE_ALL)
wr.writerow(data)
def main():
for page_no in range(1,4):
groups = gl.groups.list(per_page=5,page=page_no)
for id in groups:
group_list.append(id.name)
projects = gl.projects.list(per_page=9,page=page_no)
for project in projects:
project_list.append(project.id)
users = gl.users.list(per_page=1,page=page_no)
for user in users:
user_list.append(user)
get_group_members()
get_project_members()
def get_group_members():
for group in group_list:
group_1 = gl.groups.get(group)
grp_members = group_1.members.list()
for name in grp_members:
data = [str(name.name),str(group),name.access_level]
logging(file="group",data=data)
group_members.append(data)
def get_project_members():
for project in project_list:
project_1 = gl.projects.get(project)
prjt_memebrs = project_1.members.list()
for name in prjt_memebrs:
test = [str(name.name),str(project_1.name_with_namespace),name.access_level]
logging(file="project",data=test)
project_members.append(test)
if __name__ == "__main__":
main()
以下代码应该能够生成您需要的json。
import json
user_list = ['Administrator', 'Deepak', 'Bhamar','Akash', 'Harshit']
group_members = [['Harshit', 'randam value', 40], ['Harshit', 'randam value', 40], ['Rohit', 'randam value', 40], ['Bhamar', 'randam value', 40], ['Akash', 'randam value', 40], ['Bhamar', 'randam value', 40], ['Bhamar', 'randam value', 40], ['Akash', 'randam value', 40], ['Harshit', 'randam value', 40]]
project_members = [['Administrator', 'random value', 50], ['Administrator', 'random value', 50], ['Administrator', 'random value', 50], ['Administrator', 'random value', 50], ['Administrator', 'random value', 50], ['Rakesh', 'random value', 30], ['Priyanka', 'random value', 30], ['Sapna', 'random value', 40], ['Amit', 'random value', 40]]
data = {}
for user in user_list:
data[user] = {}
data[user]["groups"] = []
for group in group_members:
if user in group:
data[user]["groups"].append(group)
data[user]["project"] = []
for project in project_members:
if user in project:
data[user]["project"].append(project)
with open('data.json', 'w') as f:
json.dump(data, f)
它循环遍历列表,并创建一个字典,其键是用户,其值是另一个字典。嵌套字典有键组和项目,其值是其他用户参与的列表。