JoshQL
JoshQL

Reputation: 1

how to get values from a list that are like values in a set in python

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?

Upvotes: 0

Views: 65

Answers (2)

sgarza62
sgarza62

Reputation: 6238

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

Upvotes: 0

Mike
Mike

Reputation: 7203

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]

Upvotes: 1

Related Questions