顯示具有 string 標籤的文章。 顯示所有文章
顯示具有 string 標籤的文章。 顯示所有文章

2017年9月5日 星期二

Python - Convert String to list and Convert list to dictionary in Python - 字串藉由指定符號轉為list與list轉為字典

Python version :Python 3.6.0 :: Anaconda 4.3.1 (64-bit)
System version :Windows 10
str1 = '背景,0,特性,1,元件,3'
list1 = str1.split(",")
dic1 = {list1[i]: list1[i+1] for i in range(0, len(list1), 2)}
print(list1)
print(dic1)
執行結果:
['背景', '0', '特性', '1', '元件', '3']
{'背景': '0', '特性': '1', '元件': '3'}

2017年7月4日 星期二

Python - String-字串常用方法

Python版本:Python 3.6.0 :: Anaconda 4.3.1 (64-bit)
系統版本:Windows 10
字串切割,透過指定符號,常用來剖析網址,以下為剖析氣象局網址
# 字串切割,得到list
Str1 = 'http://www.cwb.gov.tw/V7/forecast/'
Str_list = Str1.split('/')
print(Str_list)
print(Str_list[4])
執行結果:
['http:', '', 'www.cwb.gov.tw', 'V7', 'forecast', '']
forecast