hi, all
i want to check if a variable is iterable like a list, how can i
implement this? 7 1277
satoru wrote:
hi, all
i want to check if a variable is iterable like a list, how can i
implement this?
this would be one way, though I'm sure others exist:
if hasattr(yourVar, '__iter__'):
# do stuff
On Sep 20, 6:35*pm, Aidan <ai...@gmail.comwrote:
satoru wrote:
hi, all
i want to check if a variable is iterable like a list, how can i
implement this?
this would be one way, though I'm sure others exist:
if hasattr(yourVar, '__iter__'):
* * * * # do stuff
thank you,but this will miss out sequences like string just because it
doesn't have an attribute named '__iter__'
satoru wrote:
hi, all
i want to check if a variable is iterable like a list, how can i
implement this?
untested
def is_iterable(param):
try:
iter(param)
except TypeError:
return False
else:
return True
--
By ZeD
On Sep 20, 8:54*pm, satoru <torainLi...@gmail.comwrote:
On Sep 20, 6:35*pm, Aidan <ai...@gmail.comwrote:
satoru wrote:
hi, all
i want to check if a variable is iterable like a list, how can i
implement this?
this would be one way, though I'm sure others exist:
if hasattr(yourVar, '__iter__'):
* * * * # do stuff
thank you,but this will miss out sequences like string just because it
doesn't have an attribute named '__iter__'
str objects have a __getitem__ attribute, as do other built-in
sequence types: unicode, xrange, buffer.
AFAIK if an object has no __iter__ but has a __getitem__, iter(obj)
will create an iterator that calls obj.__getitem__(0),
obj.__getitem__(1), etc until IndexError is raised.
satoru wrote:
On Sep 20, 6:35 pm, Aidan <ai...@gmail.comwrote:
>satoru wrote:
>>hi, all i want to check if a variable is iterable like a list, how can i implement this?
this would be one way, though I'm sure others exist:
if hasattr(yourVar, '__iter__'): # do stuff
thank you,but this will miss out sequences like string just because it
doesn't have an attribute named '__iter__'
In 3.0, it does. Such consistency is one of the advantages of 3.0.
In at least some 2.x's, str still uses the older __getitem__ iteration
protocol. I am not sure about other built-in sequences.
satoru,
I should point out that the normal
approach is to just try whatever it
is that you're doing, and let it fail
where it fails. For example:
def processSeq(x):
for i in x:
print i
processSeq([1, 2, 3])
processSeq("foobar")
processSeq(5) <-- This will fail.
cheers
James
On Sat, Sep 20, 2008 at 7:28 PM, satoru <to*********@gmail.comwrote:
hi, all
i want to check if a variable is iterable like a list, how can i
implement this?
-- http://mail.python.org/mailman/listinfo/python-list
--
--
-- "Problems are solved by method" This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Jay |
last post by:
I'm sure this is a really dumb question, but how do you detect a variable
type in Python?
For example, I want to know if the variable "a" is a list of strings or a
single string. How do I do...
|
by: Mike Meng |
last post by:
hi all,
I'm a newbie Python programmer with a C++ brain inside. I have a
lightweight framework in which I design a base class and expect user to
extend. In other part of the framework, I heavily...
|
by: opt_inf_env |
last post by:
Hello,
In some examples of a PHP implementation I saw the following:
if ( isset(x) and x==12)
I do not understand what for before to check whether some variable has
some value one need to check...
|
by: py |
last post by:
I have function which takes an argument. My code needs that argument
to be an iterable (something i can loop over)...so I dont care if its a
list, tuple, etc. So I need a way to make sure that...
|
by: vertigo |
last post by:
Hello
I receive such error:
File "p4.py", line 24, in PrintWordCountFloat
print "%s %f" % (word,words)
TypeError: list indices must be integers
i call PrintWordCountFloat with hash table,...
|
by: John Smith |
last post by:
Hello, I have a VB.NET application with a Windows form that have
several textboxes fields where I have dates entered. I would like to
do a date validation check after the the field is updated, so...
|
by: Tobiah |
last post by:
list.append() will add the two element list as the next
element of the list.
list.extend() is equivalent to list = list +
and the result is that each element of the added list
becomes it's own...
|
by: marc wyburn |
last post by:
Hi I'm using SQlite and the CSV module and trying to create a class
that converts data from CSV file into a SQLite table.
My script curently uses functions for everything and I'm trying to...
|
by: dudeja.rajat |
last post by:
Hi,
How to check if something is a list or a dictionary or just a string?
Eg:
for item in self.__libVerDict.itervalues():
self.cbAnalysisLibVersion(END, item)
where __libVerDict is a...
|
by: MeoLessi9 |
last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: Aftab Ahmad |
last post by:
Hello Experts!
I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
|
by: Aftab Ahmad |
last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below.
Dim IE As Object
Set IE =...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: marcoviolo |
last post by:
Dear all,
I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
| |