Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy a Python dictionary N times into a list

Lets say I have a dictionary and list like the below:

l: list = [1,2,3,4]
d: dict = {"Hello": "World"}

I want to copy this dictionary exactly len(l) times.

I want to eventually create a list of tuples that looks like:

[(1, {"Hello": "World"}),(2, {"Hello": "World"}),(3, {"Hello": "World"}),(4, {"Hello": "World"})]

To create this I imagine I could do:

output: list = list(zip(l, repeated_dicts))

but need to replicate the dictionary the specified number of times in the list.

I tried using itertools.islice and itertools.cycle but couldn't quite get it. Any ideas?

like image 337
Coldchain9 Avatar asked Aug 31 '25 16:08

Coldchain9


3 Answers

This problem is caused by the new PathPatternParser introduced in Spring Boot 2.6. There are two ways to resolve:

As suggested by @gsan in the comment, add following in your application.yml or application.properties:

spring.mvc.pathmatch.matching-strategy=ant_path_matcher

OR

Add the following bean in your config class:

import org.springframework.boot.actuate.autoconfigure.endpoint.web.CorsEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementPortType;
import org.springframework.boot.actuate.endpoint.ExposableEndpoint;
import org.springframework.boot.actuate.endpoint.web.EndpointLinksResolver;
import org.springframework.boot.actuate.endpoint.web.EndpointMapping;
import org.springframework.boot.actuate.endpoint.web.EndpointMediaTypes;
import org.springframework.boot.actuate.endpoint.web.ExposableWebEndpoint;
import org.springframework.boot.actuate.endpoint.web.WebEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.annotation.ControllerEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.annotation.ServletEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Bean
public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier,
        ServletEndpointsSupplier servletEndpointsSupplier, ControllerEndpointsSupplier controllerEndpointsSupplier,
        EndpointMediaTypes endpointMediaTypes, CorsEndpointProperties corsProperties,
        WebEndpointProperties webEndpointProperties, Environment environment) {
    List<ExposableEndpoint<?>> allEndpoints = new ArrayList();
    Collection<ExposableWebEndpoint> webEndpoints = webEndpointsSupplier.getEndpoints();
    allEndpoints.addAll(webEndpoints);
    allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());
    allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());
    String basePath = webEndpointProperties.getBasePath();
    EndpointMapping endpointMapping = new EndpointMapping(basePath);
    boolean shouldRegisterLinksMapping = this.shouldRegisterLinksMapping(webEndpointProperties, environment,
            basePath);
    return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes,
            corsProperties.toCorsConfiguration(), new EndpointLinksResolver(allEndpoints, basePath),
            shouldRegisterLinksMapping, null);
}

private boolean shouldRegisterLinksMapping(WebEndpointProperties webEndpointProperties, Environment environment,
        String basePath) {
    return webEndpointProperties.getDiscovery().isEnabled() && (StringUtils.hasText(basePath)
        || ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));
}

Reference: Spring Boot 2.6 + Swagger startup exception: because "this.condition" is null

like image 141
Lyncean Patel Avatar answered Sep 02 '25 06:09

Lyncean Patel


Well, the answer mentioned by @Lyncean Patel is not 100% correct because there is no OR in fixing this. From this github issue discussion thread, the way to fix it to do both:

  1. set spring.mvc.pathmatch.matching-strategy: ant_path_matcher in application.properties
  2. add this bean
@Bean
public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier, ServletEndpointsSupplier servletEndpointsSupplier, ControllerEndpointsSupplier controllerEndpointsSupplier, EndpointMediaTypes endpointMediaTypes, CorsEndpointProperties corsProperties, WebEndpointProperties webEndpointProperties, Environment environment) {
        List<ExposableEndpoint<?>> allEndpoints = new ArrayList();
        Collection<ExposableWebEndpoint> webEndpoints = webEndpointsSupplier.getEndpoints();
        allEndpoints.addAll(webEndpoints);
        allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());
        allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());
        String basePath = webEndpointProperties.getBasePath();
        EndpointMapping endpointMapping = new EndpointMapping(basePath);
        boolean shouldRegisterLinksMapping = this.shouldRegisterLinksMapping(webEndpointProperties, environment, basePath);
        return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes, corsProperties.toCorsConfiguration(), new EndpointLinksResolver(allEndpoints, basePath), shouldRegisterLinksMapping, null);
    }


private boolean shouldRegisterLinksMapping(WebEndpointProperties webEndpointProperties, Environment environment, String basePath) {
        return webEndpointProperties.getDiscovery().isEnabled() && (StringUtils.hasText(basePath) || ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));
    }

like image 30
Shashank Vivek Avatar answered Sep 02 '25 07:09

Shashank Vivek


You're looking for itertools.repeat():

Make an iterator that returns object over and over again. Runs indefinitely unless the times argument is specified.

Note that the dictionaries returned by itertools.repeat() will share the same memory; changes to one dictionary will be shared across all of the elements of the list. That can lead to some pretty confusing behavior.

To avoid this, you can use map() with copy.copy() (if everything in the dictionary is immutable) or copy.deepcopy() to ensure that the memory for each dictionary is separate:

from itertools import repeat
from copy import copy

lst = [1, 2, 3, 4]
d = {"Hello": "World"}

list(zip(lst, map(copy, repeat(d))))

This outputs:

[
 (1, {'Hello': 'World'}), (2, {'Hello': 'World'}),
 (3, {'Hello': 'World'}), (4, {'Hello': 'World'})
]
like image 23
BrokenBenchmark Avatar answered Sep 02 '25 07:09

BrokenBenchmark