Guest User

Untitled

a guest
Jan 17th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. [<common-part>-<random-text-a>, <common-part>-<random-text-b>]
  2.  
  3. [<random-text-a>, <random-text-b>]
  4.  
  5. import os
  6.  
  7. p = ["<common-part>-<some-text-a>", "<common-part>-<random-text-b>"]
  8. commonprefix = os.path.commonprefix(p)
  9.  
  10. new_p = [x[len(commonprefix):] for x in p]
  11.  
  12. print(new_p)
  13.  
  14. ['some-text-a>', 'random-text-b>']
  15.  
  16. str1 = "hello"
  17. list1 = ["hello1", "hello2", "hello3"]
  18. list2 = []
  19. for i in list1:
  20. list2.append(i.replace(str1,""))
  21. print list2
  22.  
  23. [newstr.replace(str1, '') for newstr in list_of_strings]
  24.  
  25. MyList = ["xxx-56", "xxx-57", "xxx-58"]
  26. MyList = [x[len(prefix):] for x in MyList] # for each x in the list,
  27. # this function will return x[len(prefix):]
  28. # which is the string x minus the length of the prefix string
  29.  
  30. print(MyList)
  31.  
  32. ---> ['56', '57', '58']
  33.  
  34. common = "Hello_"
  35. lines = ["Hello_1 !", "Hello_2 !", "Hello_3 !"]
  36.  
  37. new_lines = []
  38. for line in lines:
  39. # Finding first occurrence of the word we want to remove.
  40. startIndex = line.find(common) + len(common)
  41. new_lines.append(line[startIndex:])
  42.  
  43. print new_lines
Add Comment
Please, Sign In to add comment