通过使用Python(主要是REGEX),我希望获得以下输出:
string = 'leelee'
result = [('l',1),('e',2),('l',1),('e',2)]
答案 0 :(得分:1)
您可以使用regex的 help 来做到这一点,但不能单独使用regex。
首先按字符分组,然后列出理解以计算那些分组中的元素。
import re
s = 'leelee'
x = re.findall(r'(.)(\1*)',s)
print([[e[0],1+len(e[1])] for e in x])
上面的正则表达式捕获一个字符(.)
,如果该字符紧随其后(\1*)
,则匹配该字符任意次。
答案 1 :(得分:0)
您为什么需要正则表达式? Python的*
是字符串乘法,而+
是字符串串联。例如:
print("h" * 5) # hhhhh
print("h" + "t") # ht
答案 2 :(得分:0)
这是一个带有for
循环的版本:
for pair in result:
for char, times in pair:
for _ in range(times):
print(char, end='')
或者这是一个具有理解力和join
的人:
print(''.join([x * y for x, y in result]))
或最直接的解决方案:
print(string)
我认为您不会找到只使用正则表达式的人...
答案 3 :(得分:0)
您可以使用正则表达式以及其他工具来执行此操作,但这并不理想。使用itertools.groupby
更加容易。
#stickyElement {
position: absolute;
left: 0;
}
function scrolling(){
$('.someElementScrollingLeft').on('scroll', function() {
let offset = $(this).scrollLeft();
/* For me it only didn't work on phones, so checking screen size */
if($( window ).width() <= 768)
{
let stickyElement = $('#stickyElement');
stickyElement.css("left", offset);
}
});
}
here说明了这种获取迭代器len的方法。