Python reverse order
来源:互联网 发布:公司取名软件注册码 编辑:程序博客网 时间:2024/06/08 04:07
Python可以很简单实现C语言中for(i = len-1; i>=0; –i)的逆序循环,而且有不止一种写法:
第一种,for i in range(len-1, -1, -1)
,简单易懂
第二种,for i in range(len)[::-1]
,利用list的[start:end:step]排序功能,当然在大数组下性能堪忧
What is Python's range() Function?
As an experienced Python developer, or even a beginner, you've likely heard of the Python range()
function. But what does it do? In a nutshell, it generates a list of numbers, which is generally used to iterate over with for
loops. There's many use cases. Often you will want to use this when you want to perform an action X number of times, where you may or may not care about the index. Other times you may want to iterate over a list (or another iterable object), while being able to have the index available.
The range()
function works a little bit differently between Python 2.x and 3.x under the hood, however the concept is the same. We'll get to that a bit later, however.
Python's range() Parameters
The range()
function has two sets of parameters, as follows:
range(stop)
stop
: Number of integers (whole numbers) to generate, starting from zero. eg.range(3) == [0, 1, 2]
.
range([start], stop[, step])
start
: Starting number of the sequence.stop
: Generate numbers up to, but not including this number.step
: Difference between each number in the sequence.
Note that:
- All parameters must be integers.
- All parameters can be positive or negative.
range()
(and Python in general) is 0-index based, meaning list indexes start at 0, not 1. eg. The syntax to access the first element of a list ismylist[0]
. Therefore the last integer generated byrange()
is up to, but not including,stop
. For examplerange(0, 5)
generates integers from 0 up to, but not including, 5.
Python's range() Function Examples
Simple Usage
Iterating Lists
99 Bottles of Beer on the Wall...
With the following code:
We get the following output:
Brilliant! Finally you can see the true power of Python :). If you're a little confused, for reference see the Wikipedia article.
Python's range() vs xrange() Functions
You may have heard of a function known as xrange()
. This is a function that is present in Python 2.x, however it was renamed to range()
in Python 3.x, and the original range()
function was deprecated in Python 3.x. So what's the difference? Well, in Python 2.x range()
produced a list, and xrange()
returned an iterator - a sequence object. We can see this in the following example:
So in Python 3.x, the range()
function got its own type
. In basic terms, if you want to use range()
in a for
loop, then you're good to go. However you can't use it purely as a list
object. For example you cannot slice a range
type.
When you're using an iterator, every loop of the for
statement produces the next number on the fly. Whereas the original range()
function produced all numbers instantaneously, before the for
loop started executing. The problem with the original range()
function was that it used a very large amount of memory when producing a lot of numbers. However it tends to be quicker with a small amount of numbers. Note that in Python 3.x, you can still produce a list by passing the generator returned to the list()
function. As follows:
To see the speed difference between the original range()
and xrange()
function, you may want to check out this article.
Using floats with Python's range() function
Unfortunately the range()
function doesn't support the float
type. However don't get upset too soon! We can easily implement it with a function. There's several ways it can be done, however here's one.
- Python reverse order
- Python Traverse list reverse order
- String reverse order
- orise 5069(reverse order) parameters
- process a container in a reverse order
- Output a string in reverse order
- [转]python sort reverse
- leetcode-Reverse Integer-python
- Reverse Integer leetcode python
- Python list reverse
- leetcode(1),reverse(python)
- python reverse()方法
- Python string reverse
- reverse string in python
- Reverse Integer (python)
- Python List reverse()方法
- 7. Reverse Integer-python
- 【Leetcode】【python】Reverse Integer
- 二、初学SpringMVC+Mybatis之Spring IOC
- RDD弹性分布式数据集(一)
- The effect of opinion clustering on disease outbreaeks
- Docker隔绝上传文件
- python中的函数式编程——apply, filter, map, reduce
- Python reverse order
- MapReduce处理多个不同的出入文件
- java毕向东听课笔记2(函数的重载)
- 面向对象在javascript中的实践之内存分析
- 查看mysql安装版本
- WPF学习——依赖项属性(3)
- leetcode 210. Course Schedule II
- 关键字
- js高级程序设计笔记1