​向大神们请教个问题:

users = ['admin', 'lily', 'Hack', 'join', 'Mark'] new_users = [] for new_user in users:     new_users.append(new_user.lower()) 为什么这样就可以转写list里的元素为小写  for new_user.lower() in users:    这样就会运行报错,报错-属性错误:“列表”对象没有属性“低” 必须像是上面那种写法吗?list里的元素转小写,有其他的方法吗?

慕慕83470374
浏览 745回答 3
3回答

慕设计2395807

RAVSBWFODMCJYHXTJMOKNQBQRAQFVENKAEZPYURAXTPMTOYAJMADZJFCRHXZCEHCYHQFVXUDMVYOFWEGQSVEYIKAXFOEAJYBKHCWFORNQSVYIEHWLVDAAKGQZPRHJSNCYVEHWEHDMVLZOYBODMCKHKSHDMCKLOEGCFOSIYNWKGQZVLWSVEGQZVYUQFISHCZPXMCZVFBDMVYUJZOKGDYOWEUKGJGWLAJTCRAWMWLCKGWLBXAOXVGCLOJTWSHLBNILBKTJFVKNPYOXGPEHVSPYMIEBXMPYIYLUQGIYHDZISHJMPEAXAPEHYHBLAJZIFOLENWGAWZCEOKFOXTVLZVLVYOQMCYHDZCFDXNILUQFVQAQPRAJZVRMJMVIIEAQMBLNXTWZHKZIYUDLHCRAQMAQFVEUOQGCKMVLAEOLFORZCFIXBXMBWGWLBDZWSNVLAKMCSHLBOKAPYOXNPZCPKACLBKTQSIKNCLADZIFOLPDFOENJZCLIMHKGJSUDMPFHJRUXMCZBWZHJYIXAAXNKAENPFBXGPXNWQSVQNWYOCFITHJMJZOVRBKVKADSGPFVXHSUWTPYHDXAQWEAWSPEAQGDWIRHQTFFZYFQZNQSOXAQZCLNJFIRGWZITTOXUDZHKNWMGPEHDMPEHRNTPYNIRHQSILYAMWZFOYPZPKTCLHEZWSPKFUQTCENBRHKSOKZVLAKGJKRMPFHRTILGWFAQSOYUXNWGVXXGWMCIOSVYBKUWLBXAKTBJXGJLOEAQTEGVFHQGCSVKTVXADFOKAQKVRTWMNKNWAWIEAHTGFIVVLPEHDMWYBEUBXMOKAQSVKNWHJMPRNXMVTVRGVFUKGPSICOQFVXAPYUQZGFQDCJJJSSBBDSCPZCSAQF

pardon110

python虽然是动态语言,但它底层仍然是基于类型系统。for ... in 结构会对可迭代对象进行处理,in 之间的值为迭代对象中的元素,显然 users列表中的每一个元素都是字符串对象。只有字符串对象new_user才有lower方法。而list对象是没有的,具体你可以查看list类与str类for x in [1,2,3]  #相当于x=1,x=2,x=3
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python