使用来自一个列表的值在另一个列表中

huangapple go评论64阅读模式
英文:

Locust/Python and using values from one list in another list

问题

我想要使用列表1中的值来更新列表2中的id-value(这里为10017)。
如何实现这个目标?

英文:

I have list 1:

TS_IDs = [
    '10158',
    '10159',
    '10174'
]

and list 2:

values = [
    {
        "id": 10017,
        "values": [
            {
                "from": "2022-10-30T12:00:00Z",
                "to": "2022-10-30T13:00:00Z",
                "value": 15
            },
            {
                "from": "2022-10-30T13:00:00Z",
                "to": "2022-10-30T14:00:00Z",
                "value": 16
            }
      ]
    }
]

I want to use values from list 1 into list 2 for the id-value (here being 10017).

How can that be obtained?

答案1

得分: 0

You are most likely looking for random.choice() assuming you want to assign a randomly selected item from TS_IDs to each item in values.

import random
import copy

## ---------------------------
## Just so we don't mutate values
## ---------------------------
values_new = copy.deepcopy(values)
## ---------------------------

for item in values_new:
    item["id"] = random.choice(TS_IDs)

print(values_new)

However, if you want to assign the ids randomly without duplication (assuming the length of values is less than the length of TS_IDs) you might use random.sample() or random.shuffle() on TS_IDs.

import random
import copy

## ----------------------
## get a random ordering of TS_IDs.
## random.sample() works as well but mutates TS_IDs
## ----------------------
TS_IDs_new = random.sample(TS_IDs, len(TS_IDs))
## ----------------------

## ---------------------------
## Just so we don't mutate values
## ---------------------------
values_new = copy.deepcopy(values)
## ---------------------------

for index, item in enumerate(values_new):
    item["id"] = TS_IDs_new[index]

print(values_new)

If you want to just use TS_IDs in order then:

import random
import copy

## ---------------------------
## Just so we don't mutate values
## ---------------------------
values_new = copy.deepcopy(values)
## ---------------------------

for index, item in enumerate(values_new):
    item["id"] = TS_IDs[index]

print(values_new)

Note, you might want to place a guard against values having more items than TS_IDs in the last two cases.

英文:

You are most likely looking for random.choice() assuming you want to assign a randomly selected item from TS_IDs to each item in values.

import random
import copy

## ---------------------------
## Just so we don't mutate values
## ---------------------------
values_new = copy.deepcopy(values)
## ---------------------------

for item in values_new:
    item["id"] = random.choice(TS_IDs)

print(values_new)

However, if you want to assign the ids randomly without duplication (assuming the length of values is less than the length of TS_IDs) you might use random.sample() or random.shuffle() on TS_IDs.

import random
import copy

## ----------------------
## get a random ordering of TS_IDs.
## random.sample() works as well but mutates TS_IDs
## ----------------------
TS_IDs_new = random.sample(TS_IDs, len(TS_IDs))
## ----------------------

## ---------------------------
## Just so we don't mutate values
## ---------------------------
values_new = copy.deepcopy(values)
## ---------------------------

for index, item in enumerate(values_new):
    item["id"] = TS_IDs_new[index]

print(values_new)

If you want to just use TS_IDs in order then:

import random
import copy

## ---------------------------
## Just so we don't mutate values
## ---------------------------
values_new = copy.deepcopy(values)
## ---------------------------

for index, item in enumerate(values_new):
    item["id"] = TS_IDs[index]

print(values_new)

Note, you might want to place a guard against values having more items than TS_IDs in the last two cases.

huangapple
  • 本文由 发表于 2023年6月29日 21:08:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76581380.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定