Home > database >  Iterating through zipped objects in Python
Iterating through zipped objects in Python

Time:05-27

I've been having a hard time trying to solve this recently (although this looks like a trivial matter).

I have these 3 dictionaries:

letters_words = {'A': ['allow', 'arise'], 'B': ['bring', 'buy']}
words_cxns = {'allow': ['CXN1', 'CXN2'], 'arise': ['CXN1', 'CXN3'], 'bring': ['CXN2', 'CXN3'], 'buy': ['CXN3']}
cxns_ids = {'CXN1': 1, 'CXN2': 2, 'CXN3': 3} 

Every letter has a few words, every word is associated with certain constructions, every construction has an id.

In the end I want to get this:

A

allow
CXN1, 1
CXN2, 2

arise
CXN1, 1
CXN3, 3

B

bring
CXN2, 2
CXN3, 3

buy
CXN3, 3

The spaces and punctuation don't matter... The main thing is that it gets listed right.

Here is what I'm currently doing:

for letter, words in zip(letters_words.keys(), letters_words.values()):
    print(letter)
    for word in words:
        print(word)
        for w, cnxs in zip(words_cxns.keys(), words_cxns.values()):
            if w == word: 
                for c in cxns:
                    for cxn, ix in zip(cxns_ids.keys(), cxns_ids.values()):
                        if cxn == c:
                            print(c, ix)

However, my output looks like this at the moment:

A

allow
CXN1 1
CXN2 2
CXN3 3

arise
CXN1 1
CXN2 2
CXN3 3

B
bring
CXN1 1
CXN2 2
CXN3 3

buy
CXN1 1
CXN2 2
CXN3 3

What am I missing? :/

CodePudding user response:

You do not need zip for this task, as the construction merely depends on the word, not on the iteration of words. Here is a possible solution that produces your desired output:

for letter, words in letters_words.items():
    print('\n'   letter)
    for word in words:
        print('\n'   word)
        cxns = words_cxns[word]
        for cxn in cxns:
            cxn_id = cxns_ids[cxn]
            print(cxn, ',', cxn_id)

CodePudding user response:

No need to zip:

letters_words = {'A': ['allow', 'arise'], 'B': ['bring', 'buy']}
words_cxns = {'allow': ['CXN1', 'CXN2'], 'arise': ['CXN1', 'CXN3'], 'bring': ['CXN2', 'CXN3'], 'buy': ['CXN3']}
cxns_ids = {'CXN1': 1, 'CXN2': 2, 'CXN3': 3} 

for k,v in letters_words.items():
    print("\n"   k   "\n")
    for w in v:
        print(w)
        for word in words_cxns[w]:
            print(word, cxns_ids[word])

Output:

A

allow
CXN1 1
CXN2 2
arise
CXN1 1
CXN3 3

B

bring
CXN2 2
CXN3 3
buy
CXN3 3

CodePudding user response:

letters_words = {'A': ['allow', 'arise'], 'B': ['bring', 'buy']}
words_cxns = {'allow': ['CXN1', 'CXN2'], 'arise': ['CXN1', 'CXN3'], 'bring': ['CXN2', 'CXN3'], 'buy': ['CXN3']}
cxns_ids = {'CXN1': 1, 'CXN2': 2, 'CXN3': 3} 

for letter, words in letters_words.items():
    print(letter)
    for word in words:
        print(word)
        for cxn in words_cxns.get(word, []):
            print(f'{cxn}, {cxns_ids.get(cxn)}')

CodePudding user response:

Try this, the idea is to get the cxns directly from the dictionary instead of using a second zip object. I commented on the relevant row.

for letter, words in zip(letters_words.keys(), letters_words.values()):
    print(letter)
    for word in words:
        print(word)
        # no need to create a new zip object, get value from dict instead
        for cxns in words_cxns[word]:
            print(cxns, cxns_ids[cxns])

CodePudding user response:

That's embarrassing, but I've made a typo which I couldn't find for 2 days! On line 6 of my code suggestion, I've written cnxs instead of cxns. Once I changed it, everything worked!

  • Related