我有什么可能是一个基本问题,但它给我带来了一些困难,我认为有人可能会帮助我。
我的问题与列表推导有关。我的数据看起来像这样,我想要做的是将1,2和3列转换为整数。
['airport', '2007', '175702', '32788']
['airport', '2008', '173294', '31271']
['request', '2005', '646179', '81592']
['request', '2006', '677820', '86967']
['request', '2007', '697645', '92342']
['request', '2008', '795265', '125775']
['wandered', '2005', '83769', '32682']
['wandered', '2006', '87688', '34647']
['wandered', '2007', '108634', '40101']
['wandered', '2008', '171015', '64395']
我尝试了以下代码:
test = [map(int,x) for x in raw_csv]
但是当我运行它并打印结果时,我得到一个空列表。如果有人知道怎么做,我真的很感谢你告诉我如何做。谢谢你的时间。
答案 0 :(得分:2)
我会写一个简单的生成器:
def safe_ints(sequence):
for item in sequence
try:
yield int(item)
except ValueError:
yield item
test = [safe_ints(row) for row in rows]
编辑:正如OP想要的那样,如果转换失败,则按原样返回项目。
或者只是简单的功能,适用于单个令牌:
def try_int(val):
try:
return int(val)
except ValueError:
return val
test = [map(try_int, row) for row in rows]
答案 1 :(得分:2)
我认为您的地图语法有点偏差。
In [2]: stuff = [['airport', '2007', '175702', '32788'],
['airport', '2008', '173294', '31271'],
['request', '2005', '646179', '81592'],
['request', '2006', '677820', '86967'],
['request', '2007', '697645', '92342'],
['request', '2008', '795265', '125775'],
['wandered', '2005', '83769', '32682'],
['wandered', '2006', '87688', '34647'],
['wandered', '2007', '108634', '40101'],
['wandered', '2008', '171015', '64395'],]
In [4]: map(lambda x: [x[0], int(x[1]), int(x[2]), int(x[3])], stuff)
Out[4]:
[['airport', 2007, 175702, 32788],
['airport', 2008, 173294, 31271],
['request', 2005, 646179, 81592],
['request', 2006, 677820, 86967],
['request', 2007, 697645, 92342],
['request', 2008, 795265, 125775],
['wandered', 2005, 83769, 32682],
['wandered', 2006, 87688, 34647],
['wandered', 2007, 108634, 40101],
['wandered', 2008, 171015, 64395]]
答案 2 :(得分:2)
如果您知道第一列将是非int并且以下列将是整数,并且这将保持不变,那就像
一样简单result = [line[:1] + map(int, line[1:]) for line in lines]
假设这种结构,这是我能想到的最简单的方法来获得你正在寻找的东西。
答案 3 :(得分:1)
如果您特别想要“除了第一列之外的所有列”,那么很难超越John Kiparsky's answer。如果你想要“所有可以转换的列应该是”,m.wasowski's try_int
是干净而简单的。
但如果你想要更通用的东西,可以稍后扩展到第1,2,3和7列:
int_columns = {1, 2, 3, 7}
test = [[int(x) if i in int_columns else x for i, x in enumerate(row)]
for row in raw_csv]
但我可能会将其重构为可重用的函数:
def map_some(func, iterable, columns):
return [func(x) if i in columns else x for i, x in enumerate(iterable)]
test = [map_some(int, row, int_columns) for row in raw_csv]
答案 4 :(得分:-1)
一个简单的方法:
l = [['airport', '2007', '175702', '32788'], ['airport', '2008', '173294', '31271'], ['request', '2005', '646179', '81592'], ['request', '2006', '677820', '86967'], ['request', '2007', '697645', '92342'], ['request', '2008', '795265', '125775'], ['wandered', '2005', '83769', '32682'], ['wandered', '2006', '87688', '34647'], ['wandered', '2007', '108634', '40101'], ['wandered', '2008', '171015', '64395']]
[ [x[0]]+map(int,x[1:]) for x in l ]
输出:
[['airport', 2007, 175702, 32788], ['airport', 2008, 173294, 31271], ['request', 2005, 646179, 81592], ['request', 2006, 677820, 86967], ['request', 2007, 697645, 92342], ['request', 2008, 795265, 125775], ['wandered', 2005, 83769, 32682], ['wandered', 2006, 87688, 34647], ['wandered', 2007, 108634, 40101], ['wandered', 2008, 171015, 64395]]