Click here to Skip to main content
15,064,915 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi have the following data in the list

(carrier[5].pol[1].tuna)']
(carrier[6].pol[0].tuna)']
(carrier[6].pol[1].tuna)']
(carrier[7].pol[0].tuna)']
(carrier[7].pol[1].tuna)']
(fifo_i)']
(fisu_power_monitor_inst)']
(genblk1[0].MFA_MULT[0].com)']
(genblk1[0].MFA_MULT[1].com)']
(genblk1[0].MFA_MULT[2].com)']
(genblk1[0].MFA_MULT[3].com)']
(genblk1[0].MUX[0].miso)']
(genblk1[0].MUX[0].tuna)']
(genblk1[0].MUX[1].miso)']
(genblk1[0].MUX[1].tuna)']
(genblk1[0].MUX[2].miso)']
(genblk1[0].MUX[2].tuna)']
(genblk1[0].MUX[3].miso)']
(genblk1[0].MUX[3].tuna)']
u_cpri_port']
ul_pac_inst']
pack[0].abc_xyz']
pack[1].abc_xyz']
valid.cstr']


What I have tried:

I want to check each item of the list for a "." and keep the data that comes after "."
I tried the following but it gives me the data before the "." as well
[y  for x in slist for y in x.rsplit('.')]


I also tried the following but it gives me wrong data
[x.strip(".")[-1] for x in slist]




I want my final list to have the following:
tuna)']
tuna)']
tuna)']
tuna)']
tuna)']
(fifo_i)']
(fisu_power_monitor_inst)']
com)']
com)']
com)']
com)']
miso)']
tuna)']
miso)']
tuna)']
miso)']
tuna)']
miso)']
tuna)']
u_cpri_port']
ul_pac_inst']
abc_xyz']
abc_xyz']
cstr']
Posted
Updated 6-Oct-20 1:08am
v2

Try
Python
[ x.split('.')[-1] for x in slist] 
   
v2
Comments
Richard MacCutchan 6-Oct-20 8:45am
   
+5, I forgot about -1.
CPallini 6-Oct-20 9:25am
   
I just discovered it. :-)
Have my 5 as well.
Richard MacCutchan 6-Oct-20 9:56am
   
Thanks, but your answer is so much more pythonesque.
Member 11362771 6-Oct-20 22:43pm
   
@CPallini..it worked perfectly
CPallini 7-Oct-20 2:10am
   
I'm glad of it.
Python
[y for x in slist for y in x.rsplit('.')[2]]

But that will only work for strings that contain two period characters. You need to pre-process each string to test its format before adding to the new list.
Python
for item in slist:
    set = item.split('.') # try the split
    if len(set) == 3:     # is it a three word set?
        item = set[2]     # only need the last member of the set
    print(item)
   

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