[英]List of letters, change to list with numbers and 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]
如何將其更改為0 [C1, C2, C3...C6, G, I, O, P1, P2...]
這樣做的原因是 networkx 不允許具有相同標簽的節點,但不幸的是我不能 go 並更改原始數據,我需要在這里進行。
您可以將defaultdict
與itertools.count
結合使用來制作一個簡單的干凈解決方案。 您基本上為字典中的每個字母制作一個計數器,並將其與原始字母連接起來。 這應該讓你開始:
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']]
您還可以使用apply()
和適當的axis
和result_type
參數將此 function 應用於 Pandas dataframe :
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 個相同的字母,則必須更改此設置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.