473,807 Members | 2,884 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

appending key-value pairs to a dict

rbt
I know how to setup an empty list and loop thru something... appending
to the list on each loop... how does this work with dicts?

I'm looping thru a list of files and I want to put the file's name and
its sha hash into a dict on each loop.

Many thanks,

rbt
Jul 19 '05 #1
5 1892
rbt wrote:
I know how to setup an empty list and loop thru something... appending
to the list on each loop... how does this work with dicts?

I'm looping thru a list of files and I want to put the file's name and
its sha hash into a dict on each loop.


Whereas with a list you would call "append" in the loop, with a
dictionary you simply use an indexed-assignment type of access:

mydict = {}
for filename in some_list_of_fi lenames:
hash = sha.sha(open(fi lename).read()) .hexdigest() # or whatever
mydict[filename] = hash

-Peter
Jul 19 '05 #2
rbt wrote:
I know how to setup an empty list and loop thru something... appending
to the list on each loop... how does this work with dicts?

I'm looping thru a list of files and I want to put the file's name and
its sha hash into a dict on each loop.


Like so:

d = {}
for filename in files:
d[sha_func(filena me)] = filename
Or like so:

d = dict([(sha_func(filen ame), filename) for filename in files])

--
Brian Beck
Adventurer of the First Order
Jul 19 '05 #3
On Friday 20 May 2005 01:04 pm, rbt wrote:
I know how to setup an empty list and loop thru something... appending
to the list on each loop... how does this work with dicts?

I'm looping thru a list of files and I want to put the file's name and
its sha hash into a dict on each loop.

Many thanks,

rbt


Simple assignment.

adict[filename] = an_sha_hash

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Jul 19 '05 #4
rbt <rb*@athop1.ath .vt.edu> wrote:
I know how to setup an empty list and loop thru something... appending
to the list on each loop... how does this work with dicts?

I'm looping thru a list of files and I want to put the file's name and
its sha hash into a dict on each loop.


You just assign values to keys. If the key doesn't exist, it's
created automagically. You want something like this:

shaDict = {}
for fileName in fileNameList:
hash = generateShaHash (fileName)
shaDict[hash] = fileName
Jul 19 '05 #5
rbt
Peter Hansen wrote:
rbt wrote:
I know how to setup an empty list and loop thru something... appending
to the list on each loop... how does this work with dicts?

I'm looping thru a list of files and I want to put the file's name and
its sha hash into a dict on each loop.

Whereas with a list you would call "append" in the loop, with a
dictionary you simply use an indexed-assignment type of access:

mydict = {}
for filename in some_list_of_fi lenames:
hash = sha.sha(open(fi lename).read()) .hexdigest() # or whatever
mydict[filename] = hash

-Peter


Thanks guys... that works great.
Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1037
by: Chris Belcher | last post by:
I'm building an action item database among the tables are Scope - A table that contains a scope of who gets assigned this task AI Master - The master task (I want you to do this and other data) Ai Detail - A group of entries for each work group/location/equip location
3
9606
by: kylei | last post by:
When I try to append one table to another with the exact same field names I get an error... Microsoft Access was unable to append all the data to the table. The contents of fields in 0 record(s) were deleted, and 0 record(s) were lost due to key violations. *If data was deleted, the data you pasted or imported doesn't match the field data types or the FieldSize property in the destination table.
7
2436
by: PC Datasheet | last post by:
Looking for suggestions ---- A database was designed for a national automobile inspection program. In it's simplest form, the database has two tables: TblOwner OwnerID <Year/Make/Model owned by owner, Owner name/address, etc) TblInspection InspectionID
13
2408
by: Shwetabh | last post by:
Hi, I wanted to know if it is possible to do to append two tables into a third table. For example, consider these two tables Table 1 -------------------------------------------------------------- | Part_num | Prt_name | Desc1 | Desc2 | -------------------------------------------------------------- | PRT1 | PartA | abc | xyz |
3
7188
by: a_masselink | last post by:
It doesn't append anything to the primary key (autonumber, long integer), only to all the other fields. Even if I set all the fields to allow zero length: yes, indexed: no and required no. All the field names are the same in the query as in the destination table. This is the query: INSERT INTO tblTempPackagesImp ( PackQuantImp, PackTypeImpID, GrossWeightImp, GrossCubeImp, CommodityName, ContainerID, BolImportID, ContainerSealNumber,...
2
3641
by: pyscottishguy | last post by:
Hey, I started with this: factByClass = {} def update(key, x0, x1, x2, x3): x = factByClass.setdefault(key, , , , ]) x.append(x0) x.append(x1)
3
1683
by: HSXWillH | last post by:
I've looked through the site and not found what I'm looking for here. I am not code-versed or anything like that so my skills are rudimentary at best. I'm using Access 03 on a Windows Vista environment. My question is about how to minimize data fields from large data-dumps in order to give myself the most recent recordset and to eliminate all older, out of date records. I have 7 bulk tables of data imported daily from a horse-racing text...
10
6782
by: MeeMee | last post by:
Hi I have a problem appending data into an oracle table from access. I imported the new data from an excel sheet into a table in access and useed an append query to add the data into a linked oracle table. The linked table has a composit primary key (Code, Org). The table in access has new data that are not in the oracle table and the data in the PK fields are not duplicated. I also made sure that both tables have same data types. I...
2
1784
by: mott3510 | last post by:
I have a list of numbers imported from excel, an example line looks like this... 70609,86,91,,66,66, I would like a N/A to be inserted in between the two commas and after the last one. This is the code I have so far, but I haven't gotten it to work. f = open("C:\users\cory\desktop\code\Verification.csv") dd = {}
5
1832
by: Russ Slater | last post by:
Hi, I am currently getting a very odd key violation when appending values to a table. The strangest thing is that it is only happening some of the time. I have a form where people can create schedules. They can create single day events or they can select events that happen every Tuesday from today until the last day of September for example. Their schedule gets parked into a temporary table where they can then edit any details (for deleting...
0
10626
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10374
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10112
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9193
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7650
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6879
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5685
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4330
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.