'how should I find names with a shared characters in a list names in python?
I have a list of names, let's say like ['R_1', 'ER_1',ER_2''R_2','R_3', 'ER_1',ER_2', 'C1', 'R_5',...]. Then I want to find those which has 'R' in common, or 'ER'. Which command should I use?
I would like to find these later in pandas.
Solution 1:[1]
l = ['R_1', 'ER_1', 'ER_2', 'R_2', 'R_3', 'ER_1', 'ER_2', 'C1', 'R_5']
# create a list with your keys (i.e. the string before the "_")
keys = list(set([el.split("_")[0] for el in l]))
# put them in a dictionary of the form {key1: [key1_x, key1_y], key2: [key2_z, key2_x], ...}
groups = {k: [el for el in l if el.split("_")[0] == k] for k in keys}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | John Giorgio |
