Python 有人能帮我使我的程序案例不敏感吗?
这是我到目前为止的代码,现在它区分大小写。我尝试将字符串设置为小写(使用.lower),但没有成功。有人能帮忙吗Python 有人能帮我使我的程序案例不敏感吗?,python,Python,这是我到目前为止的代码,现在它区分大小写。我尝试将字符串设置为小写(使用.lower),但没有成功。有人能帮忙吗 file=open("numbertext.txt","w") my_string= input("Enter a sentence. ") splitted = my_string.split() d = {} l=[] for i,j in enumerate(splitted): if j in d: l.append(d[j]) else
file=open("numbertext.txt","w")
my_string= input("Enter a sentence. ")
splitted = my_string.split()
d = {}
l=[]
for i,j in enumerate(splitted):
if j in d:
l.append(d[j])
else:
d[j]=i
l.append(i)
print(l)
file.write(str(l))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 4, 11, 5, 6]
file=open("newfile.txt","w")
file.close
在Python
something.method
中,它不调用该方法——它只访问该名称的属性。因此,在您的情况下,需要执行file.close()
(或者更好的是,使用with语句),对于原始问题:使用somestring.lower()
splitted=my_string.lower().split()
将执行此操作函数lower
返回已转换的字符串,而不转换字符串本身。您应该在此处使用lower
:
splitted = my_string.lower().split()
优化您的代码:
d = {}
l=[]
for i,j in enumerate(splitted):
l.append(d.setdefault(j, i))
with open("numbertext.txt","w") as f:
f.write(str(l))
你的程序是做什么的?为什么套管会出现问题?当套管不起作用时,您将
.lower()
放在了哪里?它应该在my_string=…
行的末尾。它可能与编写文件的方式不同。close
实际上并不调用close方法,您需要file.close()