拆分列表中的值并创建 2 个新列表
split values in list and create 2 new list
我有列表 = ['https://pagerduty.com/teams/PRJMI0O', 'https://pagerduty.com/services/PRWZID9', 'https://pagerduty.com/service-directory/PJNKJWD'].
我想为 /teams url 和 /service* url 提取单独的列表。并将 -1 索引添加到新列表中。
示例:
INPUT list:
list = ['https://pagerduty.com/teams/PRJMI0O', 'https://pagerduty.com/services/PRWZID9', 'https://pagerduty.com/service-directory/PJNKJWD']
Expected OUTPUT
new_teams = ["PRJMI0O"]
new_service = ["PRWZID9", "PJNKJWD"]
please advice the logic.
提示:您可以使用 split function.
>>> 'https://pagerduty.com/teams/PRJMI0O'.split('/')
['https:', '', 'pagerduty.com', 'teams', 'PRJMI0O']
答案:让 urls 成为您关注的列表。
new_teams = []
new_services = []
for url in urls:
urlSplit = url.split('/')
if 'teams' in urlSplit:
new_teams.append(urlSplit[-1])
if 'services' in urlSplit:
new_services.append(urlSplit[-1])
我有列表 = ['https://pagerduty.com/teams/PRJMI0O', 'https://pagerduty.com/services/PRWZID9', 'https://pagerduty.com/service-directory/PJNKJWD'].
我想为 /teams url 和 /service* url 提取单独的列表。并将 -1 索引添加到新列表中。
示例:
INPUT list:
list = ['https://pagerduty.com/teams/PRJMI0O', 'https://pagerduty.com/services/PRWZID9', 'https://pagerduty.com/service-directory/PJNKJWD']
Expected OUTPUT
new_teams = ["PRJMI0O"]
new_service = ["PRWZID9", "PJNKJWD"]
please advice the logic.
提示:您可以使用 split function.
>>> 'https://pagerduty.com/teams/PRJMI0O'.split('/')
['https:', '', 'pagerduty.com', 'teams', 'PRJMI0O']
答案:让 urls 成为您关注的列表。
new_teams = []
new_services = []
for url in urls:
urlSplit = url.split('/')
if 'teams' in urlSplit:
new_teams.append(urlSplit[-1])
if 'services' in urlSplit:
new_services.append(urlSplit[-1])