Python 字母列表,更改为带有数字和字母的列表

Python 字母列表,更改为带有数字和字母的列表,python,graph,networkx,stellargraph,Python,Graph,Networkx,Stellargraph,如果我有一份信件清单: 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, ...

如果我有一份信件清单:

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]
如何将其更改为
0[C1、C2、C3…C6、G、i、O、P1、P2…]

这样做的原因是networkx不允许具有相同标签的节点,但不幸的是,我无法更改原始数据,我需要在此处执行此操作。

您可以与结合以生成简单的干净解决方案。基本上,你要为口述中的每个字母做一个计数器,然后把它和原来的字母连在一起。这应该让你开始:

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']
如果您不介意从零开始计数,可以稍微简化defaultdict:

counter = defaultdict(count)
当然,您可以将此应用于列表列表:

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']]
您还可以使用适当的
结果类型
参数将此功能应用于数据帧:

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
将是:

    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
您可以与混合,制成简单的清洁溶液。基本上,你要为口述中的每个字母做一个计数器,然后把它和原来的字母连在一起。这应该让你开始:

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']
如果您不介意从零开始计数,可以稍微简化defaultdict:

counter = defaultdict(count)
当然,您可以将此应用于列表列表:

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']]
您还可以使用适当的
结果类型
参数将此功能应用于数据帧:

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
将是:

    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

此解决方案假定所有相同的字母都分组在一起,并且是一个数字

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)

如果一行中的同一字母可能大于10个,则必须更改此设置。

此解决方案假定所有同一字母都分组在一起,并且是一个数字

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)

如果一行中的同一个字母可能超过10个,则必须更改此选项。

实际上我是这样做的
df['Ln']=df['Ln'].apply(addnum)
成功了。我不希望它扩大。但是谢谢你的代码,它帮了我很大的忙。事实上,我做到了
df['Ln']=df['Ln'].apply(addnum)
,它成功了。我不希望它扩大。但是谢谢你的代码,它帮了很多忙。