我试图编写将采用字符串和长度的python代码,并搜索字符串以告诉我哪个特定长度的子字符串出现次数最多,优先考虑第一个字符串是否有'一个领带。
例如,@echo off
setlocal enabledelayedexpansion
cls
set counter=0
for /f "delims=" %%A in (input.txt) do (
set line[!counter!]=%%A
set /a counter+=1
)
set /a counter-=2
set LF=^
for /L %%A in (1,1,!counter!) do set sheet=!sheet!!line[%%A]!!LF!
set /a counter+=1
for /L %%A in (!counter!,1,!counter!) do set sheet=!sheet!!line[%%A]!
echo !sheet!>input.txt
应返回@echo off
setlocal enabledelayedexpansion
cls
:: findstr /n puts line numbers at the start of each line, which will allow us to preserve blank lines
for /f "tokens=1 delims=:" %%A in ('findstr /n "^" input.txt') do set line_counter=%%A
::set /p preserves special characters
<input.txt (
for /L %%A in (1,1,!line_counter!) do set /p line[%%A]=
)
set /a line_counter-=1
set LF=^
:: Do NOT delete the two blank lines above this line.
for /L %%A in (2,1,!line_counter!) do set sheet=!sheet!!line[%%A]!!LF!
set /a line_counter+=1
for /L %%A in (!line_counter!,1,!line_counter!) do set sheet=!sheet!!line[%%A]!
echo !sheet!>input.txt
我试过了:
"cadabra abra" 2
但似乎无法让它发挥作用。我知道我至少做错了一些事情,但我不确定是什么。这是我在Python中的第一个程序,但我认为我的问题可能更多的是算法而不是语法。
答案 0 :(得分:2)
尝试使用内置方法,这样可以让您的生活更轻松:
>>> s = "cadabra abra"
>>> x = 2
>>> l = [s[i:i+x] for i in range(len(s)-x+1)]
>>> l
['ca', 'ad', 'da', 'ab', 'br', 'ra', 'a ', ' a', 'ab', 'br', 'ra']
>>> max(l, key=lambda m:s.count(m))
'ab'
编辑:
根据Stefan Pochmann评论更简单的语法:
>>> max(l, key=s.count)
答案 1 :(得分:1)
import sys
from collections import OrderedDict
def main():
inputstring = sys.argv[1]
length = int(sys.argv[2])
analyze(inputstring, length)
def analyze(inputstring, length):
d = OrderedDict()
for i in range(0, len(inputstring) - length + 1):
substring = inputstring[i:i+length]
if substring in d:
d[substring] += 1
else:
d[substring] = 1
maxlength = max(d.values())
for k,v in d.items():
if v == maxlength:
print(k)
break
main()
答案 2 :(得分:0)
非常好用于第一个Python程序的解决方案。在学习语言时,花一些时间阅读优秀的文档。它充满了例子和提示。
例如,标准库包括一个Counter
类用于计算事物(显然)和一个OrderedDict
类,它记住输入键的顺序。但文档中包含一个结合两者的例子来制作OrderedCounter
,可以用来解决这个问题:
from collections import Counter, OrderedDict
class OrderedCounter(Counter, OrderedDict):
pass
def analyze(s, n):
substrings = (s[i:i+n] for i in range(len(s)-n+1))
counts = OrderedCounter(substrings)
return max(counts.keys(), key=counts.__getitem__)
analyze("cadabra abra", 2)