我有一个文件,其中包含几行动物名称,后跟这样的数字:
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学生,所以如果答案很明显,我会道歉。
答案 0 :(得分:2)
由于您评论说您可以控制文件的格式,因此将其更改为正确的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']]
答案 1 :(得分:0)
如果您不想将文件更改为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
答案 2 :(得分:0)
如果您想使用列表和一些操作(切片,加入等等),您可以这样做:
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 :
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js" type="text/javascript"></script>
<script src="http://www.skulpt.org/static/skulpt.min.js" type="text/javascript"></script>
<script src="http://www.skulpt.org/static/skulpt-stdlib.js" type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">
// output functions are configurable. This one just appends some text
// to a pre element.
function outf(text) {
var mypre = document.getElementById("output");
mypre.innerHTML = mypre.innerHTML + text;
}
function builtinRead(x) {
if (Sk.builtinFiles === undefined || Sk.builtinFiles["files"][x] === undefined)
throw "File not found: '" + x + "'";
return Sk.builtinFiles["files"][x];
}
// Here's everything you need to run a python program in skulpt
// grab the code from your textarea
// get a reference to your pre element for output
// configure the output function
// call Sk.importMainWithBody()
function runit() {
var prog = document.getElementById("yourcode").value;
var mypre = document.getElementById("output");
mypre.innerHTML = '';
Sk.pre = "output";
Sk.configure({output:outf, read:builtinRead});
(Sk.TurtleGraphics || (Sk.TurtleGraphics = {})).target = 'mycanvas';
var myPromise = Sk.misceval.asyncToPromise(function() {
return Sk.importMainWithBody("<stdin>", false, prog, true);
});
myPromise.then(function(mod) {
console.log('success');
},
function(err) {
console.log(err.toString());
});
}
</script>
<h3>Try This</h3>
<form>
<textarea id="yourcode" cols="80" rows="20">
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)
</textarea><br />
<button type="button" onclick="runit()">Run</button>
</form>
<pre id="output" ></pre>
<!-- If you want turtle graphics include a canvas -->
<div id="mycanvas"></div>
</body>
</html>
&#13;
答案 3 :(得分:0)
如果您的输入文件是in.txt
,则可以使用。
f = open('in.txt')
out = []
for line in f:
l = line.split()
wordlist = []
numlist = []
for word in l:
if word.isalpha():
wordlist.append(word)
else:
numlist.append(word)
numlist.insert(0, ' '.join(wordlist))
out.append(numlist)
print out