Python 如何将列表的字符串组合成一个元素?
我有一个文件,上面有几行动物的名字,后面跟着这样的数字:Python 如何将列表的字符串组合成一个元素?,python,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
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第一学期的学生,如果答案显而易见,我很抱歉 由于您评论说您可以控制文件的格式,因此将其更改为正确的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格式(带或不带标题)将比使用自定义的临时解决方案容易得多
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格式,可以定义一个函数,如果字符串不能转换为浮点(表示它不是数字),则该函数将返回True: 然后: 这将为您提供:
African elephant
African giant pouched rat
Arctic Fox
Arctic ground squirrel
如果不想将文件更改为csv格式,可以定义一个函数,如果字符串不能转换为浮点(表示它不是数字),则该函数将返回True: 然后: 这将为您提供:
African elephant
African giant pouched rat
Arctic Fox
Arctic ground squirrel
如果你想玩你的列表和一些操作(切片、加入等),你可以这样做:
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的:
//输出功能是可配置的。这个只是附加了一些文本
//到一个前置元素。
功能输出(文本){
var mypre=document.getElementById(“输出”);
mypre.innerHTML=mypre.innerHTML+文本;
}
函数内置读取(x){
if(Sk.builtinFiles===未定义| | Sk.builtinFiles[“文件”][x]==未定义)
抛出“找不到文件:”“+x+””;
返回Sk.builtinFiles[“files”][x];
}
//以下是在skulpt中运行python程序所需的所有信息
//从文本区域获取代码
//获取输出前元素的引用
//配置输出功能
//调用Sk.importMainWithBody()命令
函数runit(){
var prog=document.getElementById(“yourcode”).value;
var mypre=document.getElementById(“输出”);
mypre.innerHTML='';
Sk.pre=“输出”;
configure({output:outp,read:builtinRead});
(Sk.TurtleGraphics | |(Sk.TurtleGraphics={})).target='mycanvas';
var myPromise=Sk.misceval.asyncToPromise(函数(){
返回Sk.importMainWithBody(“”,false,prog,true);
});
myPromise.then(函数(mod){
console.log('success');
},
功能(err){
log(err.toString());
});
}
试试这个
动物=[“非洲”、“大象”、“6654.000”、“5712.000”、“-999.0”、“-999.0”、“3.3”、“38.6”、“645.0”、“3”、“5”、“3”],
['African','giant','Pocked','rat','1.000','6.600','6.3','2.0','8.3','4.5','42.0','3','1','3'],
[‘北极’、‘狐狸’、‘3.385’、‘44.500’、‘999.0’、‘999.0’、‘12.5’、‘14.0’、‘60.0’、‘1’、‘1’、‘1’],
[‘北极’、‘地面’、‘松鼠’、‘0.920’、‘5.700’、‘999.0’、‘999.0’、‘16.5’、‘999.0’、‘25.0’、‘5’、‘2’、‘3’]
切片对象j=切片(0,2)
分隔符=''animalsNew=[]
对于动物中的动物:
亚动物阵列=动物[J]
arrayEnd=动物[2:]
animalName=分隔符.join(子animalarray)
arrayEnd.insert(0,animalName)
打印“animalsNew:”、“;”。加入(arrayEnd)
animalsNew.append(arrayEnd)
跑
如果你想玩你的列表和一些操作(切片、加入等),你可以这样做:
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的:
//输出功能是可配置的。这个只是附加了一些文本
//到一个前置元素。
功能输出(文本){
var mypre=document.getElementById(“输出”);
mypre.innerHTML=mypre.innerHTML+文本;
}
函数内置读取(x){
if(Sk.builtinFiles===未定义| | Sk.builtinFiles[“文件”][x]==未定义)
抛出“找不到文件:”“+x+””;
返回Sk.builtinFiles[“files”][x];
}
//以下是在skulpt中运行python程序所需的所有信息
//从文本区域获取代码
//获取输出前元素的引用
//配置输出功能
//调用Sk.importMainWithBody()命令
函数runit(){
var prog=document.getElementById(“yourcode”).value;
var mypre=document.getElementById(“输出”);
mypre.innerHTML='';
Sk.pre=“输出”;
configure({output:outp,read:builtinRead});
(Sk.TurtleGraphics | |(Sk.TurtleGraphics={})).target='mycanvas';
var myPromise=Sk.misceval.asyncToPromise(函数(){
返回Sk.importMainWithBody(“”,false,prog,true);
});
myPromise.then(函数(mod){
console.log('success');
},
功能(err){
log(err.toString());
});
}
试试这个
动物=[“非洲”、“大象”、“6654.000”、“5712.000”、“-999.0”、“-999.0”、“3.3”、“38.6”、“645.0”、“3”、“5”、“3”],
['African','giant','Pocked','rat','1.000','6.600','6.3','2.0','8.3','4.5','42.0','3','1','3'],
[‘北极’、‘狐狸’、‘3.385’、‘44.500’、‘999.0’、‘999.0’、‘12.5’、‘14.0’、‘60.0’、‘1’、‘1’、‘1’],
[‘北极’、‘地面’、‘松鼠’、‘0.920’、‘5.700’、‘999.0’、‘999.0’、‘16.5’、‘999.0’、‘25.0’、‘5’、‘2’、‘3’]
切片对象j=切片(0,2)
分隔符=“”
animalsNew=[]
对于动物中的动物:
亚动物阵列=动物[J]
arrayEnd=动物[2:]
animalName=分隔符.join(子animalarray)
arrayEnd.insert(0,animalName)
打印“animalsNew:”、“;”。加入(arrayEnd)
animalsNew.append(arrayEnd)
跑
如果您的输入文件是.txt中的,则