The hot topics discussed in the exchange group can be easily solved with Python!

I. Introduction

In an exchange group with a good learning atmosphere in Ye Tingyun, one day a Tiezi asked for advice on a question, which sparked a heated discussion among the group of friends, let's take a look!

The picture taken from the mobile phone screenshot is a bit big~~, use opencv resize to process it, then use the computer QQ screenshot, and use the mosaic to protect the privacy of group friends’ avatars.

img1 = cv.imread(r"D:\test\pic\jietu_01.jpg")
img1 = cv.resize(img1, dsize=None, fx=0.5, fy=0.5)

img2 = cv.imread(r"D:\test\pic\jietu_02.jpg")
img2 = cv.resize(img2, dsize=None, fx=0.5, fy=0.5)
cv.imshow("img1", img1)
cv.imshow("img2", img2)

The topic is as follows: It

seems quite easy, the letter-if followed by a number, the letter will be num, and there will be only one if there is no number. But if you know the truth through practice, you will only know when you write code tests.

Second, write the code

Three lines of Python code?

str1 = "A3B5C8"
ls1 = [i if i.isalpha() else str1[index_ - 1] * (int(i) - 1) for index_, i in enumerate(str1)]

The results are as follows:

But things don't seem to be that simple. When the following number is greater than 10, the output will be wrong, as shown below:

What about this?

import re 

def func(s):
    return "".join([c * int(count) for c, count in re.findall("(\D+)(\d+)", s)])


The results are as follows:

This is right, if the following number is greater than 10, the output result is correct, but the careful group of friends found the problem again, the output result like abc10 is wrong! As follows:

Whether the position of the number is inserted or not, whether it is greater than 10 needs to be considered, otherwise there will be problems with the output, and it may not be so concise and elegant. Simply rude and kill it!

str2 = "a10b10c"
ls = []
for i in range(len(str2)):
    num = ""
    if str2[i].isalpha():   # 是字母
        # 查找后面跟的数字  拼出来
        for j in range(i + 1, len(str2)):
            if str2[j].isdigit():
                num += str2[j]
                # print(num)
            # 开始又是字母了   break
        # 字母重复 然后添加进列表 没有重复 直接添加
        if num:
            ls.append(str2[i] * int(num))
# 输出

The results are as follows:

Finally got it~~

code show as below:

s = "ddeeeeaaddccccc"  # 2 4 2 2 5
start_str = s[0]  # 初始字母
count, ls = 0, []
# 遍历
for i in range(len(s)):
    if s[i] == start_str:  # 还是等于初始字母是  统计个数
        count += 1
        # 到最后一个字母  这里添加 因为后面已经无了
        if i == len(s) - 1:
            ls.append(start_str + str(count))
        # 已经开始不为初始字母了  之前那个添加到列表
        # 更新初始字母和count
        ls.append(start_str + str(count))
        start_str, count = s[i], 1


The results are as follows:

Summary: To learn programming, many topics may not be as simple as they seem. Real knowledge can be gained through practice, and problems can be discovered by hands. Thinking more can solve problems. Don't have a good eye and a low hand!