Python For 循环仅将最后一个值附加到列表 [英] Python For Loop Appending Only Last Value to List

查看:40
本文介绍了Python For 循环仅将最后一个值附加到列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个循环将一个列表的值设置为另一个列表的值,虽然我能够用我当前的代码实现这一点,但它只是将最后一组值附加到我的列表中.我的设置中可能有什么可能导致这种行为?

I have a loop that is setting the values of one list to the value of another and while I am able to achieve this with my current code it is only appending the last grouping of values to my list. What could possibly be off in my setup that could cause this behavior?

这是我的数据格式:

print(df_full_raw_format) # Data Set being set to worksheet_range values

<class 'pandas.core.frame.DataFrame'>
     b_clicks  b_cpc  date_string
0    b_clicks  b_cpc  date_string
1          72   2.43   2018-01-01
2         232    2.8   2018-01-02
3         255    2.6   2018-01-03
4         249   2.65   2018-01-04
5         202   2.86   2018-01-05

这是我的 worksheet_range 格式:

Here is my worksheet_range format:

[<Cell R1C1 ''>, <Cell R2C1 ''>, <Cell R3C1 ''>,....]

这是我的 for 循环函数:

Here is my function with the for loops:

update_raw_data_sheet(df_full_raw_format)

def update_raw_data_sheet(data_set):
    for col_val, col_name in zip(gs_columns, columns):
        updated_values = [] # List storing the combined list values
        column_data = data_set[col_name].values.tolist(); # col_name = ['b_clicks', 'b_cpc', 'date_string']
        print("COLUMN DATA")
        print(column_data)
        worksheet_range = worksheet.range(1, col_val, len(column_data), col_val); # [row_start, col_start, row_end, col_end]
        print(worksheet_range)
        for cell, data in zip(worksheet_range, column_data):
            cell.value = data # <Cell R1C1 '2018-01-01'>...
            print(cell)
            updated_values.append(cell)

    print(updated_values)

这是控制台:

(Loop 1)
COLUMN DATA
['date_string', '2018-01-01', '2018-01-02', '2018-01-03', ...] # print(column_data)
[<Cell R1C1 ''>, <Cell R2C1 ''>, <Cell R3C1 ''>,...] # print(worksheet_range)
<Cell R1C1 'date_string'> # print(cell)
<Cell R2C1 '2018-01-01'>
<Cell R3C1 '2018-01-02'>
<Cell R4C1 '2018-01-03'>
... (Loop 2)
COLUMN DATA
['b_clicks', '72', '232', '255', ...]
[<Cell R1C2 ''>, <Cell R2C2 ''>, <Cell R3C2 '', ...]
<Cell R1C2 'b_clicks'>
<Cell R2C2 '72'>
<Cell R3C2 '232'>
... (Loop 3)
COLUMN DATA
['b_cpc', 2.43, 2.8,...]
[<Cell R1C3 'b_cpc'>, <Cell R2C3 '2.43'>, <Cell R3C3 '2.8'>, ...]
<Cell R1C3 'b_cpc'>
<Cell R2C3 2.43>
<Cell R3C3 2.8>
(Post-Loop)
[<Cell R1C3 'b_cpc'>, <Cell R2C3 2.43>, <Cell R3C3 2.8>, ...] # print(updated_values)

它错过了前两个循环的值,但完美地捕获了数组中的第三个.

It misses the first two loops worth of values, but perfectly captures the 3rd in the array.

推荐答案

updated_values = []

应该在 def 语句之后的循环外,否则你会在每次迭代中覆盖添加到列表中的值.

should be outside loop just after def statement, otherwise you overwrite values added to the list in every iteration.

def update_raw_data_sheet(data_set):
    updated_values = []
    # ...

这篇关于Python For 循环仅将最后一个值附加到列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆