如何使用2个列表并将值合并到一个字典中

时间:2019-07-12 08:41:28

标签: python-3.x

尝试使用2个列表制作一本字典时无法获得正确的输出

输入是名称列表和图像列表。

protected void configure(HttpSecurity http) throws Exception {
    http 
            .authorizeRequests()
                .antMatchers("/managers").hasRole("MANAGERS")
                .antMatchers("/employees").hasRole("EMPLOYEES")
                .anyRequest().fullyAuthenticated()
            .and()
                .formLogin()
            .and()
            .csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
            .and()
                .logout().clearAuthentication(true)
                .logoutSuccessUrl("/")
                .deleteCookies("JSESSIONID")
                .invalidateHttpSession(true);
    }

-

AppInfoParser.getVersion();

-

name_list = ["Brad Pitt", "Chris Evans"]
image_list = ["http://image_1.png","http://image_2.png"]

有人可以帮我合并两个列表,并根据它制作字典吗?

1 个答案:

答案 0 :(得分:1)

name_list = ["Brad Pitt", "Chris Evans"]
image_list = ["http://image_1.png","http://image_2.png"]

dict = {}
actors = []
for i in range(len(name_list)):
    dict["firstName"] = name_list[i].split()[0]
    dict["lastName"] = name_list[i].split()[1]
    dict["imageURL"] = image_list[i]
    # or
    # dict.update({"firstName": name_list[i].split()[0], "lastName": name_list[i].split()[1], "imageURL": image_list[i]})
    actors.append(dict)

print (actors)

输出:

[{'firstName': 'Chris', 'lastName': 'Evans', 'imageURL': 'http://image_2.png'}, {'firstName': 'Chris', 'lastName': 'Evans', 'imageURL': 'http://image_2.png'}]