简体   繁体   English

如何根据字符串中的预定义部分拆分列表中的字符串部分

[英]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.

相关问题 如何将字符串拆分为不同长度的预定义子字符串列表? - How to split a string into a list of predefined substrings of different lengths? 根据预定义的字符类型分割字符串 - Split string based on predefined character types 将带有预定义部分的字符串 output 拆分为字典的最佳方法 - Best way to split a string output with predefined parts into a dictionary 如何拆分属于 python 中的元组列表的字符串 - How to split string that is a part of a tuple list in python 如何将字符串分成两部分? - How to split a string into two parts? 如何将字符串分成多个部分? - How to split a string into multiple parts? for 循环,拆分字符串,将字符串的一部分保存到新列表,IndexError:列表索引超出范围 - 适用于字符串的一部分而不是另一部分 - for loop, split string, save parts of string to new list, IndexError: list index out of range - works for one part of string and not the other 拆分字符串并在运行列表中追加部分[Python] - Split string and append parts in running list [Python] 如何根据列表中的元素拆分字符串 - How to split a string based off elements in a list 如何根据分隔符拆分 python 中的字符串,分隔符作为其中一个块的一部分? - How to split a string in python based on separator with separator as a part of one of the chunks?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM