iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 5
0

list和其他資料型態的轉換

  • list <-> string
    • join() and split()
  • list <-> tuple
    • tuple() and list()
  • list <-> set
    • set() and list()
  • list <-> dict
    • dict(), zip(), dict.values()

list <-> string

# list to string transformation
url_src = ['ithelp', 'ithome','com','tw']
joined_url = '.'.join(url_src)
print( "url_src:{}".format(url_src) )
print( "joined_url:{}".format(joined_url) )

# url_src:['ithelp', 'ithome', 'com', 'tw']
# joined_url:ithelp.ithome.com.tw

# string to list transformation
url = 'https://ithelp.ithome.com.tw'
url_part1 = url.split('://')
url_part2 = url_part1[1].split('/')
print( "url:{}".format(url) )
print( "url_part1:{}".format(url_part1) )
print( "url_part2:{}".format(url_part2) )

# url:https://ithelp.ithome.com.tw
# url_part1:['https', 'ithelp.ithome.com.tw']
# url_part2:['ithelp.ithome.com.tw']

list <-> tuple

# list to tuple transformation
tags = ['apple', 'htc', 'sony', 'samsung', 'mi']
print( "tags:{}".format(tags) )
print( "type of tags:{}".format(type(tags)) )
tags_transform = tuple(tags)
print( "tags_transform:{}".format(tags_transform) )
print( "type of tags_transform:{}".format(type(tags_transform)) )

# tags:['apple', 'htc', 'sony', 'samsung', 'mi']
# type of tags:<class 'list'>
# tags_transform:('apple', 'htc', 'sony', 'samsung', 'mi')
# type of tags_transform:<class 'tuple'>

# tuple to list transformation
tags_transform_back = list(tags_transform)
print( "tags_transform_back:{}".format(tags_transform_back) )
print( "type of tags_transform_back:{}".format(type(tags_transform_back)) )

# tags_transform_back:['apple', 'htc', 'sony', 'samsung', 'mi']
# type of tags_transform_back:<class 'list'>

list <-> set

# list to set transformation (remove dupicate)
color = ['red', 'blue', 'black', 'white', 'yellow', 'red', 'black']
print( "color:{}".format(color) )
print( "type of color:{}".format(type(color)) )
color_transform = set(color)
print( "color_transform:{}".format(color_transform) )
print( "type of color_transform:{}".format(type(color_transform)) )

# color:['red', 'blue', 'black', 'white', 'yellow', 'red', 'black']
# type of color:<class 'list'>
# color_transform:{'red', 'white', 'blue', 'black', 'yellow'}
# type of color_transform:<class 'set'>

# set to list transformation
color_transform_back = list(color_transform)
print( "color_transform_back:{}".format(color_transform_back) )
print( "type of color_transform_back:{}".format(type(color_transform_back)) )

# color_transform_back:['red', 'white', 'blue', 'black', 'yellow']
# type of color_transform_back:<class 'list'>

list <-> dict

# list to dict transformation
# 1st method
home = ['1f', 'park', '2f', 'livingroom', '3f', 'bathroom']
print( "home:{}".format(home) )
print( "type of home:{}".format(type(home)) )
zip_home = zip(home)
print( "home after zip:{}".format(list(zip_home)) )
home_transform = dict(zip(home[0::2], home[1::2]))
print( "home_transform:{}".format(home_transform) )
print( "type of home_transform:{}".format(type(home_transform)) )

# home:['1f', 'park', '2f', 'livingroom', '3f', 'bathroom']
# type of home:<class 'list'>
# home after zip:[('1f',), ('park',), ('2f',), ('livingroom',), ('3f',), ('bathroom',)]
# home_transform:{'1f': 'park', '2f': 'livingroom', '3f': 'bathroom'}
# type of home_transform:<class 'dict'>

# 2nd method
home_transform_2 = {home[x]: home[x+1] for x in range(0, len(home), 2)}
print( "home_transform_2:{}".format(home_transform_2) )
print( "type of home_transform_2:{}".format(type(home_transform_2)) )

# home_transform_2:{'1f': 'park', '2f': 'livingroom', '3f': 'bathroom'}
# type of home_transform_2:<class 'dict'>

# 3rd method
i = iter(home)
print( "home after iter:{}".format(i) )
print(dict(zip(i, i)))

# home after iter:<list_iterator object at 0x7f35b23e0d68>
# {'1f': 'park', '2f': 'livingroom', '3f': 'bathroom'}

# dict to list transformation
home_transform_back = list(home_transform.values())
print( "home_transform_back:{}".format(home_transform_back) )
print( "type of home_transform_back:{}".format(type(home_transform_back)) )

# home_transform_back:['park', 'livingroom', 'bathroom']
# type of home_transform_back:<class 'list'>

移除重複資料

利用set的元素不能重複的性質

計算資料出現數量

利用移除重複資料和count的方式計算list中資料出現的次數

# counting all items in a list
color = ['red', 'blue', 'black', 'white', 'yellow', 'red', 'black']
print( "color:{}".format(color) )
print( "type of color:{}".format(type(color)) )
counting_colors = [[x, color.count(x)] for x in set(color)]
print( "counting_colors:{}".format(counting_colors) )

# color:['red', 'blue', 'black', 'white', 'yellow', 'red', 'black']
# type of color:<class 'list'>
# counting_colors:[['red', 2], ['white', 1], ['blue', 1], ['black', 2], ['yellow', 1]]

參考


上一篇
Day4-List-特性
下一篇
Day6-String-宣告
系列文
if len(learning.python) == 30:31

尚未有邦友留言

立即登入留言