python - 在 Python 中迭代浮点列表
问题描述
我正在尝试使用 for 循环遍历浮点值列表,但出现错误。我正在尝试使用 enumerate() 打印列表。有没有更好的方法来迭代浮动列表?
该列表有数字list_float= [1546626931.138813,1546626931.138954,1546626931.139053,1546626931.139289,.......,1546628078.463885]
代码是:
import csv
import datetime
import pandas
filename = open('C:\\Users\\xyz\\Downloads\\BLF File\\output.csv', "w")
log = can.BLFReader('C:\\Users\\xyz\\Downloads\\BLF File\\test.blf')
# print ("We are here!")
log_output = []
timestamp = [] #Variable to store timestamps from blf file
time_del = [] #Variable to store time difference
# filename.write('Timestamp ID DLC Channel' + '\n')
print('We are here 1')
for time in log:
time = str(time).split()
timestamp = float(time[1])
# print(timestamp)
for count, item in enumerate(timestamp):
print(count, item)
它只打印列表中的最后一个浮点值,即“ 1546628078.463885 ”,如下所示:
0 1
1 5
2 4
3 6
4 6
5 2
6 8
7 0
8 7
9 8
10 .
11 4
12 6
13 3
14 8
15 8
16 5
在我传递的文件中,值如下:
['Timestamp:', '1546626926.380693', 'ID:', '0366', 'S', 'DLC:', '8', '25', '80', 'f8', '7f', '00', '00', '00', '00', 'Channel:', '0']
['Timestamp:', '1546626926.381285', 'ID:', '0120', 'S', 'DLC:', '2', '00', '05', 'Channel:', '1']
解决方案
timestamp
这个循环每次都会覆盖,所以最后timestamp
是一个浮点数而不是一个浮点数列表。
for time in log:
time = str(time).split()
timestamp = float(time[1])
你可能想要:
for time in log:
time = str(time).split()
timestamp.append(float(time[1]))
推荐阅读
- charts - 可变变量 + 安全函数 - tradingview pine 错误
- sql - 是否有任何可以在一个语句中更新或插入的 RDBMS?
- php - PHP中的Bind_Param函数从Uwamp返回错误
- matlab - 如何单独循环和处理目录中的文件?
- html - 如何使用 css 或 jquery 动态交换引导列
- r - R从Yahoo Finance刮掉不同的文本行
- python - Selecting strings from a list of strings having same value
- flutter - 来自 API 请求的错误“在 null 上调用了方法 '[]'。接收方:null 尝试调用:[](0)”更新
- laravel - Laravel api 返回数据格式
- sql-server - 如何通过关闭获得带有顺序的用户ID