如何在Python 2.7.1中将一串字母变成3个字母的单词

发布于 2024-10-26 07:30:29 字数 588 浏览 4 评论 0原文

我有一串字母 'aaabbbcccdddeeefffggg',我想将其读为 3 个字母的单词。例如,'aaa','bbb','ccc'...

您知道有任何代码可以执行此功能吗?

我的最终目标是为每个单词分配数字,例如

aaa= 123
bbb= 234
ccc= 356 ...

,并让输出为具有该值的单词在句子中的位置,

因此对于“aaabbbcccdddeeefffggg”的句子 三个字母的单词将是 'aaa','bbb','ccc'...

aaa 将是第一个位置 (1),bbb code> 将是第二个位置 (2),ccc 将是第三个位置 (3)

所以最终我会得到

(1,123),(2,234),(3,356) for 'aaa','bbb','ccc'

我已经尝试了几个小时,但我不知道如何做到这一点所以任何帮助将不胜感激

谢谢

I have a string of letters 'aaabbbcccdddeeefffggg' that i would like to read as 3 letter words. For example, 'aaa','bbb','ccc'...

Is there any code that you know of that can do this function?

My ultimate goal is to assign numbers to each word like

aaa= 123
bbb= 234
ccc= 356 ...

and have the output be the position of the word in the sentence with that value

so for the sentence of 'aaabbbcccdddeeefffggg'
the three letter words would be 'aaa','bbb','ccc'...

aaa would be the 1st position (1), bbb would be the 2nd position (2), ccc would be the 3rd position (3)

so ultimately i would get

(1,123),(2,234),(3,356) for 'aaa','bbb','ccc'

i have been trying this for a few hours and i cannot figure out how to do this so any help would be much appreciated

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

瑶笙 2024-11-02 07:30:29

像这样的东西吗?

data = 'aaabbbcccdddeeefffggg'
trans = {'aaa': 123, 'bbb': 234, 'ccc': 356, ...}
[(x + 1, trans[y * 3]) for x, y in enumerate(data[::3])]

否则:

def trans(c):
    a = ord('a')
    return ord(c) - a + 3 + 10 * (ord(c) - a + 2) + 100 * (ord(c) - a + 1)

data = 'aaabbbcccdddeeefffggg'
[(x + 1, trans(y)) for x, y in enumerate(data[::3])]

Something like this?

data = 'aaabbbcccdddeeefffggg'
trans = {'aaa': 123, 'bbb': 234, 'ccc': 356, ...}
[(x + 1, trans[y * 3]) for x, y in enumerate(data[::3])]

Otherwise:

def trans(c):
    a = ord('a')
    return ord(c) - a + 3 + 10 * (ord(c) - a + 2) + 100 * (ord(c) - a + 1)

data = 'aaabbbcccdddeeefffggg'
[(x + 1, trans(y)) for x, y in enumerate(data[::3])]
假装爱人 2024-11-02 07:30:29
>>> a = "aaabbbcccdddeeefffggg"
>>> [a[i:i+3] for i in range(0, len(a), 3)]
['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff', 'ggg']
>>> a = "aaabbbcccdddeeefffggg"
>>> [a[i:i+3] for i in range(0, len(a), 3)]
['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff', 'ggg']
情绪 2024-11-02 07:30:29
>>> import re
>>> re.findall(".{3}" ,"aaabbbcccdddeeefffggg")
['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff', 'ggg']
>>> import re
>>> re.findall(".{3}" ,"aaabbbcccdddeeefffggg")
['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff', 'ggg']
情未る 2024-11-02 07:30:29
ch = 'bbbiiieeefffhhhaaacccddd'

d = dict(zip(('aaa','bbb','ccc','ddd','eee','fff','ggg','hhh','iii'),
             ('123','234','345','456','567','678','789','8910','91011')))

def lect(x):
    gen = iter(x) 
    while True:
        yield ''.join((gen.next(),gen.next(),gen.next()))

print [ (i+1,d[x]) for i,x in enumerate(lect(ch)) ]

或者

import re

ch = 'bbbiiieeefffhhhaaacccddd'

d = dict(zip(('aaa','bbb','ccc','ddd','eee','fff','ggg','hhh','iii'),
             ('123','234','345','456','567','678','789','8910','91011')))

pat = re.compile('|'.join(d.iterkeys()))

print [ ((mat.start()/3)+1,d[mat.group()]) for mat in pat.finditer(ch) ]
ch = 'bbbiiieeefffhhhaaacccddd'

d = dict(zip(('aaa','bbb','ccc','ddd','eee','fff','ggg','hhh','iii'),
             ('123','234','345','456','567','678','789','8910','91011')))

def lect(x):
    gen = iter(x) 
    while True:
        yield ''.join((gen.next(),gen.next(),gen.next()))

print [ (i+1,d[x]) for i,x in enumerate(lect(ch)) ]

or

import re

ch = 'bbbiiieeefffhhhaaacccddd'

d = dict(zip(('aaa','bbb','ccc','ddd','eee','fff','ggg','hhh','iii'),
             ('123','234','345','456','567','678','789','8910','91011')))

pat = re.compile('|'.join(d.iterkeys()))

print [ ((mat.start()/3)+1,d[mat.group()]) for mat in pat.finditer(ch) ]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文