LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-11-2012, 10:18 AM   #1
ArthurHuang
Member
 
Registered: Jan 2006
Posts: 174

Rep: Reputation: 30
Python dict query and assign values problems


Hi guys, I have python dict query and assign values problems, I defined a dict

key_words_list = {
'key description1':'key1',
'key description2':'key2',
'key description3':'key3',
....
...
...
#about 150 keys
}


Then I'll search keys in inputfile which has the following format:

Key description1 10 30 20
Key description5 10 30 50
Key description7 10 30 50
Key description2 14 33 20
Key description4 15 30 50
Key description3 3 30 30
Key description6 10 30 50
Key description8 10 30 80

Now my question is
1)Can I use value in dic as arrray?
For example, can I write codes like this?
for i, key, value in key_words_list:
values[i] = 'some words'
2) If 1st answer is yes, this program can be written in C/C++ style

for line in lines:
for key,value in key_words_list:
if line.startwith(key)
value[0] = "word1"
value[1] = "word2"

BUT, is there any better pythonic style to implement this functionality?

Thanks a lot!!!

Last edited by ArthurHuang; 03-11-2012 at 05:13 PM.
 
Old 03-12-2012, 10:39 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,774

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by ArthurHuang View Post
1)Can I use value in dic as arrray?
Dictionaries don't have a defined order, if that's what you're asking. You can get a copy of the dictionary's contents as an array of (key, value) pairs (see dict.items()), or an array of just the values, or an array of the keys.

Quote:
2) If 1st answer is yes, this program can be written in C/C++ style

for line in lines:
for key,value in key_words_list:
if line.startwith(key)
value[0] = "word1"
value[1] = "word2"

BUT, is there any better pythonic style to implement this functionality?
It's really not clear what functionality you are looking for.

PS please put [code][/code] tags around your code,especially python code.
 
Old 03-13-2012, 04:18 AM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
It's really not clear what it is you are trying to do. If you are stepping through each line in you input file and looking for the entry in you dict then once you have the line from you input file split into a key and value and are ready to see if their is a match with an entry in you dict then you can try:

Code:
if key in key_words_list:
    key_words_list[key] = value
or whatever it is that you want to do with your data.
 
  


Reply

Tags
python dict item value


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
python set_source_rgb set_source_rgba values arizonagroovejet Programming 3 07-05-2010 03:55 PM
Shell script that assign's values to fields NsearchOf Programming 16 06-01-2009 10:00 AM
Script: how to assign values to variables using "awk" results? JZL240I-U Linux - Software 3 11-18-2008 12:59 AM
python cant assign to literal master Programming 4 09-05-2004 04:36 PM
Using letters/strings to assign variables in python Colonel Panic Programming 0 09-14-2001 10:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration