简体   繁体   English

如何将元素添加到“for”循环内的列表中?

[英]How to add elements to a list inside “for” loop?

Having a serious brain fart moment here because I could have sworn this code used to work properly before.这里有一个严重的大脑放屁时刻,因为我可以发誓这段代码以前可以正常工作。

Given the following:鉴于以下情况:

data = response.json()
for result in data['results']:
    for field in result['custom_fields']:
        if field['id'] == some_id_field:
            print(field['value'])

Returns:回报:

11111
22222
33333
44444
55555
66666
77777

However, I wanted to store the output as list in a separate variable to use later so I did this:但是,我想将 output 作为列表存储在一个单独的变量中以供以后使用,所以我这样做了:

data = response.json()
for result in data['results']:
    for field in result['custom_fields']:
        if field['id'] == some_id_field:
            var_for_later = field['value']
            var_for_later = var_for_later.splitlines()
            var_for_later = var_for_later

But instead, it only returns the first element in the array?但相反,它只返回数组中的第一个元素?

11111

Instead of ['11111','22222','33333','44444','55555','66666','77777']而不是['11111','22222','33333','44444','55555','66666','77777']

I could have sworn the above used to be the output until I wanted to test something and ran the code again today to see it only returned one value.我可以发誓上面曾经是 output 直到我想测试一些东西并今天再次运行代码,发现它只返回一个值。

Why is field['value'] now only storing the first element when assigned to a variable?为什么 field['value'] 现在只存储分配给变量的第一个元素?

You are assigning the value to a variable, not appending it to list.您正在将值分配给变量,而不是将其附加到列表中。 In order to store the values in list, you need to update your code to create an empty list before the for loop and append the field['value'] to list by using list.append() method.为了将值存储在列表中,您需要更新代码以在for循环和 append 使用list.append()方法列出要列出的field['value']之前创建一个空列表。 Hence, your code will become:因此,您的代码将变为:

data = response.json()

my_list = []  # creating empty list
for result in data['results']:
    for field in result['custom_fields']:
        if field['id'] == some_id_field:
            my_list.append(field['value'])  # Appending items to the list

which will return you the my_list as list in your desired format as:这将以您想要的格式返回my_list作为列表:

[11111, 22222, 33333, 44444, 55555, 66666, 77777]

Try this:尝试这个:

yourdata = []
data = response.json()
for result in data['results']:
    for field in result['custom_fields']:
        if field['id'] == some_id_field:
            yourdata.append(field['value'])

print(yourdata)

I guess you're pretty new to Python, so I suggest you to watch some tutorials or search for documentation on the internet.我猜你对 Python 很陌生,所以我建议你看一些教程或在互联网上搜索文档。 Eg w3schools例如w3schools

Why didn't your code work?为什么你的代码不起作用?

Have a look at this line: var_for_later = var_for_later It does absolutely nothing.看看这一行: var_for_later = var_for_later它什么也没做。 Also, you overwrite var_for_later in every iteration.此外,您会在每次迭代中覆盖 var_for_later。 So it should technically return '77777' and not '11111' in the end.所以它在技术上应该最终返回“77777”而不是“11111”。

Are you sure this is the output that corresponds to this code?您确定这是对应此代码的 output 吗?

Btw, do you really have multiple entrys with the same id?顺便说一句,您真的有多个具有相同 ID 的条目吗?

Earlier you were printing one field at a time, but then you started replacing the same variable in every loop which is why it jas only one value.早些时候,您一次打印一个字段,但随后您开始在每个循环中替换相同的变量,这就是为什么它只有一个值。 var_for_later only exists within that for loop. var_for_later仅存在于该 for 循环中。 In stead create your final list outside the loop and append values in every iteration.而是在循环外创建最终列表,并在每次迭代中创建 append 值。

out = []
data = response.json()
for result in data['results']:
    for field in result['custom_fields']:
        if field['id'] == some_id_field:
            out.append(field['value'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM