Reputation:
If i have a list of letters:
Out[30]:
LN
0 [C, C, C, C, C, C, G, I, O, P, P, P, R, R, R, ...
1 [C, C, C, C, C, C, G, I, O, P, P, P, R, R, R, ...
2 [C, C, C, C, C, C, G, I, O, P, P, R, R, R, R, ...
3 [C, C, C, C, C, C, G, I, O, P, P, R, R, R, R, ...
4 [C, C, C, C, C, C, G, I, O, P, P, P, R, R, R, ...
...
43244 [G, I, O, P, P, P, R, R, R, R]
43245 [G, I, O, P, P, P, R, R, R, R]
43246 [G, I, O, P, P, R, R, R]
43247 [G, I, O, P, P, R, R, R]
43248 [G, I, O, P, R, R]
How can i change it to 0 [C1, C2, C3...C6, G, I, O, P1, P2...]
The reason for this is that networkx will not allow nodes with the same labels, but unfortunately i cannot go and change the raw data, i need to do it here.
Upvotes: 0
Views: 129
Reputation: 92460
You can combine defaultdict
with itertools.count
to make a simple clean solution. You basically make a counter for each letter in the dict and concat it with the original letter. This should get you started:
from collections import defaultdict
from itertools import count
counter = defaultdict(lambda: count(1))
l = ['C', 'C', 'C', 'P', 'P', 'G', 'C', 'P']
[c + str(next(counter[c])) for c in l]
# ['C1', 'C2', 'C3', 'P1', 'P2', 'G1', 'C4', 'P3']
You can simplify the defaultdict a bit if you don't mind the counts starting at zero:
counter = defaultdict(count)
You can, of course, apply this to a list of lists:
from collections import defaultdict
from itertools import count
l = [
['C', 'C', 'C', 'P', 'P', 'G', 'C', 'P'],
['C', 'C', 'G', 'P', 'C', 'G', 'C', 'P']
]
def addNumbs(l):
counter = defaultdict(lambda: count(1))
return [c + str(next(counter[c])) for c in l]
list(map(addNumbs, l))
#[['C1', 'C2', 'C3', 'P1', 'P2', 'G1', 'C4', 'P3'],
# ['C1', 'C2', 'G1', 'P1', 'C3', 'G2', 'C4', 'P2']]
You can also apply this function to a Pandas dataframe using apply()
with appropriate axis
and result_type
parameters:
import pandas as pd
from collections import defaultdict
from itertools import count
def addNumbs(l):
counter = defaultdict(lambda: count(1))
return [c + str(next(counter[c])) for c in l]
df = pd.DataFrame([
['C', 'C', 'C', 'P', 'P', 'G', 'C', 'P'],
['C', 'C', 'G', 'C', 'G', 'G', 'C', 'P']
])
res = df.apply(addNumbs, axis=1, result_type="expand")
res
will be:
0 1 2 3 4 5 6 7
0 C1 C2 C3 P1 P2 G1 C4 P3
1 C1 C2 G1 C3 G2 G3 C4 P1
Upvotes: 1
Reputation: 21
This solution assumes that all of the same letter are grouped together and are one digit.
letters = ['C','C','C','G', 'I', 'O', 'P', 'P', 'P', 'R', 'R', 'R','R']
for i in range(len(letters)):
if i != 0:
current_word = letters[i]
prev_word = letters[i-1]
if current_word[0] == prev_word[0]:
if len(prev_word) == 1:
letters[i] = current_word + '1'
else:
letters[i] = current_word[0] + str(int(prev_word[1]) + 1)
print(letters)
This would have to be changed if there is a possibility for larger than 10 of the same letter in a row.
Upvotes: 0