简体   繁体   中英

How do I print the following objects of a list

I have the following list:

lst = ['Daniel', '2', 'daniel@outlook.com', 'Jay', '1212', 'jay@siber.com']

and as you can see it shows a number and a email, let´s say the number 2 and daniel@outlook.com are Daniel "attributes" only.

How can I build a function that will allow me from a raw_input to print those, for example:

n = str(raw_input("Enter the name on the list:"))
if n in lst:
   print(...) #(2 and daniel@outlook.com) 
else:
   print("Name is not on the List")

And it has to work like this for Jay also. Is it possible to do this?

Thanks

It seems best to change your data structure into a dictionary, so instead of:

lst = ['Daniel', '2', 'daniel@outlook.com', 'Jay', '1212', 'jay@siber.com']

you would have:

dct = {'Daniel': ('2', 'daniel@outlook.com'), 'Jay': ('1212', 'jay@siber.com')}

Then, you can do exactly what you wrote:

if name in dct:
    print dct[name]

EDIT

To create a dictionary:

names = lst[::3]
numbers = lst[1::3]
emails = lst[2::3]
dct = {name: (number, email)
       for name, number, email
       in zip(names, numbers, emails)}

Consider using a dictionary instead of a list :

names = {'Daniel':['2', 'daniel@outlook.com'], 'Jay': ['1212', 'jay@siber.com']}
n=raw_input("Enter the name on the list:")   ## Your input statement 
print(names.get(n, "Name is not in the list"))

This will eliminate the need for the if check as well.

OUTPUT:

>>> 
Enter the name on the list:Daniel
['2', 'daniel@outlook.com']

>>> 
Enter the name on the list:df
Name is not in the list

You probably want to use a dictionary of lists, for example:

lst = {'Daniel': ('2', 'daniel@outlook.com'), 'Jay': ('1212', 'jay@siber.com')}

Then in your function use:

if n in lst:
    print(lst[n])

I would do this by first turning your list into a dictionary keyed on the name:

import itertools

def grouper(iterable, n):
    args = [iter(iterable)] * n
    return itertools.izip_longest(*args)

lst = ['Daniel', '2', 'daniel@outlook.com', 'Jay', '1212', 'jay@siber.com']

dlst = dict([(name, (num, email)) for name, num, email in grouper(lst, 3)])

Then you can just look up the values you want in the dict as input comes in:

print(dlst.get(raw_input('Enter the name on the list: '),
               "Name is not in the list"))

Other were faster, but I'll post my answer anyway, as I included function that prints it in a pretty way

dict = {'Daniel': ['daniel@outlook.com', '2'], 'Jay': ['jay@siber.com', '1212']}

def func():
    name = raw_input("Enter the name on the list: ")
    if name in dict:
        print ', '.join(dict[name])
    else:
        print "Name was not found"

Result:

>>> func()
Enter the name on the list: Daniel
daniel@outlook.com, 2
>>> func()
Enter the name on the list: dsgd
Name was not found

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM