Home > Mobile >  Dictionary in for loop not working as I expected to be
Dictionary in for loop not working as I expected to be

Time:01-22

I am trying to create a dictionary using a for loop. However, not sure why, the item in bx3 and box is about 200 but when I display dicts, only 22 items are generated. Anyone can explain what I did wrong? I ensure the length of items is considered in the for loop before creating the dictionary but the results are odd. Any help will be appreciated!

enter image description here

import pandas as pd

import yfinance as yf
import pandas_ta as ta

char2 = ['a','b','c','d','e','f','g','h','i','j','k','l',
               'm','n','o','p','q','r','s','t','u','v','w','x','y','z']

rep2 = [1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8]
  
    
x=[4,9]

x2= list(dict.fromkeys(x))
lx=[]

for n1,v1 in enumerate(rep2):
    for j in x2:
        if v1==j:
            lx.append((char2[n1]).upper())

print(lx)
print('')

#read_asx_ticker

ASXLIST_FILE = 'https://www.asx.com.au/asx/research/ASXListedCompanies.csv'

df =pd.read_csv(ASXLIST_FILE,header=1)
codelist = df['ASX code'].values
codesector = df['GICS industry group'].values

dd=lx

box=[]
box2=[]
bx=[]
bx2=[]
bx3=[]
for i in range(0,len(codesector)):
    if 'Not' not in codesector[i]:
        if len(set(dd).intersection(codelist[i]))>2:
            box.append(codelist[i])
            bx2.append(codesector[i])
            
        elif len(set(dd).intersection(codelist[i]))==2:
            box2.append(codelist[i])
            bx3.append(codesector[i])
            bx.append(codelist[i] '/' codesector[i])


dicts={}

for i in range(0,len(bx3)):
    dicts[bx3[i]]= box2[i]

CodePudding user response:

This happens because you only have 22 unique values in bx3 List.

Specifically you have these values repeated:

'Consumer Services': 6,
 'Materials': 103,
 'Health Care Equipment & Services': 15,
 'Commercial & Professional Services': 8,
 'Capital Goods': 13,
 'Software & Services': 12,
 'Diversified Financials': 7,
 'Energy': 10,
 'Transportation': 1,
 'Utilities': 2,
 'Retailing': 5,
 'Real Estate': 5,
 'Technology Hardware & Equipment': 3,
 'Pharmaceuticals, Biotechnology & Life Sciences': 10,
 'Household & Personal Products': 1,
 'Food & Staples Retailing': 1,
 'Food, Beverage & Tobacco': 4,
 'Media & Entertainment': 6,
 'Consumer Durables & Apparel': 1,
 'Banks': 1,
 'Semiconductors & Semiconductor Equipment': 1,
 'Automobiles & Components': 2
  •  Tags:  
  • Related