程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python STR list dict tuple] - string, list, dictionary, tuple conversion set

編輯:Python

Point a favor and leave a note !!

list :list       read-write       repeatable        Store as value       Orderly      initial ['a',1]       How to add append

Tuples :tuple    Read only       repeatable       Store as value     Orderly       initial ('a',1)        Cannot add

Dictionaries :   dict        read-write          repeatable         Key pair value ( Key is not repeatable )      disorder       initial {‘a’:1,'b':2}      add to c['key']='value'

Catalog

One 、 list 、 Dictionaries 、 character string ----> Turn a tuple

Two 、 Dictionaries 、 Tuples 、 character string ----> To list

3、 ... and 、 list 、 Tuples 、 Dictionaries ----> Turn the string

Four 、 character string ----> Turn Dictionary , and key And value Interturn


One 、 list 、 Dictionaries 、 character string ----> Turn a tuple

# List transfer --> Tuples
print(tuple([' Zhang San ',' Li Si '])) # (' Zhang San ', ' Li Si ')
print(tuple(eval("[' Zhang San ',' Li Si ']"))) # (' Zhang San ', ' Li Si ')
# Dictionary transfer --> Tuples
print(tuple({' Zhang San ',' Li Si '})) # (' Zhang San ', ' Li Si ')
print(tuple(eval("{' Zhang San ',' Li Si '}"))) # (' Zhang San ', ' Li Si ')
dict_list = {' Zhang San ':30,' Li Si ':90}
print(tuple(dict_list.values())) # (30, 90)
print(tuple(dict_list)) # (' Zhang San ', ' Li Si ')
# String rotation --> Tuples
print(tuple('abcd')) # ('a', 'b', 'c', 'd')
# String rotation --> List transfer --> Tuples
list_s = [] # Create an empty list
list_s.append('abcd') # take abcd Write to list
print(tuple(list_s)) # ('abcd',)

Two 、 Dictionaries 、 Tuples 、 character string ----> To list

# Dictionary transfer --> list
print(list({' Zhang San ',' Li Si '})) # [' Zhang San ', ' Li Si ']
print(list(eval("{' Zhang San ',' Li Si '}"))) # [' Zhang San ', ' Li Si ']
# Turn a tuple --> list
print(list((' Zhang San ',' Li Si '))) # [' Zhang San ', ' Li Si ']
print(list(eval("(' Zhang San ',' Li Si ')"))) # [' Zhang San ', ' Li Si ']
# String rotation --> list
print('hello python'.split(' ')) # ['hello', 'python']
# String rotation --> list
list = [] # Create an empty list
list.append('abcd') # take abcd Write to list
print(list) # ['abcd']

3、 ... and 、 list 、 Tuples 、 Dictionaries ----> Turn the string

# List transfer --> character string
print(''.join(['a', 'b', 'c', 'd'])) # abcd
# Turn a tuple --> character string
print(''.join(('a', 'b', 'c', 'd'))) # abcd
# Dictionary transfer --> character string
print(str({'a': 1, 'b': 2})) # {'a': 1, 'b': 2}

Four 、 character string ----> Turn Dictionary , and key And value Interturn

# Tuples cannot be converted to dictionaries
# List cannot be converted to dictionary
# String rotation --> Dictionaries
print(eval("{'a': 1, 'b': 2}")) # {'a': 1, 'b': 2}
# Dictionaries key and value Value conversion
dic1 = {'a': 1, 'b': 2, 'c': 3}
dic2 = {value: key for key, value in dic1.items()}
print(dic2)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved