import re
import math
def encode(n, strng):
for z in range (n):
temp = []
spcPos = []
for i , x in enumerate(strng):
if x == ' ':
spcPos.append(i)
strng = re.sub(' ', '', strng)
for i in range (len(strng)):
temp.append(strng[i - n])
temp2 = []
ezEnu = 0
for i in temp:
if ezEnu in (spcPos):
temp2.append(' ')
ezEnu += 1
temp2.append(i)
ezEnu += 1
temp2 = ''.join(temp2)
temp2 = temp2.split()
temp = []
withSpc = []
withSpc2 = []
oglen = []
for i in temp2:
oglen.append(len(i))
for x in range (math.ceil(n / len(i))):
withSpc.append(i)
withSpc2.append(''.join(withSpc))
withSpc = []
newa = []
for i, x in enumerate(withSpc2):
for y in range(int(oglen[i])):
newa.append(x[y - n])
temp2 = []
ezEnu = 0
for i in newa:
if ezEnu in (spcPos):
temp2.append(' ')
ezEnu += 1
temp2.append(i)
ezEnu += 1
strng = ''.join(temp2)
return(''.join([str(n), ' ', strng]))
如何获取字符串并将其制成,以便将转义序列视为常规字符?
我有这个函数编码,它接受一个字符串并对其进行编码。问题是当我收到带有/ in或任何转义序列的字符串时,它将忽略它们,并且不将其视为要编码的消息的一部分。
我无法控制函数中的字符串,它们是预定义的。
据我了解,r''仅适用于新字符串。
谢谢:)
答案 0 :(得分:0)
根据您的确切需求,您可能会检查这是否足以满足您的用例。
mtcars %>%
select(qsec, cyl) %>%
filter(cyl == 8) %>%
pull(qsec) %>%
mean
#[1] 16.77214