تخطي للذهاب إلى المحتوى
القائمة
لقد تم الإبلاغ عن هذا السؤال
3 الردود
14659 أدوات العرض

Hello All,

How to get keys and values both form dict?

For eg. i want "(u'option2', True): 3" for res.

res = {(False, True): 1, (u'option2', True): 3, (u'option1', True): 2}

Thanks in advance

الصورة الرمزية
إهمال

Hi Pawan,

You need to use iteritems.

res = {(False, True): 1, (u'option2', True): 3, (u'option1', True): 2}

for key,value in res.iteritems():

print key, value

output:

(False, True) 1

(u'option2', True) 3

(u'option1', True) 2

Hope it helps,

Regards,

Mayank Gosai

If you want to use for python 3.

You have to use res.items() instead of res.iteritems().

For ex:

res = {(False, True): 1, (u'option2', True): 3, (u'option1', True): 2}

for key,value in res.items():

(print (key,value))

Output:

(False, True) 1

('option2', True) 3

('option1', True) 2

Thanks,

Mayank Gosai

أفضل إجابة

Hi,

refer this:

https://stackoverflow.com/questions/8023306/get-key-by-value-in-dictionary

Accept answer if helpful..

Thank you.

الصورة الرمزية
إهمال
المنشورات ذات الصلة الردود أدوات العرض النشاط
2
نوفمبر 22
12488
0
مارس 21
24
1
يناير 18
15505
1
فبراير 25
1059
0
نوفمبر 23
1313