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

查看:89
本文介绍了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天全站免登陆