473,397 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Python 'NoneType' Error

I want to print out list pieces without any zeros to make it more readable.
Part of this code is in a while loop.
Expand|Select|Wrap|Line Numbers
  1. import copy
  2. pieces = [ [1,2,3,4,5,6] , [0,0,0,0,0,0], [0,0,0,0,0,0] ]
  3. def formatt (formatt) :
  4.         while 0 in formatt[1] :
  5.                 formatt[1].remove(0)
  6.         while 0 in formatt[2] :
  7.                 formatt[2].remove(0)
  8.         while 0 in formatt[0] :
  9.                 formatt[0].remove(0)
  10.         print formatt
  11.  
  12.         #part in loop \/
  13.         form = copy.deepcopy(pieces)
  14.         formatt(form)
  15.  
But I getting this error message after the loop runs once:

Traceback (most recent call last):
File "C:\Documents and Settings\Jos\Desktop\Towers of Henoy.py", line 68, in <module>
formatt(form)
File "C:\Documents and Settings\Jos\Desktop\Towers of Henoy.py", line 37, in formatt
while 0 in formatt[1] :
TypeError: 'NoneType' object is not subscriptable

Can anyone help?


-Thanks
mahem1
Jun 5 '11 #1

✓ answered by bvdet

This can be accomplished more succinctly with a list comprehension:
Expand|Select|Wrap|Line Numbers
  1. >>> pieces = [ [1,2,3,4,5,6] , [0,0,0,0,0,0], [0,0,0,0,0,0] ]
  2. >>> [[num for num in item if num != 0] for item in pieces]
  3. [[1, 2, 3, 4, 5, 6], [], []]
  4. >>> 

3 5561
bvdet
2,851 Expert Mod 2GB
This can be accomplished more succinctly with a list comprehension:
Expand|Select|Wrap|Line Numbers
  1. >>> pieces = [ [1,2,3,4,5,6] , [0,0,0,0,0,0], [0,0,0,0,0,0] ]
  2. >>> [[num for num in item if num != 0] for item in pieces]
  3. [[1, 2, 3, 4, 5, 6], [], []]
  4. >>> 
Jun 6 '11 #2
That works great!!
The error:
'TypeError: 'NoneType' object is not subscriptable' /iterable
Was because I used 'return pieces' instead of 'return (pieces)'

-Thanks
mahem1
Jun 6 '11 #3
dwblas
626 Expert 512MB
'TypeError: 'NoneType' object is not subscriptable' /iterable
Was because I used 'return pieces' instead of 'return (pieces)'
I suspect that it was because you use the same name for the function and the variable (which one are you referring to in your code). A variable is of type string, int, etc. A function is probably None type.
Expand|Select|Wrap|Line Numbers
  1. def formatt (formatt) : 
Jun 9 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Juergen R. Plasser | last post by:
Hi, I have installed libxml2-2.5.7, libxslt-1.0.30 from source and the bindings libxml2-python-2.5.7. Everything seems to compile fine, but when I try to import the libxml2 library in python...
0
by: Sebastien de Menten | last post by:
I am trying to use ScriptControl under Excel (Windows XP) with the code: Global sc As New MSScriptControl.ScriptControl Public Function os_getcwd() sc.Language = "python" sc.ExecuteStatement...
2
by: Christian | last post by:
Hello, i have an apache 1.3 server with python on debian. Python works fine but the scripts wont´t work. This easy script i want to do on apache: #!/usr/bin/python import os os.getcwd()
3
by: Rakesh | last post by:
In my Python code fragment, I want to write a code fragment such that the minimum element of a tuple is subtracted from all the elements of a given tuple. When I execute the following python...
5
by: Madhu R. Vajrala | last post by:
Hello All, I am very new to Python, trying to install it from source (ftp://ftp.sunfreeware.com/pub/freeware/SOURCES/python-2.3.3.tar.gz) on Sun Solaris-9 (SPARC). But getting the below error...
3
by: yaffa | last post by:
hey folks i get this error: Python interpreter error: unsupported operand type(s) for |: when i run this line of code: for incident in bs('tr', {'bgcolor' : '#eeeeee'} | {'bgcolor' :...
1
by: homepricemaps | last post by:
if i do the following i get the url of an image i am looking for image = "" image = bs.img print image however if i do this out.write (image ) i get an error that says "nonetype error is...
26
by: Christoph Zwerschke | last post by:
You will often hear that for reasons of fault minimization, you should use a programming language with strict typing: http://turing.une.edu.au/~comp284/Lectures/Lecture_18/lecture/node1.html I...
3
by: Martin P. Hellwig | last post by:
Hey all, I'd like to wrap libpam so that I can use that for authentication and password management. I build ctypes (0.9.9.6) on my platform via ports. Now according to OpenPAM documentation...
2
by: Gabriel Genellina | last post by:
En Sun, 15 Jun 2008 05:35:18 -0300, Maryam Saeedi <ma.saeedi@gmail.comescribió: NoneType is not an exception, but the type of the None object. Perhaps you're not interpreting correctly some...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.