0

Let's say I have a set with values like (20140101224466, 20140209226655, ...), and I have a list that contains ('abcde.test.20140101224466', rbtd.test.20140209226655).

How would I compare my list against the set to get only the values in the list that contain the values in the set? Is there a more elegant approach?

Celeo
  • 5,583
  • 8
  • 39
  • 41
JoshQL
  • 1
  • 1
  • 2
    what have you tried so far? Also, is your "set" a true Python set, a list, or a tuple? Your notation is rather confusing... – MattDMo Sep 03 '14 at 21:36

2 Answers2

1
test_set = {20140101224466, 20140209226655, ... }
test_list = ['abcde.test.20140101224466', 'rbtd.test.20140209226655']

solution = [value for value in test_list if int(value.split('.')[-1]) in test_set]
Mike
  • 6,813
  • 4
  • 29
  • 50
  • I think this will work but is it possible to split with multiple delimeters like value.split('. _') ? – JoshQL Sep 03 '14 at 22:33
  • @JoshQL you can split with multiple delimiters using regular expressions: http://stackoverflow.com/questions/4998629/python-split-string-with-multiple-delimiters. You would `import re` and then replace `int(value.split('.')[-1])` with `int(re.split('.|_', value)[-1])` – Mike Sep 04 '14 at 12:08
0
set1 = {20140101224466, 20140209226655}
list2 = ['abcde.test.20140101224466', 'rbtd.test.20140209226655']

print [i for i in list2 if int(i.split('.')[-1]) in set1]
# ['rbtd.test.20140209226655', 'abcde.test.20140101224466']
sgarza62
  • 5,998
  • 8
  • 49
  • 69