need help

Asked by janani pavithra

hi,
i have got a doubt..
i need to know how to find the position of a paticular string in an array..
#this is my script
x=A4(may be any one of the strings in array 'a']

a=["A4","A3","Letter","Ledger"]
b=["a","aaa","l","lll"]

If x is "A4" then it should type "a"
If x is "A3" then it should type "aaa"
If x is "Letter" then it should type "l"
if x is "ledger" then it should type"lll"

so, i need to find the position of 'x' in 'a'
how should i get that..
how the script should be?
pl give the script

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

since these are typical key-value pairs, you should use a python dictionary:

# define the dictionary
d = { "A4" : "a", "A3" : "aaa", "Letter" : "l", "Ledger" : "lll" }

# check for a key
k = "B4"
if not k in d:
   print k + " not in dictionary"

# get the value for an exisiting key
v = d[ "A4" ]
# v contains "a"

# add or a new key-value pair or change the value for an existing key
d[ "B4" ] = "b"

# loop through entries
for k in d:
   print d[ k ]

a word on arrays:
what you call an array is called a list in python and can be used like arrays in other languages.

based on your example:

# to find a value in a list:
n = a.index("A4")
# n contains 0 (first entry)
# if not found raises ValueError

# If x is "A4" then it should type "a" with error handling
x = "A4"
try:
   type( b[ a.index( x ) ] )
except ValueError:
   print x + " unknown"

combining dictionary and lists:
if you have more values to connect to the keys, you can use a list (array) as value

say we want to store width and height of the format in mm:

d[ "A4" ] = [ "a", 210, 297 ]

then

print d[ "A4" ][ 2 ]

would print 297

Have fun ;-)

Can you help with this problem?

Provide an answer of your own, or ask janani pavithra for more information if necessary.

To post a message you must log in.