简体   繁体   中英

List of letters, change to list with numbers and letters

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM