Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中使用三种类型的列表创建嵌套列表?_Python_List_Nested - Fatal编程技术网

如何在python中使用三种类型的列表创建嵌套列表?

如何在python中使用三种类型的列表创建嵌套列表?,python,list,nested,Python,List,Nested,我有三份清单: EX_Num = [1.0, 2.0, 3.0] Height_str = ['tall', 'medium', 'short'] total_carbon = [8.425169446611104, 8.917085904771866, 6.174348482965436] 如何在column1=EX\u Num、column2=Height\u str和column3=total\u carbon的位置创建嵌套列表?不清楚您在问什么。。。但是如果你只是想要一个列表,只需要附

我有三份清单:

EX_Num = [1.0, 2.0, 3.0]
Height_str = ['tall', 'medium', 'short']
total_carbon = [8.425169446611104, 8.917085904771866, 6.174348482965436]

如何在
column1=EX\u Num
column2=Height\u str
column3=total\u carbon
的位置创建嵌套列表?

不清楚您在问什么。。。但是如果你只是想要一个列表,只需要附加它们

>>> nested_list.append(EX_Num)
>>> nested_list.append(Height_str)
>>> nested_list.append(total_carbon)
>>> nested_list
[[1.0, 2.0, 3.0], ['tall', 'medium', 'short'], [8.425169446611104, 8.917085904771866, 6.174348482965436]]
如果要将所有值放在一个列表中,只需将它们串联起来即可

>>> nested_list = EX_Num + Height_str + total_carbon
>>> nested_list
[1.0, 2.0, 3.0, 'tall', 'medium', 'short', 8.425169446611104, 8.917085904771866, 6.174348482965436]
如果你需要不同的东西,你需要让你的问题更清楚。]

根据评论进行编辑:

如果您知道所有列表的长度相同:

nested_list = []
# be careful, if the lists aren't all the same length, you will get errors
for x in range(len(EX_Num)):
    tmp = [EX_Num[x], Height_str[x], total_carbon[x]]
    nested_list.append(tmp)
您可以使用内置函数将多个ITerable连接到一个元组中:

ex = [1.0, 2.0, 3.0]
height = ['tall', 'medium', 'short']
total_carbon = [8.4, 8.9, 6.1]

joint = zip(ex, height, total_carbon)
print(joint)

# [(1.0, 'tall', 8.4), (2.0, 'medium', 8.9), (3.0, 'short', 6.1)]
注意:请不要对变量名使用“匈牙利符号”(
\u num
\u str
,等等)——它们只是杂乱无章的,特别是在Python这样的动态语言中


如果需要将相关数据分组在一起,那么为其创建一个简单的容器类型更具可读性。这很好:

from collections import namedtuple
Thing = namedtuple('Thing', ['ex', 'height', 'total_carbon'])
my_things = [Thing(*t) for t in zip(ex, height, total_carbon)]

现在,您可以按名称引用内容—例如,
my_things[0]。高度
—而无需记住每个属性的索引位置。

使用哪种语言?请阅读并回答。这个问题不清楚,但如果您确实在使用python,分号是不必要的,不应该使用。我假设列表3末尾的句点是o型,但如果不是,那就是语法错误。@jdLemon,谢谢你的回答。很抱歉,我没有把我的问题说清楚。我想做一个这样的最后列表:[[1.0',高,8.425169446611104,],[2.0',中,8.917085904771866],[3.0',短,6.174348482965436]。我已经编辑了我的答案。您应该编辑您的问题,以反映您在评论中发布的答案。@jdfLemon不要使用
for
循环将内容配对<代码>邮政编码为您完成。