[英]How to split parts of a string in a list based on predefined part of in the string
Plese help me with below question请帮我解决以下问题
sample_list = ['Ironman.mdc.googlesuite.net', 'Hulk.nba.abc.googlekey.net', 'Thor.web.gg.hh.googlestream.net', 'Antman.googled.net', 'Loki.media.googlesuite.net','Captain.googlekey.net']
I would want everything preceeding 'googlesuite.net', 'googlekey.net','googlestream.net' and 'googled.net' in list1 and corresponding prefixes in another list as:我希望 list1 中 'googlesuite.net'、'googlekey.net'、'googlestream.net' 和 'googled.net' 之前的所有内容以及另一个列表中的相应前缀为:
result_list1=['Ironman.mdc', 'Hulk.nba.abc', 'Thor.web.gg.hh', 'Antman', 'Loki.media', 'Captain']
result_list2=['googlesuite.net', 'googlekey.net', 'googlestream.net', 'googled.net', 'googlesuite.net', 'googlekey.net']
result_list1=['Ironman.mdc', 'Hulk.nba.abc', 'Thor.web.gg.hh', 'Antman', 'Loki.media', 'Captain']
result_list2=['googlesuite.net', 'googlekey.net', 'googlestream.net', 'googled.net', 'googlesuite.net', 'googlekey.net']
You can always split each string in the list with '.'您始终可以使用 '.' 拆分列表中的每个字符串。 and get a new list.
并获得一个新列表。 In this case, if you are only interested in the first split, you should use the second argument in the split method (which tells the occurrence):
在这种情况下,如果您只对第一个拆分感兴趣,则应该使用 split 方法中的第二个参数(它告诉发生的情况):
first_list =[x.split('.')[0] for x in sample_list]
For the second list:对于第二个列表:
second_list =[x.split('.',1)[1] for x in sample_list]
A better way is to iterate only once through the sample_list and get both the lists.更好的方法是仅通过 sample_list 迭代一次并获取两个列表。 As shown below:
如下所示:
first_list, second_list = zip(* [x.split('.',1) for x in sample_list])
Using a list comprehension along with split
:使用列表理解和
split
:
sample_list = ['Ironman.googlesuite.net', 'Hulk.googlekey.net',
'Thor.googlestream.net', 'Antman.googled.net', 'Loki.googlesuite.net',
'Captain.googlekey.net']
result_list1 = [i.split('.')[0] for i in sample_list]
print(result_list1)
This prints:这打印:
['Ironman', 'Hulk', 'Thor', 'Antman', 'Loki', 'Captain']
This strategy is to retain, for each input domain, just the component up to, but not including, the first dot separator.该策略是为每个输入域保留直到但不包括第一个点分隔符的组件。 For the second list, we can use
re.sub
here:对于第二个列表,我们可以在这里使用
re.sub
:
result_list2 = [re.sub(r'^[^.]+\.', '', i) for i in sample_list]
print(result_list2)
This prints:这打印:
['googlesuite.net', 'googlekey.net', 'googlestream.net', 'googled.net',
'googlesuite.net', 'googlekey.net']
thank you for the answers, it does help but what if I have list like this:谢谢你的回答,它确实有帮助,但如果我有这样的列表怎么办:
sample_list = ['Ironman.mdc.googlesuite.net', 'Hulk.nba.abc.googlekey.net', 'Thor.web.gg.hh.googlestream.net', 'Antman.googled.net', 'Loki.media.googlesuite.net','Captain.googlekey.net']
I would want everything preceeding 'googlesuite.net', 'googlekey.net','googlestream.net' and 'googled.net' in list1 and corresponding prefixes in another list as:我希望 list1 中 'googlesuite.net'、'googlekey.net'、'googlestream.net' 和 'googled.net' 之前的所有内容以及另一个列表中的相应前缀为:
result_list1=['Ironman.mdc', 'Hulk.nba.abc', 'Thor.web.gg.hh', 'Antman', 'Loki.media', 'Captain']
result_list2=['googlesuite.net', 'googlekey.net', 'googlestream.net', 'googled.net', 'googlesuite.net', 'googlekey.net']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.