1. python 的list和string...
list是列表,string是字元串。列表的格式就是[],[],[]
你想獲得的是列表中的第一個字元串而已。所以你用列表中的第一個值就可以
所以用s[0]
2. python中 關於 list 和 string 的問題
問題1
def make_numberlist(first,last):
num=[i for i in range(first,last)]
return num
問題2
def common_element(list1,list2):
c=[]
for i in range(len(list1)):
if list1[i] in list2 and list1[i] not in c:
c.append(list1[i])
return c
問題3
def remove_section(alist,start,end):
a=alist[:]
del a[start:end+1]
return a
問題4
def add_lists(lista,listb):
c=[]
for i in range(len(lista)):
c.append(lista[i]+listb[i])
return c
3. python中,關於list和string的說法,錯誤的是
選 B。 list可以存放任意類型,但不是有序的,否則也不會有sort方法了。len實際上通過__len__來實現的,對string 和list都支持。string、list都可變,python不可變的是tuple
4. Python中怎麼把list轉換為字元串
List中存的是字元串的時候,一般是通過join()函數去轉換:
例 :
dataList = ['1', '2', '3', '4' ]
str1 = 「 , 」 + join(dataList )
print (dataList)
結果:
a b c d
(4)pythonliststrint擴展閱讀
關於join()函數:
join()是一個字元串方法,它返回被子字元串連接的字元串。
參數:The join() method takes join()方法需要可迭代的元素來一次返回它的一個成員,比如列表,元組,字元串,字典和集合
返回值:join()方法返回一個被子字元串連接的字元串。
Type Error: 如果這個可迭代元素包含任何不是字元串的值,join()函數就會拋出TypeError。
5. python List與String 轉化問題
python List與String 轉化運行出錯,是代碼錯誤造成的,解決方法如下:
1、先在eclipse中創建一個Java工程文件,並在src下創建類Demo。
6. python 裡面怎麼把list裡面的string的標點符號去掉
假設你要從string中刪除『a』字元,那麼可以嘗試如下代碼:
myList=['abcde','acedfh','sddibadfa']
newList=[myL.replace('a','')formyLinmyList]
printnewList
其他字元同理
以上!!!
7. python中如何計算一個list里有幾個相同的string
a=[1,2,3,1,2]
print len(a)-len(set(a))
set返回的是沒有重復的組,長度之差就是有幾個重復
8. 在python里的list問題求教!!!!!
#coding: utf-8
import re
def split_on_separators(original, separators):
# 這個是用正則實現的,可能不滿足要求,不過非常簡單
# return filter(lambda x:x.strip(), re.split(r"[%s]" % separators, original))
result = [original]
for sep in separators:
temp = []
for r in result:
temp.extend(filter(lambda x:x.strip(), r.split(sep)))
result = temp
return result
if __name__ == "__main__":
print split_on_separators("I want to test this function.", "ti")
9. python 怎麼把一個list類型的字元串數組
首先我們明確,Python的list可以容納任何對象。不管他是什麼格式的。
1)創建list
創建列表的操作非常簡單,只要用中括弧報過一系列逗號分隔的值就可以了。就像第一個圖片上那樣。
2)list 切片
定義新列表後,我們可以列表中的任何一個部分作為新的列表,該技術在dive in python 中稱為切片。
讓我們直接看程序。
10. 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