首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往
  • 您找到你想要的搜索结果了吗?
    是的
    没有找到

    Python数据分析(中英对照)·Lists 列表

    列表是任何类型的对象的可变序列。 Lists are mutable sequences of objects of any type. 它们通常用于存储同质项目。 And they’re typically used to store homogeneous items. 列表是序列的一种类型,就像字符串一样,但它们确实有区别。 Lists are one type of sequence, just like strings but they do have their differences. 如果我们比较字符串和列表,一个区别是字符串是单个字符的序列, If we compare a string and a list, one difference is that strings are sequences of individual characters, 而列表是任何类型Python对象的序列。 whereas lists are sequences of any type of Python objects. 字符串和列表之间的另一个区别是字符串是不可变的,而列表是可变的。 Another difference between strings and lists is that strings are immutable, whereas lists are mutable. 除了这两个区别之外,字符串和列表当然也有自己的方法。 In addition to these two differences, strings and lists, of course,come with their own methods. 通常情况下,列表只包含一种类型的对象,尽管这不是严格的要求。 It is common practice for a list to hold objects of just one type,although this is not strictly a requirement. 让我们尝试几个简单的列表来测试它们。 Let’s try a couple of simple lists to experiment with them. 让我们构造一个简单的数字列表,以进一步了解列表。 Let’s construct a simple list of numbers to learn a little bit more about lists. 所以我要构造一个数字列表。 So I’m going to construct a list of numbers. 我要称之为数字。 I’m going to call it numbers. 我将使用数字2、4、6和8。 And I’ll use numbers 2, 4, 6, and 8. 假设我想提取或访问列表中的第一个元素。 Imagine I wanted to extract, or access, the first element of my list. 我要做的第一件事是键入列表的名称,然后我需要方括号。 The first thing for me to do is type the name of the list,then I need my square brackets. 现在请记住,在Python中,索引从零开始。 Now remember, in Python, indexes start at zero. 因此,为了能够查看该列表的第一个元素,我需要将其放入索引0,位置0。 So for me to be able to look at the first element of that list,I need to put in index 0, position 0. 在这里,Python告诉我第一个对象,即位于位置0的对象,是数字2。 Here, Python tells me that the first object, meaning the object located at position 0, is number 2. 如果我将索引更改为1,Python将给我第二个对象。 If I change the index to 1, Python gives me the second object. 现在,如果我想知道列表上最后一个对象是什么,我可以从右到左计算位置。 Now if I wanted to find out what is the very last object on my list,I can count positions from right to left. 这意味着我必须使用负指数。 And

    02

    Python学习笔记整理(一)pytho

    Python对象类型 说明:python程序可以分解成模块,语句,表达式以及对象。 1)、程序由模块构成 2)、模块包含语句 3)、语句包含表达式 4)、表达式建立并处理对象 一、使用内置类型 除非有内置类型无法提供的特殊对象需要处理,最好总是使用内置对象而不是使用自己的实现。 二、python的核心数据类型 对象类型     例子 常量/创建 数字        1234,3.1414,999L,3+4j,Decimal 字符串        'diege',"diege's" 列表        [1,[2,'three'],4] 字典        {'food':'spam','taste':'yum'} 元组(序列)    (1,‘span',4,'u') 文件        myfile=open('eggs'.'r') 其他类型    集合,类型,None,布尔型 还有模式对象,套接字对象等等。。其他的类型的对象都是通过导入或者使用模块来建立的。 由字符组成的字符串,由任意类型的元素组成的列表。这两种类型的不同之处在于,列表中的元素能够被修改,而字符串中的字符则不能被修改。换句话说,字符串的值是固定的,列表的值是可变的。元组的数据类型,它和列表比较相近,只是它的元素的值是固定的。列表和字典都可以嵌套,可以随需求扩展和删减。并能包含任意类型的对象。 Python中没有类型声明,运行的表达式,决定了建立和使用对象的类型。同等重要的是,一旦创建了一个对象。它就和操作结合绑定了--只可以对字符串进行字符串相关操作。对列表进行相关操作。Python是动态类型(它自动地跟踪你的类型而不是要求声明代码),但是它也是强类型语言(只能对一个对象性有效操作). 三、数字 整数,浮点,长整型等 支持一般的数学运算:+,- * % **(乘方) 5L,当需要有额外的精度时,自动将整型变化提升为长整型。 除表达式,python还有一些常用的数学模块和随机数模块 >>>import math >>> dir(math) >>> math.log(1) 0.0 >>> import random >>> dir(random) 四、字符串 1、是一个个单个字符的字符串的序列。 >>> s[1] 'i 第一个字符的序列是0 >>> s[0] 'd 通过字符找到索引编号 >>> S.index('a') 0 除了简单的从位置进行索引,序列也支持一种所谓分片的操作。 >>> s='diege' >>> s[1:3] 'ie'包括左边的位置不包括右边的位置 >>> s[:3] 'die' 开头到第三个(不包括第3个) >>> s[3:] 'ge' 第三个到最后(包括第3个) >>> s[:] 'diege' 所有 >>> s[-1] 'e' 倒数第1个 2、序列可以通过len()函数获取长度 >>> s='diege' >>> len(s) 5 可以根据序列定位字符串里的字符,序列从0开始 >>> s[0] 'd 可以使用反向索引 >>> s[-1] 'e' >>> s[len(s)-1]    'e'

    02
    领券