简体   繁体   中英

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. 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

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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