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