0

I have 2 variables I am trying to manipulate the data. I have a variable with a list that has 2 items.

row = [['Toyyota', 'Cammry', '3000'], ['Foord', 'Muustang', '6000']]

And a dictionary that has submissions

submission = {
    'extracted1_1': 'Toyota', 'extracted1_2': 'Camry', 'extracted1_3': '1000', 
    'extracted2_1': 'Ford', 'extracted2_2': 'Mustang', 'extracted2_3': '5000', 
    'reportDate': '2022-06-01T08:30', 'reportOwner': 'John Smith'}

extracted1_1 would match up with the first value in the first item from row. extracted1_2 would be the 2nd value in the 1st item, and extracted2_1 would be the 1st value in the 2nd item and so on. I'm trying to update row with the corresponding submission and having a hard time getting it to work properly.

Here's what I have currently:

iter_bit = iter((submission.values()))

for bit in row:
    i = 0
    for bits in bit:
        bit[i] = next(iter_bit)
        i += 1

While this somewhat works, i'm looking for a more efficient way to do this by looping through the submission rather than the row. Is there an easier or more efficient way by looping through the submission to overwrite the corresponding value in row?

3
  • 2
    You are not explicit about this, it the reason extracted1_1 matches the first item in the first row because the key end in 1_1? You're code doesn't seem to look at the keys, but is depending on order (which is not a great idea with dicts). Commented Jan 10, 2023 at 21:28
  • 1
    You can use for i, bits in enumerate(bit): instead of incrementing i yourself. Commented Jan 10, 2023 at 21:29
  • @Mark the submissions come in labeled as such to match the column and row, so it would be extractedColumn_Row Commented Jan 10, 2023 at 22:17

3 Answers 3

3

Iterate through submission, and check if the key is in the format extractedX_Y. If it does, use those as the indexes into row and assign the value there.

import re

regex = re.compile(r'^extracted(\d+)_(\d+)$')

for key, value in submissions.items():
    m = regex.search(key)
    if m:
        x = int(m.group(1))
        y = int(m.group(2))
        row[x-1][y-1] = value
Sign up to request clarification or add additional context in comments.

3 Comments

When I run this it is showing m = None Is that the expected behavior?
I had a typo, I wrote $ where I meant ^.
That did the trick! Appreciate this, as this allows for more flexibility than my initial attempt.
0

It seems you are trying to convert the portion of the keys after "extracted" to indices into row. To do this, first slice out the portion you don't need (i.e. "extracted"), and then split what remains by _. Then, convert each of these strings to integers, and subtract 1 because in python indices are zero-based.

for key, value in submission.items():
    # e.g. key = 'extracted1_1', value = 'Toyota'
    if not key.startswith("extracted"):
        continue

    indices = [int(i) - 1 for i in key[9:].split("_")]
    # e.g. indices = [0, 0]

    # Set the value
    row[indices[0]][indices[1]] = value

Now you have your modified row:

[['Toyota', 'Camry', '1000'], ['Ford', 'Mustang', '5000']]

Comments

-1

No clue if its faster but its a 2-liner hahaha

  for n, val in zip(range(len(row) * 3), submission.values()):
     row[n//3][n%3] = val

that said, i would probably do something safer in a work environment, like parsing the key for its index.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.