The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Type Check Problem
Discuss Type Check Problem in the Python Programming forum on Dev Shed. Type Check Problem Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 5th, 2013, 12:24 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 7
Time spent in forums: 1 h 37 m 5 sec
Reputation Power: 0
|
|
|
Type Check Problem
Quick question...
I want to add up elements in a list...pretty simple. But when some of those elements are nested lists, I want to add those up also. And then add the sum of the nested lists to the sum of the original list.
For example:
Code:
nested_list = [3, 43, 1, [11, 15, 3, 23, 2], [6, 4, 1, 88], [12, 53, 96]]
total = 0
for nested in nested_list:
if nested == type(list):
x = sum(nlist)
total += x
print(total)
else:
total += nested
print(total)
When the code hits the first nested list I get the "unsupported operand type" error.
Why is it skipping over the if and going to the else?
Any help is much appreciated. Thanks
|

February 5th, 2013, 01:47 AM
|
|
Contributing User
|
|
Join Date: Jul 2007
Location: Joensuu, Finland
|
|
Quote: | Originally Posted by CastorTroy
Code:
if nested == type(list):
When the code hits the first nested list I get the "unsupported operand type" error. |
A slight mistake. You meant:
Code:
if type(nested) == type(list):
Actually, though, the recommended way is:
Code:
if isinstance(nested, list):
In any case, your solution works only on two levels (there is no true recursion), but I guess that’s no problem?
__________________
My armada: openSUSE 12.3 (home desktop, laptop, work desktop), Ubuntu 12.04 LTS (mini laptop), Debian GNU/Linux 7.0 (server), Mythbuntu 12.04 LTS (HTPC), Bodhi Linux 2.0 & Windows 7 Ultimate (test desktop), FreeBSD 9.1 (test server)
|

February 5th, 2013, 10:27 AM
|
 |
Contributing User
|
|
|
|
Someone recently mentioned lisp, thus
Code:
def car(L):
return L[0]
def cdr(L):
return L[1:]
def flatten(L):
'''
>>> flatten([3,4,[43,34,[23,2423]]])
[3, 4, 43, 34, 23, 2423]
>>> flatten(3)
[3]
>>> flatten([])
[]
'''
if isinstance(L,str):
return [L]
if hasattr(L,'__len__'):
if 0 == len(L):
return []
return flatten(car(L)) + flatten(cdr(L))
return [L]
print(sum(flatten([3, 43, 1, [11, 15, 3, 23, 2], [6, 4, 1, 88], [12, 53, 96]])))
__________________
[code] Code tags[/code] are essential for python code!
|

February 7th, 2013, 07:09 PM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 7
Time spent in forums: 1 h 37 m 5 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by SuperOscar A slight mistake. You meant:
Code:
if type(nested) == type(list):
Actually, though, the recommended way is:
Code:
if isinstance(nested, list):
In any case, your solution works only on two levels (there is no true recursion), but I guess that’s no problem? |
That was perfect...many thanks
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|