hi guyz,
i'm a Python user (not so long) , anyway i've read some stuff about lua .

i've written function like this

def pair(dic = {} ):
    return dic.keys(), dic.values()

a dictionary is a parameter to the pair function .
to use it like this :

dic = {1 : "Slackware", 2 : "Windows XP SP2"}
>>> for x, y in pair(dic):
    print x, y

anyway , it works fine with the dic but doesn't work with a dictionary like this :

dic2 ={"Operating System" : "Version", "Slackware": "11", "Ms Windows": "XP SP2"}

it produce this error : ValueError: too many values to unpack

i tried to use x, y in dic2.keys(), dic2.values() , and it still the same problem .
what do you think ?

Re: using Lua's pair() 80 80

When I have these kinds of maddening bugs, I use print statements to see if my assumptions are correct. In this case, we are assuming that x and y will take on values of keys and values, respectively. But now:

>>> print pair(dic)
([1, 2], ['Slackware', 'Windows XP SP2'])
>>> print pair(dic2)
(['Ms Windows', 'Operating System', 'Slackware'], ['XP SP2', 'Version', '11'])

Note that pair() returns two lists: the list of keys and the list of values. So ...

>>> for item in pair(dic2):
    print item

['Ms Windows', 'Operating System', 'Slackware']
['XP SP2', 'Version', '11']

So what would the line

for x,y in pair(dic2) mean?

* Iterate through dic2
* Unpack each item, assumed to be a 2-sequence, and hand the first slot to x and the second slot to y.

Now the error message makes sense: each item in dic2 is a 3-sequence!

If you want to print key, value pairs, then this is much easier:

>>> def printdict(dic):
    for key in dic:
       print key,dic[key]
>>> printdict(dic)
1 Slackware
2 Windows XP SP2
>>> printdict(dic2)
Ms Windows XP SP2
Operating System Version
Slackware 11

(Although note that the dictionary comes out in non-deterministic order.)

Hope it helps,

BTW -- anyone else notice that the "Toggle Plain Text" option seems to be broken recently?

Re: using Lua's pair() 80 80

Hi Jeff,
what is broken in the "Toggle Plain Text" option?

Re: using Lua's pair() 80 80

I think our friend StrikerX might be looking for this ...

dic2 ={"Operating System" : "Version", "Slackware": "11", "Ms Windows": "XP SP2"}
for key, val in dic2.items():
    print key, val
Re: using Lua's pair() 80 80

Yes, vega's version does the same as mine.

what is broken in the "Toggle Plain Text" option?

Actually ... nothing now! (weird!!). From two days ago until right now, if I hit "Toggle Plain Text", it didn't do anything.


Re: using Lua's pair() 80 80

thanks jrcagle & vegaseat ^_^
dic2.items() -> was what i looking for .
thanx again .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.