我有一个文件,里面有几行动物的名字,后面是这样的数字:
African elephant 6654.000 5712.000 -999.0 -999.0 3.3 38.6 645.0 3 5 3
African giant pouched rat 1.000 6.600 6.3 2.0 8.3 4.5 42.0 3 1 3
Arctic Fox 3.385 44.500 -999.0 -999.0 12.5 14.0 60.0 1 1 1
Arctic ground squirrel .920 5.700 -999.0 -999.0 16.5 -999.0 25.0 5 2 3
Asian elephant 2547.000 4603.000 2.1 1.8 3.9 69.0 624.0 3 5 4
Baboon 10.550 179.500 9.1 .7 9.8 27.0 180.0 4 4 4
.
.
.
我有一个数据列表,如下所示:
[['African', 'elephant', '6654.000', '5712.000', '-999.0', '-999.0', '3.3', '38.6', '645.0', '3', '5', '3'],
['African', 'giant', 'pouched', 'rat', '1.000', '6.600', '6.3', '2.0', '8.3', '4.5', '42.0', '3', '1', '3'],
['Arctic', 'Fox', '3.385', '44.500', '-999.0', '-999.0', '12.5', '14.0', '60.0', '1', '1', '1'],
['Arctic', 'ground', 'squirrel', '.920', '5.700', '-999.0', '-999.0', '16.5', '-999.0', '25.0', '5', '2', '3'], ... ]
但我需要每个动物的名字都有自己的元素,比如:
[['African elephant', '6654.000', '5712.000', '-999.0', '-999.0', '3.3', '38.6', '645.0', '3', '5', '3'],
['African giant pouched rat', '1.000', '6.600', '6.3', '2.0', '8.3', '4.5', '42.0', '3', '1', '3'],
['Arctic Fox', '3.385', '44.500', '-999.0', '-999.0', '12.5', '14.0', '60.0', '1', '1', '1'],
['Arctic ground squirrel', '.920', '5.700', '-999.0', '-999.0', '16.5', '-999.0', '25.0', '5', '2', '3']...]
有没有办法在列表中循环并将动物名称的每个字符串组合成一个元素?你知道吗
我是Python第一学期的学生,如果答案很明显,我很抱歉。你知道吗
如果你想玩你的列表和一些操作(切片,加入等等),你可以这样做:
例如,在浏览器中使用此代码段。 它是基于skulpt的:
因为您评论说您可以控制文件的格式,所以将其更改为正确的CSV格式(带或不带标题)要比使用定制的临时解决方案容易得多。你知道吗
那你要做的就是
如果不想将文件更改为csv格式,可以定义一个函数,如果字符串不能转换为float(表示它不是数字),则返回True:
然后:
这将为您提供:
相关问题 更多 >
编程相关推荐