Hello All,
I am trying to move an application from python 1.5.2 to 2.3. The code
works fine in 1.5.2 but gives the exception (exceptions.TypeError
unpack non-sequence) in python 2.3. I did not write this code so I am
not sure what is happening here.
Here is the code snippet:
for (item, agent) in self.lItems:
lItems.append(interpolate(self._ITEM_FMT, id=str(item)))
Note:
self.lItems contains two elements.
Questions:
1) What is the for statement doing?
2) Is this called tuple unpacking or list unpacking?
3) Is there newer syntax?
4) Why does he use the "for" loop like that?
Any help is appreciated.
Thanks
Ahsan 4 8018 I am trying to move an application from python 1.5.2 to 2.3. The code works fine in 1.5.2 but gives the exception (exceptions.TypeError unpack non-sequence) in python 2.3. I did not write this code so I am not sure what is happening here.
Here is the code snippet:
for (item, agent) in self.lItems: lItems.append(interpolate(self._ITEM_FMT, id=str(item)))
Note: self.lItems contains two elements.
Always exactly 2 items?
Questions: 1) What is the for statement doing?
Attempting to assign the names item and agent a pair of values in
self.lItems
2) Is this called tuple unpacking or list unpacking?
list unpacking:
[item, agent] = [1,2]
tuple unpacking:
item, agent = 1,2
(item, agent) = 1,2
item, agent = (1,2)
(item, agent) = (1,2)
I would be willing to bet that the list below is cast into a tuple:
(item, agent) = [1,2]
3) Is there newer syntax?
I wouldn't so much call it newer as more intuitive. for i,j in [(1,2), (3,4)]:
.... print i, j
....
1 2
3 4
4) Why does he use the "for" loop like that?
Because he doesn't realize he could do the below.
item, agent = self.lItems
lItems.append(interpolate(self._ITEM_FMT, id=str(item)))
- Josiah
Josiah Carlson <jc******@nospam.uci.edu> wrote in message news:<bv**********@news.service.uci.edu>... I am trying to move an application from python 1.5.2 to 2.3. The code works fine in 1.5.2 but gives the exception (exceptions.TypeError unpack non-sequence) in python 2.3. I did not write this code so I am not sure what is happening here.
Here is the code snippet:
for (item, agent) in self.lItems: lItems.append(interpolate(self._ITEM_FMT, id=str(item)))
Note: self.lItems contains two elements.
Always exactly 2 items?
What if there are more than 2 items? How can I do something where
element 0 and 1 are assigned to (item, agent) and so on? If this is a
silly question please let me know what I can read.
Thanks
Ahsan
"ahsan Imam" <ah********@newtimes.com> wrote in message
news:78**************************@posting.google.c om... Josiah Carlson <jc******@nospam.uci.edu> wrote in message
news:<bv**********@news.service.uci.edu>... I am trying to move an application from python 1.5.2 to 2.3. The code works fine in 1.5.2 but gives the exception (exceptions.TypeError unpack non-sequence) in python 2.3. I did not write this code so I am not sure what is happening here.
I do not know of any change in Python that would make code like the below
invalid. Are you possibly running the program with different input data?
I suggest you insert 'print self.lItems' before the loop to see it *that*
changed (somewhere else in the program). Here is the code snippet:
for (item, agent) in self.lItems: lItems.append(interpolate(self._ITEM_FMT, id=str(item)))
self.lItems must contain zero or more (item,agent) *pairs*
What if there are more than 2 items? How can I do something where element 0 and 1 are assigned to (item, agent) and so on?
Yes and no. You would have to group elements 0 and 1 into a pair, elements
2 and 3 into another (the second), and so on. There is probably something
in itertools that will do this. Otherwise, writing your own generator to
do so should be easy enough. Then write 'for (i,a) in grouper(s.l):' where
grouper is the pair generator.
Terry J. Reedy
> What if there are more than 2 items? How can I do something where element 0 and 1 are assigned to (item, agent) and so on? If this is a silly question please let me know what I can read.
If there ever was more than two items, that is, if it was a flat
sequence like this: [1,2,3,4,5,6], then the original for loop couldn't
have worked.
- Josiah This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Alex Vinokur |
last post by:
------ foo.cpp ------
#include <vector>
using namespace std;
int main()
{
const vector<int> v1 (10);
const vector<bool> v2 (10);
&v1;
|
by: L Mehl |
last post by:
Can someone tell me where to find out what this Call Stack item means?
Thank you for any help.
Larry Mehl
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system...
|
by: David Lozzi |
last post by:
Howdy,
I'm using a WYSIWYG editor called TinyMCE. When I edit some text and then
save it back to my SQL server using a SQLCommand, all HTML characters are
changed to HTML code, i.e. >strong<...
|
by: elderic |
last post by:
Hi there,
are there other ways than the ones below to check for <type 'function'>
in a python script?
(partly inspired by wrapping Tkinter :P)
def f():
print "This is f(). Godspeed!"
1.:...
|
by: mark |
last post by:
Hi All,
Apologies for the newbie question but I've searched and tried all
sorts for a few days and I'm pulling my hair out ; Please feel free to teach me to suck
eggs because it's all new to me...
|
by: sweetpotatop |
last post by:
Hi,
I got an pop-up error when I tried to run my window application.
However, I have no idea which line of code is causing the problem. Is
there a way to figurate that out? In VB, when I do...
|
by: Wojciech Gryc |
last post by:
Hi,
I recently started using Python and am extremely happy with how
productive it's made me, even as a new user. I'm hoping to continue
using the language for my research, and have come across a...
|
by: Utku Altinkaya |
last post by:
Hello,
I am wrapping the reference container vector as document suggests.
typedef std::vector<CUEEntity*EntityContainer;
class_<CUEEntity>("Entity", init<CUEEntity*>());
...
|
by: Nike1984 |
last post by:
I'm fairly new to Javascript and it's more of a guessing game for me...
I'm trying to build an app for Google Maps and just had some issues recently.
First off I just wanted to say that everything...
|
by: iammilind |
last post by:
In one of my code, I was using vector<> for certain class. In one of my struct, I have 'const' member data. However, vector<>::clear() throws compile error with that:...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
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: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
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)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
| |