python3.xのリスト、元祖、文字列間の相互変換(詳細)

2381 ワード

python3.xのリスト、元祖、文字列間の相互変換(詳細)
str1 = 'helloWord'            #   
list1 = [' ',' ',' ',' ']   #  (       )
list2 = [' ',' ',1,2,3]
tuple1 = (' ',' ',' ',' ')  #  
tuple2 = '  ','  ','   ',666   #       

#      
strtolist = list(str1)
print(strtolist)
#['h', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'd']

#      
strtotuple = tuple(str1)
print(strtotuple)
#('h', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'd')

#      (  )                      
listtoStr = str(list1)
print(listtoStr)
#[' ', ' ', ' ', ' ']

#  (    )    (  )
listtoStr1 = ''.join(list1)
print('listtoStr1   :{}'.format(listtoStr1))
#listtoStr1   :    

'''
#  (    )    (  )
listtoStr2 = ''.join(list2)
print('listtoStr1   :{}'.format(listtoStr2))
#    :sequence item 2: expected str instance, int found
#           ,  int       .join   
#              ,       
'''

listInStr = [str(i) for i in list2]
listtoStr3 = ''.join(listInStr)
print('       ,     :{}'.format(listtoStr3))
#       ,     :  123


#     
listToTuple = tuple(list1)
print(listToTuple)
#(' ', ' ', ' ', ' ')

'''
#      (  )
tupleToStr = str(tuple1)
print(tupleToStr)
#(' ', ' ', ' ', ' ')
'''
#  (     )    ,  
tupleToStr1 = ''.join(tuple1)
print('         :{}'.format(tupleToStr1))
#         :    

'''
#  (    )    
tupleToStr2 = ''.join(tuple2)
print('         :{}'.format(tupleToStr2))
#  sequence item 3: expected str instance, int found
'''


#               ,       ,            str
tupleToStr3 = (str(i) for i in tuple2)
#   tupleToStr3 = [str(i) for i in tuple2]    ,   
tupleToStr3 = ''.join(tupleToStr3)
print('     (    )    :{}'.format(tupleToStr3))
#     (    )    :       666

#     
tupleTolist = list(tuple1)
print(tupleTolist)
#[' ', ' ', ' ', ' ']


#  (    )   
tupleTolist1 = list(tuple2)
print(tupleTolist1)
#['  ', '  ', '   ', 666]