kor_pgson
kor_pgson

Reputation: 1

python dictionary: can i put list as key in dictionary?

<ipython-input-3-e36ce72b5104> in <module>
 87     return s_u_df, s_i_df, p_i_df, s_t_df, p_t_df
 88 
---> 89 print(analyze_data())
 90 
 91 

<ipython-input-3-e36ce72b5104> in analyze_data()
 27     #unit_data분석
 28     for i in  unq_unit1_dt:
---> 29         a = unit_data['1st-unit'].count(i)
 30         unit1_d[str(i)] = a
 31         a = 0

~\Anaconda3\lib\site-packages\pandas\core\series.py in count(self, level)
   1718 
   1719         if isinstance(level, str):
-> 1720             level = self.index._get_level_number(level)
   1721 
   1722         lev = self.index.levels[level]

~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in _get_level_number(self, level)
   1418 
   1419     def _get_level_number(self, level):
-> 1420         self._validate_index_level(level)
   1421         return 0
   1422 

~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in _validate_index_level(self, level)
   1414         elif level != self.name:
   1415             raise KeyError(
-> 1416                 f"Requested level ({level}) does not match index name ({self.name})"
   1417             )
   1418 

KeyError: "Requested level (['TFT3_Fiora', 'TFT3_Caitlyn', 'TFT3_Leona', 'TFT3_Vi', 'TFT3_Vayne', 'TFT3_Irelia', 'TFT3_Riven', 'TFT3_Thresh', 'TFT3_Ekko', 'empty', 'empty', 'empty', 'empty']) does not match index name (None)"

This is my error

what i intended to do is i have list of list that have multiple duplicates and i want to match 'list as key':'number of duplicates' as value

first i get list without duplicates and

by using 'for in range' i tried counting things in without duplicates list in original list

유닛.csv file is made of 3 columns and has list in it

is there any better way of doing what i intended to do?

here is my code

    unq_unit1_dt = unit_data['1st-unit'].drop_duplicates()
unq_unit2_dt = unit_data['2nd-unit'].drop_duplicates()
unq_unit3_dt = unit_data['3rd-unit'].drop_duplicates()

#저장할 리스트
unit1_d= {};s_u1_d = {};u1_L = []
unit2_d = {};s_u2_d = {};u2_L = []
unit3_d = {};s_u3_d = {};u3_L = []
s_u_d = {};u4_L = []

#unit_data분석
for i in  unq_unit1_dt:
    a = unit_data['1st-unit'].count(i)
    unit1_d[str(i)] = a
    a = 0

for j in unq_unit2_dt:
    b = unit_data['2nd-unit'].count(j)
    unit2_d[j] = b
    b = 0

for h in  unq_unit3_dt:
    c = unit_data['3rd-unit'].count(h)
    unit3_d[h] = c
    c = 0

#데이터 정렬 후 값 추출
s_u1_d = sort_dict(unit1_d); u1_df = pd.DataFrame(s_u1_d); u1_df.to_csv("유닛1.csv", index=False,encoding = 'cp949')
s_u2_d = sort_dict(unit2_d); u2_df = pd.DataFrame(s_u2_d); u2_df.to_csv("유닛2.csv", index=False,encoding = 'cp949')
s_u3_d = sort_dict(unit3_d); u3_df = pd.DataFrame(s_u3_d); u3_df.to_csv("유닛3.csv", index=False,encoding = 'cp949')

for i in range(5):
    u1_L.append(list(s_u1_d.keys)[i])
    u2_L.append(list(s_u2_d.keys)[i])
    u3_L.append(list(s_u3_d.keys)[i])

Upvotes: 0

Views: 37

Answers (1)

eje211
eje211

Reputation: 2429

I find your code difficult to read, but, in general, as per Python documentation, not every value is authorized as a Python dictionary key. Only immutable values are allowed: values you cannot change. So any kind of number works, a string works, a tuple works, an object works (something that's an instance of a class you create), but lists don't work because you can change them. You can never use a list as a dictionary key. But, again, a tuple is fine.

Upvotes: 1

Related Questions