[英]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...]
如何将其更改为
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.这样做的原因是 networkx 不允许具有相同标签的节点,但不幸的是我不能 go 并更改原始数据,我需要在这里进行。
You can combine defaultdict
with itertools.count
to make a simple clean solution.您可以将
defaultdict
与itertools.count
结合使用来制作一个简单的干净解决方案。 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:如果您不介意从零开始的计数,您可以稍微简化一下 defaultdict:
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:您还可以使用
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
will be: 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
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.如果有可能连续超过 10 个相同的字母,则必须更改此设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.