如何将列表的字符串组合成一个元素?

2024-09-30 23:36:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个文件,里面有几行动物的名字,后面是这样的数字:

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第一学期的学生,如果答案很明显,我很抱歉。你知道吗


Tags: 文件元素列表数字名字动物groundfox
3条回答

如果你想玩你的列表和一些操作(切片,加入等等),你可以这样做:

animals = [['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']]

sliceObj = slice(0, 2)
delimiter = ' '
animalsNew=[]
for animal in animals:
    subanimalArray=animal[sliceObj]
    arrayEnd=animal[2:]

    animalName = delimiter.join(subanimalArray)

    arrayEnd.insert(0, animalName)
    print "animalsNew:",' ; '.join(arrayEnd)
    animalsNew.append(arrayEnd)

例如,在浏览器中使用此代码段。 它是基于skulpt的:

因为您评论说您可以控制文件的格式,所以将其更改为正确的CSV格式(带或不带标题)要比使用定制的临时解决方案容易得多。你知道吗

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

那你要做的就是

import csv

with open('test_files/test.csv') as f:
    lines = list(csv.reader(f))

print(lines)

#  [['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']]

如果不想将文件更改为csv格式,可以定义一个函数,如果字符串不能转换为float(表示它不是数字),则返回True:

def is_string(string):
    try:
        float(string)
        return False
    except ValueError:
        return True

然后:

# The list of lists:
lst = [['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'] ]

for animal in lst:
    animalname = ''
    for item in animal:
        if is_string(item):
            animalname += item + ' '
        else:
            break;
    print animalname.rstrip(' ')

这将为您提供:

African elephant
African giant pouched rat
Arctic Fox
Arctic ground squirrel

相关问题 更多 >