首页 > 其他分享 >list_tuple

list_tuple

时间:2023-03-07 21:47:06浏览次数:34  
标签:__ .__ tuple tup list print sizeof append

l = [1,2,"hello","world"]
tup = ("jody",32)
print(l)
[1, 2, 'hello', 'world']
print(tup)
('jody', 32)
l = [1,2,3,4]
l[3] = 40
print(l)
[1, 2, 3, 40]
tup = (1,2,3,4)
print(tup)
(1, 2, 3, 4)
# add elements
new_tup = tup+(5,)
print(new_tup)
(1, 2, 3, 4, 5)
l.append(5)
print(l)
[1, 2, 3, 40, 5]
l = []
l.append('hello')
# 片切操作
l = [1,2,3,4]
print(l[1:3])
tup = (1,2,3,4)
print(tup[1:3])
[2, 3]
(2, 3)
#嵌套
l = [[1,2,3],[4,5]]
tup = ((1,2,3),(4,5,6))
print(l)
print(tup)
[[1, 2, 3], [4, 5]]
((1, 2, 3), (4, 5, 6))
# 相互转换
print(list((1,2,3)))
print(tuple([1,2,3]))

[1, 2, 3]
(1, 2, 3)
#内置函数
l = [3,2,3,7,8,1]
#统计3的数量
print(l.count(3))
2
#找到7的下标
print(l.index(7))
3
# 反转list
l.reverse()
print(l)
[3, 2, 3, 7, 8, 1]
#排序(ASC)
l.sort()
print(l)
[1, 2, 3, 3, 7, 8]
tup = (3,2,3,7,8,1)
print(tup.count(3))
2
print(tup.index(7))
3
print(list(tup))
print(list(reversed(tup)))
[3, 2, 3, 7, 8, 1]
[1, 8, 7, 3, 2, 3]
print(sorted(tup))
[1, 2, 3, 3, 7, 8]
print(sorted(l))
[1, 2, 3, 3, 7, 8]
# print(tup.sort()) wrong code
---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

<ipython-input-34-10257b8d26da> in <module>
----> 1 print(tup.sort())


AttributeError: 'tuple' object has no attribute 'sort'
# 列表和元组的存储差异
l = [1,2,3]
tup = (1,2,3)
print(l.__sizeof__())
64
print(tup.__sizeof__())
48
l = []
print(l.__sizeof__())
40
l.append(1)
print(l)
print(l.__sizeof__())
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
240
l.append(2)
print(l.__sizeof__())
240
l.append(3)
print(l.__sizeof__())
240
l.append(4)
print(l.__sizeof__())
240
l.append(5)
print(l.__sizeof__())
240
l.append(6)
print(l.__sizeof__())
240
l.append(7)
print(l.__sizeof__())
240
l.append(8)
print(l.__sizeof__())
240
l.append(9)
print(l.__sizeof__())
240
l.append(10)
print(l.__sizeof__())
320

标签:__,.__,tuple,tup,list,print,sizeof,append
From: https://www.cnblogs.com/ukzq/p/17189776.html

相关文章

  • RecyclerView-实现ListView效果
    在Android开发中,我们经常需要实现这样一个下拉的浏览列表:这非常有用,典型的例子就是我们的微信好友列表,可以显示出好友的头像昵称等,还能点击实现更多功能,今天我学习了一个......
  • java微基准测试JMH引入报错RuntimeException: ERROR: Unable to find the resource: /
    项目引入JMH进行性能测试,完整demo业务类:packagecom.simon.benchmark;/***@Author:huzhiyang*@Date:2023/3/717:03*@Desc:*/publicclassBizService{......
  • 02、CMakeLists.txt基本写法
    cmake_minimum_required(VERSION3.10)#设置c++标准#set(CMAKE_CXX_STANDARD11)#设置输出文件存放目录set(CMAKE_RUNTIME_OUTPUT_DIRECTORY${CMAKE_BINARY_DIR}/......
  • 82. Remove Duplicates from Sorted List II
    ##题目Givenasortedlinkedlist,deleteallnodesthathaveduplicatenumbers,leavingonlydistinctnumbersfromtheoriginallist.Forexample,Given1->......
  • 86. Partition List
    ##题目Givenalinkedlistandavaluex,partitionitsuchthatallnodeslessthanxcomebeforenodesgreaterthanorequaltox.Youshouldpreservethe......
  • ArrayList和LinkedList的区别
    实现接口不同。两个都实现了List接口,LinkedList还实现了Deque接口。底层实现不同。ArrayList是基于数组实现,LinkedList是基于链表实现。效率存在差异。由于底层实现不同......
  • 大白话+画图 从源码角度一步步搞懂ArrayList和LinkedList的使用
    1.说说ArrayList1.基本原理ArrayList,原理就是底层基于数组来实现。01.基本原理:数组的长度是固定的,java里面数组都是定长数组,比如数组大小设置为100,此时你不停的往Arra......
  • LinkedList 源码解读
    1.创建 LinkedListList<String>list=newLinkedList<>();list.add("wang");2.构造方法:开起了什么都没有做/***Constructsanemptylist.*/......
  • ArrayList源码解读
    1.创建ArrayListList<String>list=newArrayList<>();list.add("wang");2.构造方法:elementData的长度就是ArrayList的容量,在第一次使用时,elementData的长度会扩展......
  • phthon字符与list常用方法和属性
    字符methods:count:统计字符在字符串中出现的次数(returnint)[searchStr,startIndex,endIndex]mypty='Thisisademoofthecountmethodofstr'print(my......