如何在Python中进行不区分大小写的字符串比较?
我想以一种非常简单和Pythonic的方式将常规字符串的比较封装到存储库字符串中.我还希望能够使用常规python字符串在字符串中查找值.
我正在处理文本字符串,如下所示:
LN1 2DW, DN21 5BJ, DN21 5BL, ...
在Python中,我如何计算逗号之间的元素数量?每个元素可以由6个,7个或8个字符组成,在我的示例中,显示了3个元素.分隔符始终是逗号.
我从未做过任何与文本挖掘有关的事情,所以这对我来说是一个开始.
我想要一个像'ddxxx'返回的字符串('d': 2, 'x': 3).到目前为止,我已经尝试过
result = {}
for i in s:
if i in s:
result[i] += 1
else:
result[i] = 1
return result
Run Code Online (Sandbox Code Playgroud)
s字符串在哪里,但我不断得到一个KeyError.例如,如果我把s因为'hello'返回的错误是:
result[i] += 1
KeyError: 'h'
Run Code Online (Sandbox Code Playgroud) Level ="""
aaaaaa
awawa"""
Run Code Online (Sandbox Code Playgroud)
我想知道你如何计算python中多行字符串的行数.
一旦你计算了那些线,你如何计算该线中有多少个字母.我假设你要做这个部分len(line_of_string).
如何在没有str.count()它的情况下执行此操作,因为它在Python v2.7.3文档中被列为已弃用?
我无法找到我应该使用的东西.
例如:
input: I live in New York
output: York New in live I
Run Code Online (Sandbox Code Playgroud)
PS:我用过s[::-1],这只是反转字符串
kroY weN ni evil I,但是这不是所需的输出.我也尝试过:
def rev(x) :
x = x[::-1]
for i in range(len(x)) :
if x[i] == " " :
x = x[::-1]
continue
print x
Run Code Online (Sandbox Code Playgroud)
但这也是不正确的请帮助我编写代码.
我需要一个函数来计算每个字母出现在字符串中的次数.它必须将大写字母和小写字母统计为相同的字母.我有点做了,但它并不完美.
def lmao(x):
aye=x.count("a")
Aye=x.count("A")
bye=x.count("b")
Bye=x.count("B")
cye=x.count("c")
Cye=x.count("C")
dye=x.count("d")
Dye=x.count("D")
Eye=x.count("E")
eye=x.count("e")
Fye=x.count("F")
fye=x.count("f")
Gye=x.count("G")
gye=x.count("g")
Hye=x.count("H")
hye=x.count("h")
Iye=x.count("I")
iye=x.count("i")
Jye=x.count("J")
jye=x.count("j")
Kye=x.count("K")
kye=x.count("k")
Lye=x.count("L")
lye=x.count("l")
Mye=x.count("M")
mye=x.count("m")
Nye=x.count("N")
nye=x.count("n")
Oye=x.count("O")
oye=x.count("o")
Pye=x.count("P")
pye=x.count("P")
Qye=x.count("Q")
qye=x.count("q")
rye=x.count("r")
Rye=x.count("R")
sye=x.count("s")
Sye=x.count("S")
tye=x.count("t")
Tye=x.count("T")
uye=x.count("u")
Uye=x.count("U")
Vye=x.count("V")
vye=x.count("v")
Wye=x.count("W")
wye=x.count("w")
Xye=x.count("X")
xye=x.count("x")
Yye=x.count("Y")
yye=x.count("y")
Zye=x.count("Z")
zye=x.count("z")
killme=(aye+Aye,bye+Bye,cye+Cye,Dye+dye,Eye+eye,Fye+fye,Gye+gye,Hye+hye,Iye+iye,jye+Jye,Kye+kye,Lye+lye,Mye+mye,Nye+nye,Oye+oye,Pye+pye,Qye+qye,rye+Rye,sye+Sye,Tye+tye,uye+Uye,Vye+vye,Wye+wye,xye+Xye,Yye+yye,Zye+zye)
return killme
Run Code Online (Sandbox Code Playgroud)
所以,是的,那就是我想出的灾难.有没有办法缩短这个过程?
您将如何在使用所有字符的同时将普通字符串拆分为尽可能多的相同部分。例如
a = "abab"
Run Code Online (Sandbox Code Playgroud)
会返回"ab",而与
b= "ababc"
Run Code Online (Sandbox Code Playgroud)
它会返回"ababc",因为它不能使用所有字母分成相同的部分。
我做过这个但是太长了,我怎么能做一个更简单的方法呢?提前致谢
letter_a = all_words.count('a')
letter_b = all_words.count('b')
letter_c = all_words.count('c')
letter_d = all_words.count('d')
letter_e = all_words.count('e')
letter_f = all_words.count('f')
letter_g = all_words.count('g')
letter_h = all_words.count('h')
letter_i = all_words.count('i')
letter_j = all_words.count('j')
letter_k = all_words.count('k')
letter_l = all_words.count('l')
letter_m = all_words.count('m')
letter_n = all_words.count('n')
letter_o = all_words.count('o')
letter_p = all_words.count('p')
letter_q = all_words.count('q')
letter_r = all_words.count('r')
letter_s = all_words.count('s')
letter_t = all_words.count('t')
letter_u = all_words.count('u')
letter_v = all_words.count('v')
letter_w = all_words.count('w')
letter_x = all_words.count('x')
letter_y = all_words.count('y')
letter_z = all_words.count('z')
print("There …Run Code Online (Sandbox Code Playgroud) 我正在做一个Python脚本.
我有一个字符串,len()字符串是1048576和sys.getsizeof()字符串的字符串1048597.
但是,当我将此字符串写入文件时,文件的字节大小为1051027.我的代码如下,任何人都可以告诉我为什么文件的字节大小与字符串的字节大小不同?
print type(allInOne) # allInOne is my string
print len(allInOne)
print sys.getsizeof(allInOne)
newFile = open("./all_in_one7.raw", "w")
newFile.write(allInOne.encode('ascii'))
newFile.close()
Run Code Online (Sandbox Code Playgroud)
我的字符串是allInOne,它是由许多进程生成的,它是这样生成的allInOne = numpy.uint8(dataset.pixel_array).tostring(),在此之上,dataset.pixel_array是类型numpy.ndarray.我不知道这些信息是否有任何帮助.
我想编写一个函数来计算字符串中子字符串的所有非重叠出现次数。这是我到目前为止:
def count(substr,theStr):
count = 0
for i in range(len(theStr)):
if theStr[i:i+len(substr)] == substr:
count = count + 1
return count
Run Code Online (Sandbox Code Playgroud)
如您所见,我的函数只计算字符串的出现次数,而不计算不重叠的出现次数。例如,输入“ana”和“Banana”将产生2,即使“Banana”中只有一个“ana”的非重叠实例。如何扩展我的功能以使其正常工作?
python ×9
string ×3
python-2.7 ×2
python-3.x ×2
comma ×1
comparison ×1
counting ×1
deprecated ×1
dictionary ×1
file ×1
function ×1
python-2.x ×1
text ×1
text-mining ×1