python元组和列表

1.list和tuple共同点和区别

共同点:1:里面元素都可以储存不同类型数据,2:都属于序列

不同点: tuple:不可变序列(元素1,元素二)(如果元素里有list,则list中的序列可变)

list:可变序列[元素1, 元素2]

2.定义一个变量,包含现在所学的数据类型

输入

list_data = [1, 1.1, b'a', False, None, 1+2j, '6', (6, 6, 6), [1, 2]]
print(list_data, type(list_data))

输出

[1, 1.1, b'a', False, None, (1+2j), '6', (6, 6, 6), [1, 2]] <class 'list'>

3.目前学到的序列有哪些?

字符串str;字节bytes;元组tuple;列表list

将除tuple之外的序列转换为tuple

输入

str_data = 'a'
bytes_data = b'1'
list_data = [1, 2]
tuple_data1 = tuple(str_data)
tuple_data2 = tuple(bytes_data)
tuple_data3 = tuple(list_data)
print(tuple_data1, type(tuple_data1))
print(tuple_data2, type(tuple_data2))
print(tuple_data3, type(tuple_data3))

输出 

('a',) <class 'tuple'>
(49,) <class 'tuple'>
(1, 2) <class 'tuple'>


将除list之外的序列转换为list

输入

str_data = 'a'
bytes_data = b'1'
tuple_data = (1, 2)
list_data1 = list(str_data)
list_data2 = list(bytes_data)
list_data3 = list(tuple_data)
print(list_data1, type(list_data1))
print(list_data2, type(list_data2))
print(list_data3, type(list_data3))

 输出

['a'] <class 'list'>
[49] <class 'list'>
[1, 2] <class 'list'>

 

4.tuple中有哪些操作方法
count(self, value, /)
      Return number of occurrences of value.
      #统计出现的次数


 index(self, value, start=0, stop=9223372036854775807, /)
      Return first index of value.

     #返回第一个的索引(索引:从0开始)


5.list中有哪些操作方法
   append(self, object, /)
       Append object to the end of the list.

       #将对象追加到列表的末尾。
   
   clear(self, /)
       Remove all items from list.
       #从列表中删除所有项目。


   copy(self, /)
       Return a shallow copy of the list.
       #返回列表的浅层副本。


   count(self, value, /)
       Return number of occurrences of value.
      #返回值的出现次数。


   extend(self, iterable, /)
       Extend list by appending elements from the iterable.
       #通过从可迭代对象追加元素来扩展列表。


   index(self, value, start=0, stop=9223372036854775807, /)
       Return first index of value.
       #返回值的第一个索引。
       Raises ValueError if the value is not present.
      #如果值不存在,则提高值错误。


   insert(self, index, object, /)
       Insert object before index.
       #在索引之前插入对象。


   pop(self, index=-1, /)
       Remove and return item at index (default last).
       #删除并返回索引处的项目(默认在后面)。
       Raises IndexError if list is empty or index is out of range.
       #如果列表为空或索引超出范围,则引发索引错误。


   remove(self, value, /)
       Remove first occurrence of value.
       #删除第一个出现的值。
       Raises ValueError if the value is not present.
       #如果值不存在,则提高值错误。


   reverse(self, /)
       Reverse *IN PLACE*.
      #反向


   sort(self, /, *, key=None, reverse=False)
       Sort the list in ascending order and return None.
       #按升序对列表进行排序,并返回 None。
       The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
       order of two equal elements is maintained).
       #排序是就地的(即列表本身被修改)和稳定的(即保持两个相等元素的顺序)。
       If a key function is given, apply it once to each list item and sort them,
       ascending or descending, according to their function values.
       #如果给出了一个关键功能,则将其应用于每个列表项一次并对其进行排序,
       升序或降序,根据其函数值。
       The reverse flag can be set to sort in descending order.

       #可以将反向标志设置为按降序排序。
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值