Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [<common-part>-<random-text-a>, <common-part>-<random-text-b>]
- [<random-text-a>, <random-text-b>]
- import os
- p = ["<common-part>-<some-text-a>", "<common-part>-<random-text-b>"]
- commonprefix = os.path.commonprefix(p)
- new_p = [x[len(commonprefix):] for x in p]
- print(new_p)
- ['some-text-a>', 'random-text-b>']
- str1 = "hello"
- list1 = ["hello1", "hello2", "hello3"]
- list2 = []
- for i in list1:
- list2.append(i.replace(str1,""))
- print list2
- [newstr.replace(str1, '') for newstr in list_of_strings]
- MyList = ["xxx-56", "xxx-57", "xxx-58"]
- MyList = [x[len(prefix):] for x in MyList] # for each x in the list,
- # this function will return x[len(prefix):]
- # which is the string x minus the length of the prefix string
- print(MyList)
- ---> ['56', '57', '58']
- common = "Hello_"
- lines = ["Hello_1 !", "Hello_2 !", "Hello_3 !"]
- new_lines = []
- for line in lines:
- # Finding first occurrence of the word we want to remove.
- startIndex = line.find(common) + len(common)
- new_lines.append(line[startIndex:])
- print new_lines
Add Comment
Please, Sign In to add comment