python基本数据类型——list

一、创建列表:

li = []
li = list()
name_list = ['alex', 'seven', 'eric']

name_list = list(['alex', 'seven', 'eric'])

二、基本操作:

#append追加
name_list = ["zhangyanlin","suoning","nick"]
name_list.append('zhang')
print(name_list)
 
#count制定字符出现几次
name_list = ["zhangyanlin","suoning","nick"]
name_list.append('zhang')
name_list.append('zhang')
name_list.append('zhang')
print(name_list.count('zhang'))
 
#extend可扩展,批量往里加数据
name_list = ["zhangyanlin","suoning","nick"]
name = ["aylin","zhang","yan","lin"]
name_list.extend(name)
print(name_list)
 
#index找到字符所在的位置
name_list = ["zhangyanlin","suoning","nick"]
print(name_list.index('nick'))
 
#insert插入,往索引里面插入值
name_list = ["zhangyanlin","suoning","nick"]
name_list.insert(1,"zhang")
print(name_list)
 
#pop在原列表中移除掉最后一个元素,并赋值给另一个变量
name_list = ["zhangyanlin","suoning","nick"]
name = name_list.pop()
print(name)
 
#remove移除,只移除从左边找到的第一个
name_list = ["zhangyanlin","suoning","nick"]
name_list.remove('nick')
print(name_list)
 
#reverse反转
name_list = ["zhangyanlin","suoning","nick"]
name_list.reverse()
print(name_list)
 
#del删除其中元素,删除1到3之间的
name_list = ["zhangyanlin","suoning","nick"]
del name_list[1:3]
print(name_list)

#join将列表元素用指定字符串连接
name_list = ["you","are","good"]
s = " ".join(name_list)
print(name_list)
# you are good
class list(object):
    """
    list() -> new empty list
    list(iterable) -> new list initialized from iterable's items
    """
    def append(self, p_object): # real signature unknown; restored from __doc__
        """ L.append(object) -- append object to end """
        pass

    def count(self, value): # real signature unknown; restored from __doc__
        """ L.count(value) -> integer -- return number of occurrences of value """
        return 0

    def extend(self, iterable): # real signature unknown; restored from __doc__
        """ L.extend(iterable) -- extend list by appending elements from the iterable """
        pass

    def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
        """
        L.index(value, [start, [stop]]) -> integer -- return first index of value.
        Raises ValueError if the value is not present.
        """
        return 0

    def insert(self, index, p_object): # real signature unknown; restored from __doc__
        """ L.insert(index, object) -- insert object before index """
        pass

    def pop(self, index=None): # real signature unknown; restored from __doc__
        """
        L.pop([index]) -> item -- remove and return item at index (default last).
        Raises IndexError if list is empty or index is out of range.
        """
        pass

    def remove(self, value): # real signature unknown; restored from __doc__
        """
        L.remove(value) -- remove first occurrence of value.
        Raises ValueError if the value is not present.
        """
        pass

    def reverse(self): # real signature unknown; restored from __doc__
        """ L.reverse() -- reverse *IN PLACE* """
        pass

    def sort(self, cmp=None, key=None, reverse=False): # real signature unknown; restored from __doc__
        """
        L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
        cmp(x, y) -> -1, 0, 1
        """
        pass

    def __add__(self, y): # real signature unknown; restored from __doc__
        """ x.__add__(y) <==> x+y """
        pass

    def __contains__(self, y): # real signature unknown; restored from __doc__
        """ x.__contains__(y) <==> y in x """
        pass

    def __delitem__(self, y): # real signature unknown; restored from __doc__
        """ x.__delitem__(y) <==> del x[y] """
        pass

    def __delslice__(self, i, j): # real signature unknown; restored from __doc__
        """
        x.__delslice__(i, j) <==> del x[i:j]
                   
                   Use of negative indices is not supported.
        """
        pass

    def __eq__(self, y): # real signature unknown; restored from __doc__
        """ x.__eq__(y) <==> x==y """
        pass

    def __getattribute__(self, name): # real signature unknown; restored from __doc__
        """ x.__getattribute__('name') <==> x.name """
        pass

    def __getitem__(self, y): # real signature unknown; restored from __doc__
        """ x.__getitem__(y) <==> x[y] """
        pass

    def __getslice__(self, i, j): # real signature unknown; restored from __doc__
        """
        x.__getslice__(i, j) <==> x[i:j]
                   
                   Use of negative indices is not supported.
        """
        pass

    def __ge__(self, y): # real signature unknown; restored from __doc__
        """ x.__ge__(y) <==> x>=y """
        pass

    def __gt__(self, y): # real signature unknown; restored from __doc__
        """ x.__gt__(y) <==> x>y """
        pass

    def __iadd__(self, y): # real signature unknown; restored from __doc__
        """ x.__iadd__(y) <==> x+=y """
        pass

    def __imul__(self, y): # real signature unknown; restored from __doc__
        """ x.__imul__(y) <==> x*=y """
        pass

    def __init__(self, seq=()): # known special case of list.__init__
        """
        list() -> new empty list
        list(iterable) -> new list initialized from iterable's items
        # (copied from class doc)
        """
        pass

    def __iter__(self): # real signature unknown; restored from __doc__
        """ x.__iter__() <==> iter(x) """
        pass

    def __len__(self): # real signature unknown; restored from __doc__
        """ x.__len__() <==> len(x) """
        pass

    def __le__(self, y): # real signature unknown; restored from __doc__
        """ x.__le__(y) <==> x<=y """
        pass

    def __lt__(self, y): # real signature unknown; restored from __doc__
        """ x.__lt__(y) <==> x<y """
        pass

    def __mul__(self, n): # real signature unknown; restored from __doc__
        """ x.__mul__(n) <==> x*n """
        pass

    @staticmethod # known case of __new__
    def __new__(S, *more): # real signature unknown; restored from __doc__
        """ T.__new__(S, ...) -> a new object with type S, a subtype of T """
        pass

    def __ne__(self, y): # real signature unknown; restored from __doc__
        """ x.__ne__(y) <==> x!=y """
        pass

    def __repr__(self): # real signature unknown; restored from __doc__
        """ x.__repr__() <==> repr(x) """
        pass

    def __reversed__(self): # real signature unknown; restored from __doc__
        """ L.__reversed__() -- return a reverse iterator over the list """
        pass

    def __rmul__(self, n): # real signature unknown; restored from __doc__
        """ x.__rmul__(n) <==> n*x """
        pass

    def __setitem__(self, i, y): # real signature unknown; restored from __doc__
        """ x.__setitem__(i, y) <==> x[i]=y """
        pass

    def __setslice__(self, i, j, y): # real signature unknown; restored from __doc__
        """
        x.__setslice__(i, j, y) <==> x[i:j]=y
                   
                   Use  of negative indices is not supported.
        """
        pass

    def __sizeof__(self): # real signature unknown; restored from __doc__
        """ L.__sizeof__() -- size of L in memory, in bytes """
        pass

    __hash__ = None

list
list源码

 

三、数据类型转换

字符串转列表
s = "你好morra"
li = list(s)
print(li)

OUTPUT:
['', '', 'm', 'o', 'r', 'r', 'a']
元组转列表
tu = ("你好","alex")
li = list(tu)
print(li)

OUTPUT:
['你好', 'alex']
字典转列表
dic = {'k1':'hello','k2':'morra'}
l3 = list(dic)              #字典在循环的时候默认只循环key
print(l3)

l4 = list(dic.values())
print(l4)

l5 = list(dic.items())
print(l5)

OUTPUT:
['k2', 'k1']
['morra', 'hello']
[('k2', 'morra'), ('k1', 'hello')]

四、可迭代性

l = ['i', 'am', 'spark']
# 可以被for循环所迭代
for i in l:
    print (i)
# i am spark

五、可嵌套性

li = ['字符串',('tuple','hh'),{"key1":"value1","key2":"value2"}]
print(li[2]["key1"])
#输出  value1

 

转载于:https://www.cnblogs.com/LiCheng-/p/6431084.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值