Python对数组的基本操作

# coding=utf-8
创建并打印数组
‘‘‘
arr = ["aex", "bfe", "mpilgrim", "zddd", "example"];
print(arr);#[‘aex‘, ‘bfe‘, ‘mpilgrim‘, ‘zddd‘, ‘example‘]
print(arr[2]);#mpilgrim
‘‘‘
数组的负索引
li[-n] == li[len(li) - n]
‘‘‘
print(arr[-1]);#example
‘‘‘
数组的分片
arr[1:3]表示从第一个元素开始,直到但不包含第三个元素
‘‘‘
print(arr[1:3]);#[‘bfe‘, ‘mpilgrim‘]
‘‘‘
向数组中添加元素
‘‘‘
arr.append("new");
print(arr);#[‘aex‘, ‘bfe‘, ‘mpilgrim‘, ‘zddd‘, ‘example‘, ‘new‘]
arr.insert(2, "new");
print(arr);#[‘aex‘, ‘bfe‘, ‘new‘, ‘mpilgrim‘, ‘zddd‘, ‘example‘, ‘new‘]
arr.extend([‘gete‘,‘sdwz‘,‘wettt‘]);
print(arr);#[‘aex‘, ‘bfe‘, ‘new‘, ‘mpilgrim‘, ‘zddd‘, ‘example‘, ‘new‘, ‘gete‘, ‘sdwz‘, ‘wettt‘]
‘‘‘
在数组中搜索元素
‘‘‘
print(arr.index("example"));#5

#print(arr.index("f"));#ValueError: ‘f‘ is not in list
print("example" in arr);#True
‘‘‘
删除数组中的元素
remove是删除元素的首次出现,pop是删除最后一个元素,并且返回最后一个元素
‘‘‘
arr.remove("new");
print(arr);#[‘aex‘, ‘bfe‘, ‘mpilgrim‘, ‘zddd‘, ‘example‘, ‘new‘, ‘gete‘, ‘sdwz‘, ‘wettt‘]
print(arr.pop());#wettt
print(arr);#[‘aex‘, ‘bfe‘, ‘mpilgrim‘, ‘zddd‘, ‘example‘, ‘new‘, ‘gete‘, ‘sdwz‘]
‘‘‘
在数组中使用运算符
arr = [1, 2] * 3 等同于 arr = [1, 2] + [1, 2] + [1, 2]
‘‘‘
arr=arr+[‘fegrc‘,‘getrvs‘];
print(arr);#[‘aex‘, ‘bfe‘, ‘mpilgrim‘, ‘zddd‘, ‘example‘, ‘new‘, ‘gete‘, ‘sdwz‘, ‘fegrc‘, ‘getrvs‘]
arr+=[‘two‘];
print(arr);#[‘aex‘, ‘bfe‘, ‘mpilgrim‘, ‘zddd‘, ‘example‘, ‘new‘, ‘gete‘, ‘sdwz‘, ‘fegrc‘, ‘getrvs‘, ‘two‘]
arr1=[‘dfd‘,‘hrh‘];
print(arr1*3);#[‘dfd‘, ‘hrh‘, ‘dfd‘, ‘hrh‘, ‘dfd‘, ‘hrh‘]
arr2=[3,4];
print(arr2*3);#[3, 4, 3, 4, 3, 4]

时间: 2024-08-30 07:14:09

Python对数组的基本操作的相关文章

实例365(11)---------数组的基本操作(一)

一:获取二维数组的行数与列数,截图 二:代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace ArrayRank { public partial class

No4.数组的基本操作__Java学习笔记

1 public class HelloArrayOp { 2 3 public static void main(String[] args) { 4 // TODO Auto-generated method stub 5 6 System.out.println("第131页:数组的基本操作:" 7 +"\n"+"遍历数组" 8 +"\n"+"填充替换数组元素" 9 +"\n"+&

05 Scala 数组的基本操作,数组的进阶操作和多维数组

1. 数组的基本操作 1)定长数组 数组的概念和C,JAVA中的数组是一样的, 都是存储同一种类型的元素.定长数组存储一定长度的数组.    //声明一个数组,类型为Int,元素个数为10. val nums = new Array[Int](10) //声明一个数组,类型为String元素个数为10 . val a = new Array[String](10) //声明一个数组,初始化第一个元素为'Hello',第二个元素为"World",通过类型推到,判断出//数组的类型为Str

Python numpy数组扩展效率问题

Numpy库的ndarray数组可以方便地进行各种多维数据处理工作 可是它最大的缺点就是不可动态扩展--"NumPy的数组没有这种动态改变大小的功能,numpy.append()函数每次都会重新分配整个数组,并把原来的数组复制到新数组中."(引用自http://blog.chinaunix.net/uid-23100982-id-3164530.html) 场景: 今天用ndarray处理 42000 条数据时,就遇到了数组扩展的效率问题 文件名:train.csv(后附下载) 文件大

python图像数组操作与灰度变换

Python图像数组操作与灰度变换 使用python以及numpy通过直接操作图像数组完成一系列基本的图像处理 numpy简介: NumPy是一个非常有名的 Python 科学计算工具包,其中包含了大量有用的工具,比如数组对象(用来表示向量.矩阵.图像等)以及线性代数函数. 数组对象可以实现数组中重要的操作,比如矩阵乘积.转置.解方程系统.向量乘积和归一化.这为图像变形.对变化进行建模.图像分类.图像聚类等提供了基础. 在上一篇python基本图像操作中,当载入图像时,通过调用 array()

Swift学习笔记(二十)——数组的基本操作

Swift中数组的基本操作具体如下: (1)计算数组长度 . (2)判断数组是否为空 . (3)数组新增一个元素 . (4)数组合并 . (5)  插入元素到任意位置 . (6)删除任意位置的某个元素 (7)删除数组最后一个元素 (8)插入删除操作数组不能越界 如以下操作都是非法的: . (9)修改数组中的某个值 . (10)数组元素的批量替换 其中可以替换的替换的个数可以是不匹配的: . 但是以下的操作是非法的:批量替换也必须是用数组去赋值: (11)数组的遍历 for in 循环遍历: .

LeeCode初级算法的Python实现--数组

LeeCode初级算法的Python实现--数组 # -*- coding: utf-8 -*- """ @Created on 2018/6/3 17:06 @author: ZhifengFang """ # 排列数组删除重复项 def removeDuplicates(nums): if len(nums) <= 1: return len(nums) i = 1 while len(nums) != i: if nums[i] ==

python遍历数组的两种方法的代码

工作过程中,把开发过程中较好的一些内容段备份一下,下面内容是关于python遍历数组的两种方法的内容,希望对小伙伴有用途. colours = ["red","green","blue"] for colour in colours: print colour # red # green # blue 下面的方法可以先获得数组的长度,然后根据索引号遍历数组,同时输出索引号 colours = ["red","gree

Python 切分数组

Python 切分数组 将一个数组,均分为多个数组 代码 # -*- coding:utf-8 -*- # py3 def list_split(items, n): return [items[i:i+n] for i in range(0, len(items), n)] if '__main__' == __name__: list1 = ['s1', 's2', 's3', 's4', 's5', 's6', 's7'] list2 = list_split(list1, 3) prin