We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,401 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Set and tuple comprehensions and generator, log of some of my interactive experiments

Here is some saved interaction in command prompt.

>>> type((1,2))
<type 'tuple'>
>>> (3*x for x in (4,4))
<generator object <genexpr> at 0x00E73EB8>
>>> tuple(x**2 for x in range(10))
(0, 1, 4, 9, 16, 25, 36, 49, 64, 81)
>>> set(x for x in range(10))
set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> {x:x**2 for x in range(10)}
SyntaxError: invalid syntax
>>> x=2
>>> {x:x**2}
{2: 4}
>>> [{x:x**2} for x in range(10)]
[{0: 0}, {1: 1}, {2: 4}, {3: 9}, {4: 16}, {5: 25}, {6: 36}, {7: 49}, {8: 64}, {9: 81}]
>>> type([{x:x**2} for x in range(10)])
<type 'list'>
>>> dict([[x:x**2] for x in range(10)])
SyntaxError: invalid syntax
>>> dict([[x,x**2] for x in range(10)])
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
>>>
1
Contributor
1
Reply
1 Day
Discussion Span
3 Years Ago
Last Updated
2
Views
pyTony
pyMod
Moderator
6,312 posts since Apr 2010
Reputation Points: 879
Solved Threads: 987
Skill Endorsements: 26

I got one down vote for this post, so I assume that somebody thought:

Oh, heck. This goes totally over my head.

and gave me down vote for missing of clarity.

The motivation to send this post was to show how many interesting features there are in understanding Pythons way of doing things.

So let's take that first one, which came up many times in my programs, when suddenly there was generators included as members of the list, yes that is possible, Pythons types are dynamic and multiple types of object can be put in list.

Are you advocate of object oriented design and have all your programs with main program:

obj=myobject()
myobject,run()

So you like to make list of list of your program file names and instance of man object of each file? No problem 1, 'a', or myspreadsheet(), all you can stuff in list, dict, you name it (there does exist some limitations which type of element set can have for example or dict index must have order, so no set indexes, thank you.

So let's take this first guy, code object called generator. Ever put range() in your for loops? This is one object that can replace that range. Why? Because range is not generator but still is one method to make a list with specified numbers. And list type belongs to class of types called iterables. What makes an iterable type? Only that it must have one method to generate next element from its members, that is generator called iterator.

Don't trust me? Here is the proof:

Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a=(3*x for x in (4,4))
>>> for i in a: print i
...
12
12
>>> tuple(a)
(12, 12)
>>> b= range(4)
>>> b
[0, 1, 2, 3]
>>> type(range)
<type 'builtin_function_or_method'>
>>> type(range(4))
<type 'list'>
>>>

There, does it make sense to you. No. If it does not matter to you, fine. Don't worry, be happy :icon_biggrin:

If it does, here is some homework to you. What happens here:

>>> a=(3*x for x in (4,4))
>>> a
<generator object <genexpr> at 0x00C6BDC8>
>>> tuple(a)
(12, 12)
>>> tuple(a)
()
>>> b=a
>>> tuple(b)
()
>>> b
<generator object <genexpr> at 0x00C6BDC8>
>>>
pyTony
pyMod
Moderator
6,312 posts since Apr 2010
Reputation Points: 879
Solved Threads: 987
Skill Endorsements: 26

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0540 seconds using 2.66MB