Iqbal Pratama Iqbal Pratama - 2 years ago 78
Python Question

Comparing 2 imported python unicode lists from excel

I'm trying to compare 2 python unicode lists which are imported from an excel file to count their variables.

This is my code

import xlrd
import tkFileDialog

excel_file = tkFileDialog.askopenfilename(filetypes=[('excelfile', '*.xslx')], title = 'Choose a .xslx file')

workbook = xlrd.open_workbook(excel_file)
sheet = workbook.sheet_by_index(0)

data = [sheet.col_values(i)[1:] for i in range(sheet.ncols) if sheet.col_values(i)[1]=='Difficulty']
data2 = [sheet.col_values(i)[1:] for i in range(sheet.ncols) if sheet.col_values(i)[1]=='Decision']

print data, data2

hard_yes = 0
hard_no = 0
medium_yes = 0
medium_no = 0
easy_yes = 0
easy_no = 0

for i in zip(data, data2):
if i == ('Hard', 'Yes'):
hard_yes = hard_yes + 1
elif i == ('Hard', 'No'):
hard_no = hard_no + 1
elif i == ('Medium', 'Yes'):
medium_yes = medium_yes + 1
elif i == ('Medium', 'No'):
medium_no = medium_no + 1
elif i == ('Easy', 'Yes'):
easy_yes = easy_yes + 1
elif i == ('Easy', 'No'):
easy_no = easy_no + 1

print hard_yes, hard_no, medium_yes, medium_no, easy_yes, easy_no

I'm trying to store the values to the variables. I can do this when both lists is hardcoded, but I can't store the values when I imported the lists from excel.

This is the output from above code

[[u'Difficulty', u'Easy', u'Medium', u'Medium', u'Hard', u'Easy', u'Hard', u'Easy', u'Easy', u'Easy', u'Hard', u'Hard', u'Medium', u'Hard', u'Medium', u'Easy', u'Medium', u'Medium', u'Medium', u'Easy', u'Hard']] [[u'Decision', u'Yes', u'No', u'Yes', u'Yes', u'Yes', u'No', u'No', u'No', u'Yes', u'Yes', u'No', u'Yes', u'No', u'Yes', u'Yes', u'No', u'No', u'No', u'Yes', u'No']]
0 0 0 0 0 0

EDIT: I just realize that this is a unicdoe list, how do I compare two unicode lists? Or how to properly cast a unicode list into a string list?

Answer Source

Problem is with data and data2 lists. They are actually list of lists, not list of elements. So, with zip, You are iterating over 1 element list.

Just replace

for i in zip(data, data2):


for i in zip(data[0], data2[0]):
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download