这是我的代码到目前为止,现在它是区分大小写的。我尝试将字符串变为小写(使用.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:
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
函数
lower
返回转换后的字符串,不转换字符串本身。您应该在这里使用lower
:代码优化:
splitted = my_string.lower().split()
就可以了在Python中
something.method
不调用该方法-它只访问该名称的属性。所以在您的例子中,您需要执行file.close()
(或者更好地使用with语句),对于您的原始问题:使用somestring.lower()
。你知道吗相关问题 更多 >
编程相关推荐