Python 向列表中添加值并将类型更改为int
我正在抓取一个页面,它有时会给我一个包含5个值的列表,有时会小于5个值。我需要格式化这些列表,使它们都有5个值,都是整数。这些列表稍后将添加到JSON中,因此它们需要遵循相同的模式 但我不能这么做。值未按预期更改为整数 示例和期望值Python 向列表中添加值并将类型更改为int,python,Python,我正在抓取一个页面,它有时会给我一个包含5个值的列表,有时会小于5个值。我需要格式化这些列表,使它们都有5个值,都是整数。这些列表稍后将添加到JSON中,因此它们需要遵循相同的模式 但我不能这么做。值未按预期更改为整数 示例和期望值 scraped alement: all_lst = [['\n 35,726\n ', '\n 61\n
scraped alement:
all_lst = [['\n 35,726\n ',
'\n 61\n ',
8764,
'\n 11,756\n ',
'\n 3,417\n '],
['\n 185,620\n ',
'\n 116\n ',
41823]]
expected result:
all_lst = [[35726, 61, 8764, 11756, 3417], [185620, 116, 41823, 185620, 116]]
for lst in all_lst:
if len(lst) == 5:
for i in range(5):
if type(lst[i]) == str:
lst[i] = int(lst[i].replace(' ','').replace('\n','').replace(',',''))
else:
lst[i] = lst[i]
else:
lst = list(islice(cycle(lst), 5))
for i in range(5):
if type(lst[i]) == str:
lst[i] = int(lst[i].replace(' ','').replace('\n','').replace(',',''))
else:
lst[i] = lst[i]
我尝试过的
scraped alement:
all_lst = [['\n 35,726\n ',
'\n 61\n ',
8764,
'\n 11,756\n ',
'\n 3,417\n '],
['\n 185,620\n ',
'\n 116\n ',
41823]]
expected result:
all_lst = [[35726, 61, 8764, 11756, 3417], [185620, 116, 41823, 185620, 116]]
for lst in all_lst:
if len(lst) == 5:
for i in range(5):
if type(lst[i]) == str:
lst[i] = int(lst[i].replace(' ','').replace('\n','').replace(',',''))
else:
lst[i] = lst[i]
else:
lst = list(islice(cycle(lst), 5))
for i in range(5):
if type(lst[i]) == str:
lst[i] = int(lst[i].replace(' ','').replace('\n','').replace(',',''))
else:
lst[i] = lst[i]
现在的输出是
[[3572618764117563417],'\n 185620\n','\n 116\n',41823]您可以使用这样的嵌套列表理解:
[[int(str(i).replace(',', '')) for i in islice(cycle(l), 5)] for l in all_lst]
这将返回:
[[35726, 61, 8764, 11756, 3417], [185620, 116, 41823, 185620, 116]]
您可以使用如下嵌套列表:
[[int(str(i).replace(',', '')) for i in islice(cycle(l), 5)] for l in all_lst]
这将返回:
[[35726, 61, 8764, 11756, 3417], [185620, 116, 41823, 185620, 116]]
您唯一的问题是在将lst用作迭代器的同时更改它。你正在砍你坐的树枝。相反,初始化一个空列表并将您的项目添加到其中
lst_out = list()
for lst in all_lst:
if len(lst) == 5:
for i in range(5):
if type(lst[i]) == str:
lst[i] = int(lst[i].replace(' ','').replace('\n','').replace(',',''))
else:
lst = list(islice(cycle(lst), 5))
for i in range(5):
if type(lst[i]) == str:
lst[i] = int(lst[i].replace(' ','').replace('\n','').replace(',',''))
lst_out.append(lst)
print(lst_out)
编辑:已删除的
list[i]=list[i]
,没有任何效果。您唯一的问题是在将lst用作迭代器的同时更改它。你正在砍你坐的树枝。相反,初始化一个空列表并将您的项目添加到其中
lst_out = list()
for lst in all_lst:
if len(lst) == 5:
for i in range(5):
if type(lst[i]) == str:
lst[i] = int(lst[i].replace(' ','').replace('\n','').replace(',',''))
else:
lst = list(islice(cycle(lst), 5))
for i in range(5):
if type(lst[i]) == str:
lst[i] = int(lst[i].replace(' ','').replace('\n','').replace(',',''))
lst_out.append(lst)
print(lst_out)
编辑:已删除
list[i]=list[i]
,但没有效果。发生了什么(输出,完整错误消息)?编辑问题以显示它。添加到@MichaelButscherWhat发生的帖子(输出,完整错误消息)?编辑问题以显示它。添加到@MichaelButscheryPost后,您的代码给出了正确的输出,但解释完全错误。OP的问题是else
块中的代码使用list
构造函数创建了一个新列表,但随后只修改新列表而不是原始列表。您的代码给出了正确的输出,但解释是错误的。OP的问题是else
块中的代码使用list
构造函数创建了一个新列表,但随后只修改新列表而不是原始列表。