如何在Python中将列表类型转换为数字类型

发布于 2024-10-29 23:57:22 字数 388 浏览 1 评论 0原文

我有一个外部文件,它是一列浮点数(希望将来能够将其扩展为多列)。我想将其导入到数组中。该文件是 cvs 文件,但也可以是 txt 文件。

我正在使用:

reader = csv.reader(open("e=0.6.csv"))
table = []
for row in reader:
    values = []    
    for col in row: 
        values.append(float(col))
    table.aapend(values)

结果是一个列表 [[1.0], [0.98], ......] 我想将列表转换为数字数组 (1.0, 0.98, ...) 我该怎么做?

I have a external file that is a single column of floating point numbers ( want to be able to expand it to have multiple columns in the future). I want to import it into an array. The file is a cvs file but it just as well could be a txt file.

I am using:

reader = csv.reader(open("e=0.6.csv"))
table = []
for row in reader:
    values = []    
    for col in row: 
        values.append(float(col))
    table.aapend(values)

The result is a list [[1.0], [0.98], ......]
I want to convert the List to an array of numbers (1.0, 0.98, ...)
How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

节枝 2024-11-05 23:57:22
reader = csv.reader(open("e=0.6.csv"))
table = tuple(float(col) for row in reader for col in row)
reader = csv.reader(open("e=0.6.csv"))
table = tuple(float(col) for row in reader for col in row)
递刀给你 2024-11-05 23:57:22

为什么要创建一个名为值的新列表?为什么不附加到表中呢? IE

reader = csv.reader(open("e=0.6.csv"))
table = []
for row in reader: 
    for col in row: 
        table.append(float(col))

Why do you make a new list called values? Why not append to table instead? i.e.

reader = csv.reader(open("e=0.6.csv"))
table = []
for row in reader: 
    for col in row: 
        table.append(float(col))
丘比特射中我 2024-11-05 23:57:22

(1.0, 0.98, ...) 是一个元组。这是从中创建元组的一种方法:

>>> l = [[1.0], [0.98]]
>>> tuple(i[0] for i in l)
(1.0, 0.97999999999999998)

(1.0, 0.98, ...) is a tuple. Here's one way to make a tuple from that:

>>> l = [[1.0], [0.98]]
>>> tuple(i[0] for i in l)
(1.0, 0.97999999999999998)
云仙小弟 2024-11-05 23:57:22
reader = csv.reader(open("e=0.6.csv"))
table = []
for row in reader:
    for col in row: 
        table.aapend(float(col))
reader = csv.reader(open("e=0.6.csv"))
table = []
for row in reader:
    for col in row: 
        table.aapend(float(col))
℉絮湮 2024-11-05 23:57:22

使用列表理解:

>>> l = [[0.1], [0.2], [0.3]]
>>> [x[0] for x in l]
[0.1, 0.2, 0.3]

或者映射函数:

>>> map(lambda x:x[0], l)
[0.1, 0.2, 0.3]

或者化简函数:

>>> reduce(lambda x,y: x+y, l, [])
[0.1, 0.2, 0.3]

或者生成器:

>>> (x[0] for x in l)
<generator object <genexpr> at 0x1004e00f0>

或者直接将数字存储在列表中。

Use a list comprehension:

>>> l = [[0.1], [0.2], [0.3]]
>>> [x[0] for x in l]
[0.1, 0.2, 0.3]

Or the map function:

>>> map(lambda x:x[0], l)
[0.1, 0.2, 0.3]

Or the reduce function:

>>> reduce(lambda x,y: x+y, l, [])
[0.1, 0.2, 0.3]

Or a generator:

>>> (x[0] for x in l)
<generator object <genexpr> at 0x1004e00f0>

Or just store your numbers directly in a list.

瀟灑尐姊 2024-11-05 23:57:22

在你的具体情况下,我会跳过这一步,只做类似的事情:

reader = csv.reader(open("e=0.6.csv"))

table = [float(col) for col in row for row in reader]

但是,如果你想使用原来的答案,你可以这样做:

使用 映射方法

>>> l = [[1.0], [0.98], [3.0]]
>>> map(lambda x: x[0], l)
[1.0, 0.97999999999999998, 3.0]

或列表理解:

>>> [e for subl in l for e in subl]
[1.0, 0.97999999999999998, 3.0]

无论哪种情况,您都可以通过执行 tuple(result) 将返回的列表转换为元组

In your specific case, I'd skip this step and just do something like:

reader = csv.reader(open("e=0.6.csv"))

table = [float(col) for col in row for row in reader]

However if you wanted to go with your original answer you can do:

Use the map method:

>>> l = [[1.0], [0.98], [3.0]]
>>> map(lambda x: x[0], l)
[1.0, 0.97999999999999998, 3.0]

Or list comprehension:

>>> [e for subl in l for e in subl]
[1.0, 0.97999999999999998, 3.0]

In either case, you can convert the list returned into a tuple by just doing tuple(result)

萌面超妹 2024-11-05 23:57:22

我不明白您为什么在这种情况下使用 csv.reader 。你自己说过这不是真正的 csv!只需执行以下任一操作:

map(float, open("e=0.6.csv").readlines())

[float(x) for x in open("e=0.6.csv").readlines()]

I don't understand why you're using csv.reader in this case. You said yourself that it wasn't really a csv! Just do either:

map(float, open("e=0.6.csv").readlines())

or

[float(x) for x in open("e=0.6.csv").readlines()]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文