菜鸟教程 -- 学的不仅是技术,更是梦想!

Python 基础教程
(追記) (追記ここまで)

Python 练习实例40

Python 100例 Python 100例

题目:将一个数组逆序输出。

程序分析:用第一个与最后一个交换。

程序源代码:

实例

#!/usr/bin/python# -*- coding: UTF-8 -*-if__name__ == '__main__': a = [9,6,5,4,1]N = len(a)print(a)foriinrange(len(a) // 2): a[i],a[N - i - 1] = a[N - i - 1],a[i]print(a)

以上实例输出结果为:

[9, 6, 5, 4, 1]
[1, 4, 5, 6, 9]

Python 100例 Python 100例

AI 思考中...

7 篇笔记 写笔记

  1. #0

    nathan

    bin***[email protected]

    39

    以下方式也可以将一个数组逆序输出:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    if __name__ == '__main__':
     a = [9,6,5,4,1]
     print a[::-1]
    

    nathan

    bin***[email protected]

    9年前 (2017年03月07日)
  2. #0

    叮咚

    126***[email protected]

    14

    使用 reverse() 函数:

    >>> a = [9,6,5,4,1]
    >>> a.reverse()
    >>> print a
    [1, 4, 5, 6, 9]
    

    叮咚

    126***[email protected]

    9年前 (2017年04月09日)
  3. #0

    朦胧

    253***[email protected]

    2

    参考方法:

    # coding:utf-8
    List1=[2,5,4,8,3,1,9]
    print("打印原来的数组:",List1)
    print()
    List2=List1[-1::-1]
    print("打印新的数组:",List2)
    

    朦胧

    253***[email protected]

    9年前 (2017年06月13日)
  4. #0

    陆小斯

    iml***[email protected]

    1

    这样也是可以的,通过列表位置倒序打印。

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    a = ['1','2','3','4','5','6']
    c = 0
    d = []
    for i in a:
     c+= 1
     d.append(a[-c])
    print d
    

    陆小斯

    iml***[email protected]

    9年前 (2017年06月16日)
  5. #0

    Webben

    wei***[email protected]

    1

    参考实例:

    #!/usr/bin/python3
    # -*- coding: UTF-8 -*-
    i = [1,2,3,4,5,6];
    n = len(i);
    d = [];
    while( n > 0 ):
     d.append(i[n-1]);
     n -= 1;
    print(d);

    Webben

    wei***[email protected]

    9年前 (2017年08月03日)
  6. #0

    Tongfei

    315***[email protected]

    0

    参考:

    arr = [1, 2, 3, 4, 5, 6]
    arr_new = []
    for i in range(len(arr) - 1, -1, -1): 
     arr_new.append(arr[i])
    print(arr_new)

    Tongfei

    315***[email protected]

    9年前 (2017年09月05日)
  7. #0

    滑稽树上滑稽果

    374***[email protected]

    1

    Python3 测试:

    a=[0,1,2,3,4,5,6,7,8,9]
    for i in range(0,10):
     print(a.pop(),end='')

    滑稽树上滑稽果

    374***[email protected]

    9年前 (2017年11月12日)

点我分享笔记

  • 昵称 (必填)
  • 邮箱 (必填)
  • 引用地址

AltStyle によって変換されたページ (->オリジナル) /