473,403 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

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 1870
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_filenames:
hash = sha.sha(open(filename).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(filename)] = filename
Or like so:

d = dict([(sha_func(filename), 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_filenames:
hash = sha.sha(open(filename).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
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...
3
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...
7
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...
13
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...
3
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...
2
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
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...
10
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...
2
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...
5
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
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,...
0
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,...
0
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...

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.