LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-20-2017, 07:59 AM   #1
MrLinuxDonnelly
Member
 
Registered: May 2015
Location: London, United Kingdom
Distribution: Redhat 6.5, Centos, Fedora
Posts: 33

Rep: Reputation: Disabled
Python TypeError: unhashable type: 'list'


Ok so quick overview

i have a python script which basically counts the amount of times a line occurs and produces a output with the string and occurrences

this is the working code

echo -e "username is richard\nusername is bob\nusername is phil" | awk '{print $3}' | ./lineCount1.py
1 bob
1 phil
1 richard

Code:
#!/usr/bin/python
import sys 
names = {} 
for name in sys.stdin.readlines(): 
        name = name.strip() 
        if name in names:
                names[name] += 1
        else:
                names[name] = 1
for name, count in names.iteritems(): 
        sys.stdout.write("%d\t%s\n" % (count, name))
Now i wanted to remove having to format (use awk) before entering the data into the script so i used the .split functionality which seems to break it

echo -e "username is richard\nusername is bob\nusername is phil" | ./lineCount2.py
Traceback (most recent call last):
File "./lineCount2.py", line 8, in <module>
if name in names:
TypeError: unhashable type: 'list'

Code:
#!/usr/bin/python

import sys
names = {}
for line in sys.stdin.readlines():
        word = line.split()
        name = [line[2]]
        if name in names:
                names[name] += 1
        else:
                names[name] = 1
for name, count in names.iteritems():
        sys.stdout.write("%d\t%s\n" % (count, name))
Ive read up on this a bit and can see its something to do with it needing to be tuples ? Had a play around but cant seem to get it to work. Any help would be appreciated.
 
Old 02-20-2017, 08:14 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,216

Rep: Reputation: 5432Reputation: 5432Reputation: 5432Reputation: 5432Reputation: 5432Reputation: 5432Reputation: 5432Reputation: 5432Reputation: 5432Reputation: 5432Reputation: 5432
just read carefully these two lines:
Code:
        word = line.split()
        name = [line[2]]
 
Old 02-20-2017, 09:08 AM   #3
MrLinuxDonnelly
Member
 
Registered: May 2015
Location: London, United Kingdom
Distribution: Redhat 6.5, Centos, Fedora
Posts: 33

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
just read carefully these two lines:
Code:
        word = line.split()
        name = [line[2]]
Ahhh yes ive changed as per below however same error

Code:
#!/usr/bin/python

import sys
names = {}
for line in sys.stdin.readlines():
        word = line.split(' ')
        name = [word[2]]
        if name in names:
                names[name] += 1
        else:
                names[name] = 1
for name, count in names.iteritems():
        sys.stdout.write("%d\t%s\n" % (count, name))

Code:
echo -e "username is richard\nusername is bob\nusername is phil"  | ./lineCount2.py 
Traceback (most recent call last):
  File "./lineCount2.py", line 8, in <module>
    if name in names:
TypeError: unhashable type: 'list'
 
Old 02-20-2017, 09:14 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,216

Rep: Reputation: 5432Reputation: 5432Reputation: 5432Reputation: 5432Reputation: 5432Reputation: 5432Reputation: 5432Reputation: 5432Reputation: 5432Reputation: 5432Reputation: 5432
why name = [word[2]]
probably: name = word[2] ??
 
1 members found this post helpful.
Old 02-20-2017, 09:16 AM   #5
MrLinuxDonnelly
Member
 
Registered: May 2015
Location: London, United Kingdom
Distribution: Redhat 6.5, Centos, Fedora
Posts: 33

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
why name = [word[2]]
probably: name = word[2] ??
ahhh yes why did i put that in a "[]" would have cause the output to be in a bracket also.

Thanks for pointing out my madness much appreciated
 
Old 02-20-2017, 09:23 AM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,216

Rep: Reputation: 5432Reputation: 5432Reputation: 5432Reputation: 5432Reputation: 5432Reputation: 5432Reputation: 5432Reputation: 5432Reputation: 5432Reputation: 5432Reputation: 5432
glad to help you.
If you want to say thanks just click on yes.
This is not madness, this is called practising.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
TypeError: unsupported operand type(s) for -: 'set' and 'float' Bobbic Linux - Newbie 1 12-12-2015 01:18 PM
[SOLVED] Python: error - TypeError: 'str' object is not callable angel115 Programming 3 06-24-2014 09:10 AM
[SOLVED] Python TypeError when calling Python program from a Bash script noppeli Programming 2 01-15-2013 08:06 AM
python os.system() or subprocess.Popen() TypeError on ubuntu lucid 10.04 sahar.gh Programming 1 06-25-2012 07:56 PM
TypeError: list indices must be integers felix001 Programming 1 12-27-2010 08:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration