简体   繁体   中英

dict_values to string in python

I simply want to convert an object that looks like this:

dict_values(['baf0242b-d7fc-49d7-aada-220344969fb6'])

That I got from doing this:

dictionary.values()

To a simple string: 'baf0242b-d7fc-49d7-aada-220344969fb6'

How can I?

Say you have a dictionary:

d = {'key': 'baf0242b-d7fc-49d7-aada-220344969fb6'}

The result you see is from running the following:

d.values()

To get what you want, convert it to a list and get the first item on it:

d = {'key': 'baf0242b-d7fc-49d7-aada-220344969fb6'}
result = list(d.values())[0]

list(your_dict.values())[0]

正如@rdas 所提到的, dict_values是 Python 3+ 中的一个view ,第一个索引将返回您的字符串。

If you are getting the first value, or the only value in your case. This might be a bit more memory efficient:

next(iter(d.values())))

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