在Itertool中枚举2枚重复。产品引起数据python的问题
看来itertools.products
在这里写了以下方法的方式。我需要的是我需要的东西,但是数据的创建方式存在问题。
def replace_multiple_npi_lists(self):
conn = self.establish_connection()
list_ids = [] # list of unique list ids
new_npis = [] # list of new npis
df = pd.read_csv("./uploads/new_file.csv")[["NAME", "NPI_ID"]]
df = df.dropna()
if get_npi_lists := self.get_all_account_npi_lists(561939):
for name in get_npi_lists:
npi_list = {
"id": name["id"],
"name": name["name"],
}
list_ids.extend(
npi_list["id"]
for target in df["NAME"].unique()
if target == npi_list["name"]
)
for target in df["NAME"].unique():
new_list = df[df["NAME"] == target].reset_index(drop=True)
new_list = new_list["NPI_ID"].apply(lambda x: str(int(x))).to_list()
final_list = dict(npis=new_list)
new_npis.append(final_list)
try:
# loop through new_lists and send list to npis
for (i, npis), (i, id) in itertools.product(
enumerate(new_npis), enumerate(list_ids)
):
if id == list_ids[i]:
r = conn.put(
f"https://xxx.xxx.com/RestApi/v1/npi/npi-list/{id}",
json=npis,
)
r.raise_for_status()
if r.status_code == requests.codes.ok:
print(f"List #{i + 1} has been uploaded")
new_list = {"id": id, "npis": npis}
print(new_list)
time.sleep(5)
self.establish_connection()
except requests.exceptions.HTTPError:
print(r.json())
然后,终点在URL中接受ID
这样的这样:
{
'npis': [
122,
123,
...
]
}
我已经通过打印语句格式化了下面的格式化,以便我可以查看是否将正确的数字传递给正确的ID。
该方法有效,它可以在功能方面执行我想要的操作,但并不能创建我需要发送它们的方式。
list_ids
:是唯一IDS的列表new_npis
:是一个dicts的列表,列出了npi numbers
list_ids
[4151, 8785, 8786]
new_npis,
[{'npis': ['3099994294', '1430739187', '5968165218']}, {'npis': ['3559958528', '2502671659', '7646439044']}, {'npis': ['8065327496', '3487201540', '4693760324']}]
我相信我的问题在于我如何在尝试中编写循环子句,循环的输出由以下内容组成:
List #1 has been uploaded
{'id': 4151, 'npis': {'npis': ['3099994294', '1430739187', '5968165218']}}
List #2 has been uploaded
{'id': 8785, 'npis': {'npis': ['3099994294', '1430739187', '5968165218']}}
List #3 has been uploaded
{'id': 8786, 'npis': {'npis': ['3099994294', '1430739187', '5968165218']}}
List #1 has been uploaded
{'id': 4151, 'npis': {'npis': ['3559958528', '2502671659', '7646439044']}}
List #2 has been uploaded
{'id': 8785, 'npis': {'npis': ['3559958528', '2502671659', '7646439044']}}
List #3 has been uploaded
{'id': 8786, 'npis': {'npis': ['3559958528', '2502671659', '7646439044']}}
List #1 has been uploaded
{'id': 4151, 'npis': {'npis': ['8065327496', '3487201540', '4693760324']}}
List #2 has been uploaded
{'id': 8785, 'npis': {'npis': ['8065327496', '3487201540', '4693760324']}}
List #3 has been uploaded
{'id': 8786, 'npis': {'npis': ['8065327496', '3487201540', '4693760324']}}
因此,当前确实希望我要在一定程度上,似乎曾经将第一个dact从new_npis
添加到ALL ID中
s list_ids
,然后再次对其进行循环,以添加new_npis
的第二个dact,依此类推。
所需的最终结果是以下内容:
{'id': 4151, 'npis': {'npis': ['3099994294', '1430739187','5968165218']}}
{'id': 8785, 'npis': {'npis': ['3559958528', '2502671659', '7646439044']}}
{'id': 8786, 'npis': {'npis': ['8065327496', '3487201540', '4693760324']}}
请原谅dict的格式,这是我唯一可以让它们打印的方法
It seems there is something going on with the way the itertools.product
has been written here I have the below method in class. Which more or less does what I need however there is an issue with the way the data is being created.
def replace_multiple_npi_lists(self):
conn = self.establish_connection()
list_ids = [] # list of unique list ids
new_npis = [] # list of new npis
df = pd.read_csv("./uploads/new_file.csv")[["NAME", "NPI_ID"]]
df = df.dropna()
if get_npi_lists := self.get_all_account_npi_lists(561939):
for name in get_npi_lists:
npi_list = {
"id": name["id"],
"name": name["name"],
}
list_ids.extend(
npi_list["id"]
for target in df["NAME"].unique()
if target == npi_list["name"]
)
for target in df["NAME"].unique():
new_list = df[df["NAME"] == target].reset_index(drop=True)
new_list = new_list["NPI_ID"].apply(lambda x: str(int(x))).to_list()
final_list = dict(npis=new_list)
new_npis.append(final_list)
try:
# loop through new_lists and send list to npis
for (i, npis), (i, id) in itertools.product(
enumerate(new_npis), enumerate(list_ids)
):
if id == list_ids[i]:
r = conn.put(
f"https://xxx.xxx.com/RestApi/v1/npi/npi-list/{id}",
json=npis,
)
r.raise_for_status()
if r.status_code == requests.codes.ok:
print(f"List #{i + 1} has been uploaded")
new_list = {"id": id, "npis": npis}
print(new_list)
time.sleep(5)
self.establish_connection()
except requests.exceptions.HTTPError:
print(r.json())
Then end point accepts an id
in the URL and a json body like so:
{
'npis': [
122,
123,
...
]
}
I have formatted the below via print statements so i can see if the correct numbers are being passed to the right ids.
The method works, it does what I want it to do in terms of functionality but it doesn't create the dicts the way i need for them to be sent.
list_ids
: is a list of unique IDSnew_npis
: is a list of dicts that hold NPI numbers
list_ids
[4151, 8785, 8786]
new_npis
[{'npis': ['3099994294', '1430739187', '5968165218']}, {'npis': ['3559958528', '2502671659', '7646439044']}, {'npis': ['8065327496', '3487201540', '4693760324']}]
I believe my issue lies with how I have written the loop in the try clause, the output from the loop consists of the following:
List #1 has been uploaded
{'id': 4151, 'npis': {'npis': ['3099994294', '1430739187', '5968165218']}}
List #2 has been uploaded
{'id': 8785, 'npis': {'npis': ['3099994294', '1430739187', '5968165218']}}
List #3 has been uploaded
{'id': 8786, 'npis': {'npis': ['3099994294', '1430739187', '5968165218']}}
List #1 has been uploaded
{'id': 4151, 'npis': {'npis': ['3559958528', '2502671659', '7646439044']}}
List #2 has been uploaded
{'id': 8785, 'npis': {'npis': ['3559958528', '2502671659', '7646439044']}}
List #3 has been uploaded
{'id': 8786, 'npis': {'npis': ['3559958528', '2502671659', '7646439044']}}
List #1 has been uploaded
{'id': 4151, 'npis': {'npis': ['8065327496', '3487201540', '4693760324']}}
List #2 has been uploaded
{'id': 8785, 'npis': {'npis': ['8065327496', '3487201540', '4693760324']}}
List #3 has been uploaded
{'id': 8786, 'npis': {'npis': ['8065327496', '3487201540', '4693760324']}}
So currently it does want I want to a certain degree, how ever it seems to be adding the first dict from new_npis
to the all id
s in list_ids
and then looping over them again to add the second dict from new_npis
and so on.
The desired end result is to be the following:
{'id': 4151, 'npis': {'npis': ['3099994294', '1430739187','5968165218']}}
{'id': 8785, 'npis': {'npis': ['3559958528', '2502671659', '7646439044']}}
{'id': 8786, 'npis': {'npis': ['8065327496', '3487201540', '4693760324']}}
excuse the formatting here of the dicts it's the only way I could get them to print
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经通过以下内容解决了它:
I have resolved it by doing the following: