如果某些列表项出现在另一个列表中,如何使用str.lower将它们变为小写?

问题描述

我一直在尝试解决CodeWars上的问题并遇到障碍。我知道这不是特别的“ pythonic”方法(我是初学者)。而且我敢肯定,有更有效,更高级的方法可以做到这一点。但是我实际上只是想了解为什么下面的代码无法正常工作。我只想使列表中的字符串项小写,如果它们出现在另一个过滤器单词列表中。 (无视删除第一项的代码。这是因为任务要求对第一项进行不同的处理。)

def title_case(title,minors=''):
    title = title.title()
    mwds = minors.split()
    lst = title.split(" ",1)

    exfirst = lst[1].split()
    for wd in exfirst:
        wd.lower()
        if wd in mwds:
            wd.lower()
    return exfirst

print(title_case('a clash of KINGS','a an the of'))
print(title_case('THE WIND IN THE WILLOWS','The In'))

结果:

['Clash','Of','Kings']
['Wind','In','The','Willows']

预期结果:

['Clash','of','in','the','Willows']

解决方法

这是因为您没有将新的未成年人分配给旧的未成年人。要进行分配,必须在exfirst列表上使用index作为迭代器。

for i in range(exfirst):
    wd= exfirst[i].lower()
    if wd in mwds:
        exfirst[i] = wd
,

lower()方法不会更改字符。您应该这样做:

public function update(Request $request)
{
    $data = $this->validate($request,[
        'Firstname' => 'required|max:255','Lastname'  => 'required|max:255','Email'     => 'required|email','Address'   => 'required','MobileNum' => 'nullable',//<-- assuming this can be nullable as it wasn't included in your original validation
    ]);

    Auth::user()->fill($data)->save();

    return Auth::user();
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...