简体   繁体   English

使用每个键的多个值更新字典中的值

[英]Update value in Dictionary with multiple values per key

I have a dictionary with multiple 16 digit keys, each has 5 values assigned to it to later be printed in a nicely formatted column. 我有一本具有多个16位数字键的字典,每个字典都有5个值分配给它,以便以后在格式良好的列中打印。 I'm running each key through a couple functions, and wanted to update the values as it happens, leaving the rest as they were. 我正在通过几个函数运行每个键,并希望在发生更改时更新值,而其余的保持不变。

ex: 例如:

ccDict={"111111111111111111":("N/A", "N/A", "N/A", "N/A", "N/A")}

printed after running through functions: 运行函数后打印:

CardNumber                 Length   Prefix   SumDbleEvenPlace  SumOddPlace       Valid?    
11111111111111111111111        19       N/A      N/A               N/A               No 

I'm having trouble updating and changing specific values, and haven't been able to find much documentation online. 我在更新和更改特定值时遇到问题,并且无法在线找到很多文档。 Is there no way to update one of the specific values, and leaving the rest as they are? 有没有办法更新一个特定值,而其余的保持不变? I'd be ok with transferring t 我可以转让t

You should use a list instead of a tuple for your values. 您应该使用列表而不是元组作为值。 Then you can do this: 然后,您可以执行以下操作:

ccDict={"111111111111111111":["N/A", "N/A", "N/A", "N/A", "N/A"]}
ccDict['111111111111111111'][0] = "new value"
ccDict
{'111111111111111111': ['new value', 'N/A', 'N/A', 'N/A', 'N/A']}

If you insist on using a tuple, you can do this: 如果您坚持使用元组,则可以执行以下操作:

ccDict={"111111111111111111":("N/A", "N/A", "N/A", "N/A", "N/A")}
new_values = list(ccDict['111111111111111111'])
new_values[0] = "new value"
ccDict['111111111111111111'] = tuple(new_values)
ccDict['111111111111111111']
('new value', 'N/A', 'N/A', 'N/A', 'N/A')

Since it sounds like you're having trouble initializing the ccDict values as well, you can do something like this: 由于听起来您也难以初始化ccDict值,因此可以执行以下操作:

for k in my_list:
    ccDict[k] = [str(len(k)), "N/A", "N/A", "N/A", "No"]

if my_list is your list of cc values. 如果my_list是您的抄送值列表。

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

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