Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
191 views
in Technique[技术] by (71.8m points)

python - I want to change my list with lists to a list with tuples

Right now I get this:

[[28, 27], [29, 27], [29, 28], [29, 29], [28, 29], [27, 29], [27, 28], [27, 27]]

I want this output:

[(28, 27), (29, 27), (29, 28), (29, 29), (28, 29), (27, 29), (27, 28), (27, 27)]

My code:

def load_seed_from_file(_file_name: str) -> tuple:
    """ Load population seed from file. Returns tuple: population (dict) and world_size (tuple). """

population = {}
file_name = Path(RESOURCES / str("seed_" + _file_name + ".json"))
print(file_name)
with open (file_name, "r") as json_file:
    loaded_json = json.load(json_file)
    for key, value in loaded_json.items():
        if key == "world_size":
            world_size = tuple(value)
            print(world_size)
        else:
            for i in value:
                if value[i] != None:
                    print(value[i]["neighbours"])
                else:
                    population.update({i: value[i]})
            print(population)
    return (population, world_size)

I'd like to change my list with lists to a list with tuples


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use tuple():

[tuple(x) for x in lst]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...