如何从字符串列表创建列表? [英] How can I create lists from a list of strings?

查看:65
本文介绍了如何从字符串列表创建列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串列表,例如:

I have a list of strings such as:

names = ['apple','orange','banana'] 

并且我想为列表中的每个元素创建一个列表,该列表的名称将与字符串完全相同:

And I would like to create a list for each element in the list, that would be named exactly as the string:

apple = []  
orange = []  
banana = []  

我如何在 Python 中做到这一点?

How can I do that in Python?

推荐答案

你可以通过创建一个 dict 来做到这一点:

You would do this by creating a dict:

fruits = {k:[] for k in names}

然后通过(例如:)fruits['apple'] 访问每个 - 您不想走单独变量的道路!

Then access each by (for eg:) fruits['apple'] - you do not want to go down the road of separate variables!

这篇关于如何从字符串列表创建列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆