Click here to Skip to main content
15,443,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please help me with below question in python

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:

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']


What I have tried:

Tried this

result_list1 = [i.split('.')[0] for i in sample_list]
print(result_list1)


Did not get proper result
Posted
Updated 12-Mar-20 19:29pm

I think the only way to separate those parts would be by using a regular expression parser. Take a look at re — Regular expression operations — Python 3.7.7rc1 documentation[^] for some ideas.

[edit]
It can be done with split but it requires a bit of calculation, as shown in the example below. You should be able to adapt this to your own requirements:
Python
def combine(list, lstart, lend):
    for w in list[lstart:lend-1]:
        print(w, end='.')
    for w in list[lend-1:lend]:
        print(w)

list = 'Hulk.nba.abc.googlekey.net'.split('.')
right = len(list) - 2
combine(list, 0, right)
combine(list, right, len(list))

list = 'Antman.googled.net'.split('.')
right = len(list) - 2
combine(list, 0, right)
combine(list, right, len(list))

[/edit]
 
Share this answer
 
v2
Comments
Member 14767732 12-Mar-20 21:55pm     CRLF
Hi Richard Thanks you for the solution.It works brilliantly, but i have a problem in storing the values. I want to append the `(w,end=".")`and `w` to a list. Example, the list would be : `Names1=['Hulk.nba.abc.googlekey','Antman']` I am getting only `['nba.abc']` I am able to do for googlekey,net and googled.net but for the former one. Can you please help.
I got it , I did this:

def combine(temp1, lstart, lend):
    global y
    y=""
    for w in temp1[lstart:lend-1]: 
        y=y+w+"."
    for w in temp1[lend-1:lend]:
        y=y+w

for i in edge_names:
    temp1=(i.split('.'))
    #print(temp1)
    right = len(temp1) - 2
    combine(temp1, 0, right)
    Names1.append(y)
    combine(temp1, right, len(temp1)) 
    names2.append(y)
   
print(names1)
print(names2)


for a list of
['Hulk.nba.abc.googlekey.net']



It segregates the above into :

names1=['Hulk.nba.abc]
names2=['googlekey.net']


Thank you so much for your help. Appreciate it!
 
Share this answer
 
A clever RegEx can simplify your code much more.
Python
temp1=(i.split('.google'))

will return a list of 2 which is: 'Hulk.nba.abc' and 'key.net'
just have to add 'google' in front of second.

Another way is to use search() function with the RegEx '(.+)\.(google.+)', the result is 2 groups which are: 'Hulk.nba.abc' and 'googlekey.net'

Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx: Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx: Regexper[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900