I want to print out list pieces without any zeros to make it more readable.
Part of this code is in a while loop. -
import copy
-
pieces = [ [1,2,3,4,5,6] , [0,0,0,0,0,0], [0,0,0,0,0,0] ]
-
def formatt (formatt) :
-
while 0 in formatt[1] :
-
formatt[1].remove(0)
-
while 0 in formatt[2] :
-
formatt[2].remove(0)
-
while 0 in formatt[0] :
-
formatt[0].remove(0)
-
print formatt
-
-
#part in loop \/
-
form = copy.deepcopy(pieces)
-
formatt(form)
-
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
This can be accomplished more succinctly with a list comprehension: - >>> pieces = [ [1,2,3,4,5,6] , [0,0,0,0,0,0], [0,0,0,0,0,0] ]
-
>>> [[num for num in item if num != 0] for item in pieces]
-
[[1, 2, 3, 4, 5, 6], [], []]
-
>>>
3 5561 bvdet 2,851
Expert Mod 2GB
This can be accomplished more succinctly with a list comprehension: - >>> pieces = [ [1,2,3,4,5,6] , [0,0,0,0,0,0], [0,0,0,0,0,0] ]
-
>>> [[num for num in item if num != 0] for item in pieces]
-
[[1, 2, 3, 4, 5, 6], [], []]
-
>>>
That works great!!
The error:
'TypeError: 'NoneType' object is not subscriptable' /iterable
Was because I used 'return pieces' instead of 'return (pieces)'
-Thanks
mahem1
'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. Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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()
|
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...
|
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...
|
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' :...
|
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...
|
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...
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
| |