Conversions between arrays, tuples, dictionaries, and strings in Python

Source: Internet
Author: User

1. Dictionaries

Dictionary to String

>>> dict={' name ': ' Zhzhgo ', ' Age ':25}>>> print type (str (dict)), str (dict) <type ' str ' > {' age ': ' Name ': ' Zhzhgo '}>>>

Dictionary conversion to tuples

>>> dict={' name ': ' Zhzhgo ', ' Age ':25}>>> print type (tuple (dict)), tuple (dict) <type ' tuple ' > (' Age ', ' name ') >>>

Dictionary to List

>>> dict={' name ': ' Zhzhgo ', ' Age ':25}>>> Print type (list (dict)), List (dict) <type ' list ' > [' Age ' ', ' name ']>>>


2, meta-group

Tuples converted to strings

>>> mytupe= >>> Print mytupe.__str__ () (1, 2, 3) >>> >>> mylist= (' h ', ' e ', ' l ', ' l ', ' o ') >>> print '. Join (MyList) hello>>>

Tuples to List

>>> mytupe= (mytupe) >>> Print List [1, 2, 3]>>>

Tuples cannot be converted to dictionaries


3. List

List to String

>>> mylist=[' h ', ' e ', ' l ', ' l ', ' O ']>>> print '. Join (MyList) hello>>>


List to tuple

>>> mylist=[1,2,3]>>> Print Tuple (mylist) (1, 2, 3) >>>


List cannot be converted to dictionary


4. String

Convert string to tuple

>>> mystring= "Hello" >>> print tuple (mystring) (' H ', ' e ', ' l ', ' l ', ' o ') >>>

String to List

>>> mystring= "Hello" >>> Print List (mystring) [' H ', ' e ', ' l ', ' l ', ' O ']>>>

String to Dictionary

>>> mystring= "{' name ': ' Zhzhgo ', ' age ': +} ' >>> print type (eval (mystring)) <type ' Dict ' >> >>

This article from "Today's efforts, tomorrow's success!" "Blog, be sure to keep this provenance http://zhzhgo.blog.51cto.com/10497096/1671868

Conversions between arrays, tuples, dictionaries, and strings in Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.