Hello,
Any comments on the below mentioned problem are most appreciated.
I have a function and it is throwing this error: - FEXpython_v2.py", line 32, in UnitHolderDistributionqty
-
count[item]+=1
-
KeyError: 3
This is the function: - def Distributionqty(dictionary):
-
holder=list()
-
held=list()
-
distqtydic={}
-
count={}
-
for key in sorted(dictionary.keys()):
-
holder.append(key)
-
held.append(len(dictionary[key]))
-
for (key, value) in map(None, holder, held):
-
distqtydic[key]=value
-
for item in distqtydic.values():
-
count[item]+=1
-
for k,v in sorted(count.items()):
-
fdist=k
-
qty=v
-
print fdist,qty
Not sure why.
G.
3 12831
Hello,
Any comments on the below mentioned problem are most appreciated.
I have a function and it is throwing this error: - FEXpython_v2.py", line 32, in UnitHolderDistributionqty
-
count[item]+=1
-
KeyError: 3
This is the function: - def Distributionqty(dictionary):
-
holder=list()
-
held=list()
-
distqtydic={}
-
count={}
-
for key in sorted(dictionary.keys()):
-
holder.append(key)
-
held.append(len(dictionary[key]))
-
for (key, value) in map(None, holder, held):
-
distqtydic[key]=value
-
for item in distqtydic.values():
-
count[item]+=1
-
for k,v in sorted(count.items()):
-
fdist=k
-
qty=v
-
print fdist,qty
Not sure why.
G.
You need something like this: -
for item in distqtydic.values():
-
if distqtydic.has_key(item):
-
count[item]+=1
-
else:
-
count[item] = 1
-
Going back to your first lesson, your keys are strings (as in '3'), if you haven't changed things too much since then. In that case, you'd use:
but I don't recall at this moment if the what type target value is. If it is an int, it may or may not (don't recall) be passed by reference. If you can give a sample of the argument, that would help a lot.
Hi.
I was able to work out with the following: - def UnitHolderDistributionqty(dictionary):
-
from collections import defaultdict
-
count=defaultdict(int)
-
for item in dictionary.values():
-
count[len(item)]+=1
-
for k,v in sorted(count.items()):
-
fdist=k
-
qty=v
-
print fdist,qty
Thanks again.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Norman Shelley |
last post by:
Is there a way to get at the object one is trying to access with a key to list
out all the possible keys?
See the except clause below.
#!/usr/bin/env python2.2
from inspect import *
import sys...
|
by: |
last post by:
Hello.
Maybe someone will help me with this KeyError:
.............................
Traceback (most recent call last):
File "C:\Python\tabla.py", line 929, in -toplevel-
tablesDirectory =...
|
by: Rune Strand |
last post by:
I'm experiencing strange errors both with pickle and cPickle in the
below code:
import cPickle as pickle
#import pickle
from string import ascii_uppercase
from string import ascii_lowercase...
|
by: Peter Olcott |
last post by:
www.halting-problem.com
|
by: Jon Davis |
last post by:
If I have a class with a virtual method, and a child class that overrides
the virtual method, and then I create an instance of the child class AS A
base class...
BaseClass bc = new ChildClass();...
|
by: homepricemaps |
last post by:
i have an
href which looks like this:
<td class="all">
<a class="btn" name="D1" href="http://www.cnn.com">
</a>
here is my code
|
by: nyenyec |
last post by:
urllib.quote chokes on unicode in 2.4.4.
2.4.4 (#1, Oct 18 2006, 10:34:39)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File...
|
by: christof |
last post by:
Hi,
I am using pickle/unpickle to let my program save its documents to
disk. While this it worked stable for a long time, one of my users now
complained, that he had a file which can't be...
|
by: josh001 |
last post by:
I'm trying to figure out why KeyError: 0 pops up when I try to run the program containing the code.
OpenDLG = wx.FileDialog(
self.win,
message='Please choose...
|
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: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
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...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |