導航:首頁 > 編程語言 > pythonlist轉碼

pythonlist轉碼

發布時間:2023-12-15 04:08:29

A. python list轉換string

此處直接報錯
Traceback (most recent call last):
File "<pyshell#27>", line 1, in <mole>
print(" ".join(list1))
TypeError: sequence item 0: expected str instance, int found

解決方案:
print(" ".join('%s' %id for id in list1))

即遍歷list的元素,把他轉化成字元串。這樣就能成功輸出1 two three 4結果了。

原文鏈接: http://t.csdn.cn/AwUqX

B. Python3 字元串str和列表list轉換

>>> str1 = "abcdefg"

>>> list1 = list(str1)

>>> print(list1)

['a', 'b', 'c', 'd', 'e', 'f', 'g']

>>> str4 = "username=admin&passsword=123456"

>>> list4 = str4.split("純祥行&")

>>> print(type(list4))

<class 'list'>

>>> print(list4)

['username=admin', 'passsword=123456']

如果我們要對多個字元進行分割,那麼可以使用內置模塊 re.split() 方法。

>>> str5 = "username=admin&passsword=123456"

>>> import re

>>> list5 = re.split("&|=", str5)

>>> print(type(list5))

<class 'list'>

>>> print(list5)

['username', 'admin', 'passsword', '123456']

>>> import json

>>> str3 = '["aaa", "bbb", "ccc", "ddd"]'

>>> list3 = json.loads(str3)

>>> print(type(list3))

<class 'list'>

>>> print(list3)

['aaa', 'bbb', 'ccc', 'ddd']

>>> str2 = "['aaa', 'bbb', 'ccc', 'ddd']"

>>> list2 = eval(str2)

>>> print(type(list2))

<class 'list'>

>>> print(list2)

['aaa', 'bbb', 'ccc', 'ddd'宴閉]

針對str2,json.loads()方法為何失靈了?

因為 json.loads() 將json格式字元串轉換為python對象,而按 json 的標准規范應該使用雙引號,如做嘩果使用單引號會導致報錯。

# 注意,轉換之後,雙引號會變為單引號

>>> list1 = ["aaa", 123, 'ccc', True]

>>> str1 = str(list1)

>>> print(type(str1))

<class 'str'>

>>> print(str1)

['aaa', 123, 'ccc', True]

>>> list3 = ['username=admin', 'passsword=123456']

>>> str3 = "&".join(list3)

>>> print(type(str3))

<class 'str'>

>>> print(str3)

username=admin&passsword=123456

# 這里列表中使用了單引號

>>> list4 = ['username=admin', 'passsword=123456']

>>> import json

>>> str4 = json.mps(list4)

>>> print(type(str4))

<class 'str'>

>>> print(str4)

["username=admin", "passsword=123456"]

Python3下字典、字元串及列表的相互轉換

C. python如何將list中的字元轉為數字

python裡面好像只能直接轉一維的list,以python 3.6為例:

問題 1:

list=['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

轉化為:list=[0, 1 ,2, 3, 4, 5, 6, 7, 8, 9]

代碼如下:

list_to_float=list(map(lambdax:float(x),list))

問題2:(對於二維數組,需要加個循環,變成一維數組)

list=[['0', '1', '2'], ['3', '4', '5'], ['6', '7', '8']]

轉化為:list=[[0, 1 ,2], [3, 4, 5], [6, 7, 8]]

代碼如下:

list_to_float=[]
foreachinlist:
each_line=list(map(lambdax:float(x),each))
list_to_float.append(each_line)

總之:關鍵還是map函數映射,如果是python 2.x的話,你可以試試

list_to_float=map(lambdax:float(x),list)

D. Python中怎麼把list轉換為字元串

List中存的是字元串的時候,一般是通過join()函數去轉換:

例 :
dataList = ['1', '2', '3', '4' ]

str1 = 「 , 」 + join(dataList )

print (dataList)

結果:
a b c d

(4)pythonlist轉碼擴展閱讀

關於join()函數:

join()是一個字元串方法,它返回被子字元串連接的字元串。

參數:The join() method takes join()方法需要可迭代的元素來一次返回它的一個成員,比如列表,元組,字元串,字典和集合

返回值:join()方法返回一個被子字元串連接的字元串。

Type Error: 如果這個可迭代元素包含任何不是字元串的值,join()函數就會拋出TypeError。

閱讀全文

與pythonlist轉碼相關的資料

熱點內容
android開發音樂播放器 瀏覽:806
ug120陣列命令快捷鍵 瀏覽:595
氣動隔膜式壓縮機 瀏覽:470
linux如何修改主機名 瀏覽:104
單片機游標上下移動 瀏覽:528
數據加密驗證 瀏覽:108
程序員被激怒 瀏覽:891
winxp找不到伺服器dns地址 瀏覽:842
以文本文件的格式保存考生文件夾 瀏覽:41
編譯原理文法分為幾類 瀏覽:570
JAVA基礎學python要多久 瀏覽:74
java流量控制 瀏覽:936
java實現多重繼承 瀏覽:707
票據通加密狗怎麼在新系統上使用 瀏覽:795
航模加密狗連接電腦 瀏覽:473
好用的匯編語言編譯器 瀏覽:863
自己編譯安卓虛擬機 瀏覽:913
中國的古代演算法 瀏覽:656
上層怎麼看程序員 瀏覽:27
程序員便當排骨 瀏覽:855