473,385 Members | 1,838 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,385 software developers and data experts.

Translation and comparison of user input, to a list using a dictionary

Jory R Ferrell
I am having some difficulties understanding how to implement the dict() in Pyth v3.2 at least in the way that I would like to use it. Basically I first create a list. I then randomly reorganize the list. Then I want to create a dictionary using that list, assigning the indexed item from the list to a key in the dictionary. After running the program, the user is prompted to input a variable which is supposed to be equal to one of the keys of the dictionary. But I do not want the users input to be compared and weighed against the key itself, but rather the object assigned to the key (obviously...why the hell else bother creating the dictionary? :P). When I do this though, python starts complaining that variables haven't
t been declared. I thought this was the most direct way to compare the two but now I am completely stuck. No clue how to solve this.


Expand|Select|Wrap|Line Numbers
  1.  
  2. list_A = ['Obj1','Obj2','Obj3','Obj4','Obj5']
  3.  
  4. shuffle(list_A)
  5.  
  6. dict_A = {1:list_A[0], 2:list_A[1],3:list_A[2],4:list_A[3]
  7. ,5:list_A[4],6:list_A[5]}
  8.  
  9. z = input('enter something')
  10.  
  11. if dict_A[1] == list_A[0]: 
  12.     #does dict_A item '1' (i.e. list_A),
  13.     #equal the actual list_A index of 0?
  14.     print('blah,blah,blah)
  15. else:
  16.     something, something nice, something bad
  17.  
  18.  
Aug 11 '11 #1
2 3560
dwblas
626 Expert 512MB
You would search the entire dictionary and compare the dictionary's value to the look up variable. This does not take that long unless the dictionary is huge.
Expand|Select|Wrap|Line Numbers
  1. dict_A = {1:"abc", 2:"def", 3:"ghi"}
  2. look_up = "def"
  3. for key in dict_A:
  4.     if dict_A[key] == look_up:
  5.         print "Found", key, dict_A[key] 
Aug 12 '11 #2
Your code has some problems:
-shuffle() needs to be imported first. 'import random' should work.
-This page http://www.tutorialspoint.com/python/number_shuffle.htm says shuffle() should be called using a static 'random' object. 'random.shuffle(myList)'
-In line 7, you are asking for a 6th list item, but only 5 exist. I think thats not allowed.
-line 14 is missing a ' to end the string
Aug 18 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Kyle E | last post by:
Ok, I have a little tiny problem, a beginner problem, that I am overlooking. I am writing a Information Gathering Program, that takes user input from scripted questions and prints them in a handy...
3
by: N?ant Humain | last post by:
I have just begun learning Python so that I can write a simple script to make modification of a file used by another Python script easier. This file is basically a list of regular expressions. What...
0
by: Steve Perry | last post by:
I have a serialized list and dictionary stored in a file, with the following format: mydata: color origin datasource1 {server:"server1.domain" username:"client" password:"secret"} Is...
9
by: chuck | last post by:
I need some help with validating user input. I am writing a C computer program for an intro to C course. Here is the situation. I am creating an application that will do currency conversions. ...
0
by: JB | last post by:
Hi All, I was wondering if there was an easy and straightforward way to bind elements in a ListView control, with the corresponding elements in a List or Dictionary. Ideally I'd like a...
2
by: vinkumar | last post by:
Hi, I have to search for string and replace with user input value using js. I have used inner HTML (objExplorer.Document.body.innerHTML) to get user input. (Eg: If user inputs web port no. as...
1
by: pchadha20 | last post by:
Hello Guys, I am Not able to user synonyms based on dictionary tables like ..when i write the command like SELECT * FROM USER_SOURCE then error comes like ERROR at line 1: ORA-00980:...
1
by: Tamer Ibrahim | last post by:
Hi, I'm trying to validate user input date using range validator on a text box . This code is throwing this exception "The value 'DateTime.Now;' of the MaximumValue property of 'rvQDate'...
5
by: =?Utf-8?B?THVpZ2k=?= | last post by:
Hi all, having a List<stringand Dictionary<int, stringhow can I check that every string in the list is also in the Dictionary (and viceversa)? (and raise an exception when not). Thanks in...
1
by: golnaz hgh | last post by:
the xml file is like this: <publist> <book> A book entry has the following fields: o Title; o One or more authors; o Publisher; o Year </book>
0
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,...
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: 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...
0
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...

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.