python的反转(切片)
看下面代码吧,简单来说不如直接看代码。如下:
#coding=utf-8
__author__ = 'debude'
a = 'python'
print a[::-1] #从最后n开始,每走一位都打印出来。如果改为-2,就是打印n后面的h和y。nhy
print a[:2][::-1] #先取前2位,再反转
print a[::-1][:2] #先反转,再取翻转后的前两位。
还可以各种反转,比如元组 tuple:
#coding=utf-8
__author__ = 'debude'
a = ('python',99,11)
print a[::-1] #从最后n开始,每走一位都打印出来。如果改为-2,就是打印n后面的h和y。nhy
print a[:2][::-1] #先取前2位,再反转
print a[::-1][:2] #先反转,再取翻转后的前两位。
结果为:
(11, 99, 'python')
(99, 'python')
(11, 99)
python的反转(切片)的更多相关文章
- 在python&numpy中切片(slice)
在python&numpy中切片(slice) 上文说到了,词频的统计在数据挖掘中使用的频率很高,而切片的操作同样是如此.在从文本文件或数据库中读取数据后,需要对数据进行预处理的操作.此时就 ...
- Python中的切片符
最近在学python,感觉切片符有点难以理解.在网上查了点资料,然后做个总结 理解切片符,首先得知道数组是从0开始的, 而且切片符最后一个是-1. 我们先定义个数组 a=[1,2,3,4,5] 切 ...
- python迭代和切片
from collections import Iterable #切片************************ # #取一个list或tuple的部分元素是非常常见的操作 ,Python提供 ...
- Python高级特性(切片,迭代,列表生成式,生成器,迭代器)
掌握了Python的数据类型.语句和函数,基本上就可以编写出很多有用的程序了. 比如构造一个1, 3, 5, 7, ..., 99的列表,可以通过循环实现: L = [] n = 1 while n ...
- Python高级教程-切片
Python中的切片 取一个list或tuple的部分元素是非常常见的操作.比如,一个list如下: >>> L = ['A','B','C','D'] 对经常取指定索引范围的操作, ...
- Python中的切片操作
python中的切片操作功能十分强大,通常我们利用切片来进行提取信息,进行相关的操作,下面就是一些切片的列子. 列如我们从range函数1-100中取7的倍数,函数及结果如下所示: >>& ...
- python的字符串切片技术
听说过python的字符串切片技术吗?是不是听着超高级的?实际上,也不用想得太难,python的字符串切片技术就是将字符串的某些字符提取出来而已~ 字符串切片 字符串是一种序列类型,可以按序号访问其中 ...
- Python 字符串反转
方法一: 切片的方法 a = "hello"b = len(a)i = 1c = ""while i<=b: d = a[b-i] c += d i+=1 ...
- Python学习--06切片
Python里提供了切片(Slice)操作符获取列表里的元素. 示例: >>> L = [1,2,3,4,5] # 取前2个元素,传统方法 >>> [L[0],L[ ...
随机推荐
- BZOJ1149[CTSC2007]风玲Mobiles
Description Input Output 输出仅包含一个整数.表示最少需要多少次交换能使风铃满足Ike的条件.如果不可能满足,输出-1. Sample Input 6 2 3 -1 4 5 6 ...
- [LeetCode] Repeated Substring Pattern 重复子字符串模式
Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...
- [LeetCode] Mini Parser 迷你解析器
Given a nested list of integers represented as a string, implement a parser to deserialize it. Each ...
- [LeetCode] Super Ugly Number 超级丑陋数
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
- [LeetCode] Pascal's Triangle 杨辉三角
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- [LeetCode] Spiral Matrix 螺旋矩阵
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- [EF2]Sneak Preview: Persistence Ignorance and POCO in Entity Framework 4.0
http://blogs.msdn.com/b/adonet/archive/2009/05/11/sneak-preview-persistence-ignorance-and-poco-in-en ...
- 常用的shell脚本
[root@WEB1-live sh]# cat licai_fabu.sh #!/bin/bash pid=` ps -ef | grep java | grep '8011' | awk '{pr ...
- iOS PhotoKit框架如何获取视频文件大小
// 获取相册中的资源[group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUI ...
- Docker 学习之命令详解
1. docker version docker version 显示 Docker 版本信息. 2. docker info docker info 显示 Docker 系统信息,包括镜像和容器数. ...