其他分享
首页 > 其他分享> > 2021-04-01

2021-04-01

作者:互联网

python–list

元素之间只能用逗号分隔
在这里插入图片描述classmates = ['Michael', 'Bob', 'Tracy']

1.查找,定位下标

index(self, value, start=0, stop=9223372036854775807, /)
Return first index of value.
返回value第一次出现的位置索引,start设定查找起点,stop设定查找终点
>>> a
['apple', 'bb', 'aa', 'aa', 1, 2]
>>> a.index('aa')
2
  1. 添加
classmates.append('Adam')
>>> classmates
['Michael', 'Bob', 'Tracy', 'Adam']`
>>> classmates.insert(1, 'Jack')
>>> classmates
['Michael', 'Jack', 'Bob', 'Tracy', 'Adam']
  1. 删除
entry=["1","1","2"]

entry=[entry[i] for i in range(0,len(entry)) if entry[i]!="1"]

print(entry)
c=[1,2,4,1,1,2]
for i in range(len(c)-1,-1,-1):
     if c[i]==1:
          c.remove(1)
print(c)
>>> classmates
['Michael', 'Jack', 'Bob', 'Tracy']
>>> classmates.pop(1)
'Jack'
>>> classmates
['Michael', 'Bob', 'Tracy']
  1. 赋值
>>> classmates[1] = 'Sarah'
>>> classmates
['Michael', 'Sarah', 'Tracy']
  1. 其他
>>> a = ['apple','bb','aa','aa']
>>> a.count('aa')
2
>>> b=[1,2]
>>> a.extend(b)
>>> a
['apple', 'bb', 'aa', 'aa', 1, 2]
>>> s = ['python', 'java', ['asp', 'php'], 'scheme']
>>> len(s)
4

要得到’php’,用s[2,1]=

标签:aa,index,01,classmates,04,Michael,2021,Tracy,Bob
来源: https://blog.csdn.net/qq_42988706/article/details/115378382