二维数组90度旋转

#!/usr/bin/env python# -*- coding: utf-8 -*-# @Time : 2018-01-15 20:40# @Author :  hhj# @Site :

## [0, 1, 2, 3, 4]# [0, 1, 2, 3, 4]# [0, 1, 2, 3, 4]# [0, 1, 2, 3, 4]# [0, 1, 2, 3, 4]# ----------------二维数组90度旋转----# [0, 0, 0, 0, 0]# [1, 1, 1, 1, 1]# [2, 2, 2, 2, 2]# [3, 3, 3, 3, 3]# [4, 4, 4, 4, 4]data=[[col for col in range(10)] for row in range(5)]

for col in data:print(col)

# 方法一for r_index,row in enumerate(data):    for c_index in range(r_index,len(row)):        temp=data[c_index][r_index]        data[c_index][r_index] =row[c_index]        data[r_index][c_index]=temp

print("--------------------%s" %r_index)    for r in data:print(r)# 方法二n=len(data)for i in range(len(data)):    a=[data[i][i] for row in range(n)]    print(a)    # [0, 1, 2, 3, 4]    # [0, 1, 2, 3, 4]    # [0, 1, 2, 3, 4]    # [0, 1, 2, 3, 4]    # [0, 1, 2, 3, 4]    # ----------------二维数组90度旋转----    # [0, 0, 0, 0, 0]    # [1, 1, 1, 1, 1]    # [2, 2, 2, 2, 2]    # [3, 3, 3, 3, 3]    # [4, 4, 4, 4, 4]

G

M

T

Detect language
Afrikaans
Albanian
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bengali
Bosnian
Bulgarian
Catalan
Cebuano
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Filipino
Finnish
French
Galician
Georgian
German
Greek
Gujarati
Haitian Creole
Hausa
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Khmer
Korean
Lao
Latin
Latvian
Lithuanian
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mongolian
Myanmar (Burmese)
Nepali
Norwegian
Persian
Polish
Portuguese
Punjabi
Romanian
Russian
Serbian
Sesotho
Sinhala
Slovak
Slovenian
Somali
Spanish
Sundanese
Swahili
Swedish
Tajik
Tamil
Telugu
Thai
Turkish
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Yiddish
Yoruba
Zulu
  Afrikaans
Albanian
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bengali
Bosnian
Bulgarian
Catalan
Cebuano
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Filipino
Finnish
French
Galician
Georgian
German
Greek
Gujarati
Haitian Creole
Hausa
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Khmer
Korean
Lao
Latin
Latvian
Lithuanian
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mongolian
Myanmar (Burmese)
Nepali
Norwegian
Persian
Polish
Portuguese
Punjabi
Romanian
Russian
Serbian
Sesotho
Sinhala
Slovak
Slovenian
Somali
Spanish
Sundanese
Swahili
Swedish
Tajik
Tamil
Telugu
Thai
Turkish
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Yiddish
Yoruba
Zulu
         

Text-to-speech function is limited to 200 characters

  Options : History : Feedback : Donate Close

原文地址:https://www.cnblogs.com/hhjwqh/p/8401774.html

时间: 2024-10-06 00:53:25

二维数组90度旋转的相关文章

算法基础之2维数组90度旋转

data=[[col for col in rang(4)] for row in rang(4)] for row in data: print(row) print("-------------------------------------------") for r_index, row in enumerate(data): for c_index in range (r_index ,len(row)): temp=data[c_index][r_index] date[c

python 二维数组90°旋转

题目: 使用python生成一个4×4二维数组并将其顺时针旋转90° 源码如下: import random datarow=[] data=[] for i in range(4):     for j in range(4):         datarow.append(random.randint(10,80))     data.append(datarow)     datarow = [] print("data") for k in data:     print(k)

Python学习笔记 之 递归、二维数组顺时针旋转90°、正则表达式

递归.二维数组顺时针旋转90°.正则表达式 1.   递归算法是一种直接或间接调用自身算法的过程. 特点: 递归就是在过程或函数里调用自身 明确的递归结束条件,即递归出口 简洁,但是不提倡 递归次数多容易造成栈溢出 要求: 每次调用递归规模上有所减小 前一次为后一次做准备 规模较小时必须直接给出解答而不再进行递归调用 例子:递归实现二分法 1 def searchMyData(mydate,a1): 2 mid = int(len(mydate)/2) 3 if mid >= 1: 4 if m

day4 二维数组旋转90度

二维数组的旋转其实就是数组里面的元素对调的情况:下面有一个4×4的二维数组,[[0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3]],现在要求把二维数组转化为下列形式,[[0, 0, 0, 0], [1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]].下面来看一下流程图: 流程图: 流程图如上面所示,就是进行简单的互换而已,下面我们来用代码实现行列互换: data = [[i for i in range(4)

leetcode——Rotate Image(二维数组顺时针旋转90度)

题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up: Could you do this in-place? 分析: 二维数组a[n][n]顺时针旋转90度,要解决这个问题,无疑,第一件事儿就是找规律. 当n=1时,不用动了. 当n=2时, 旋转之后变为 有: a[0][0] = a[1][0] a[1][0] = a[1

python-二维数组实现90度旋转

本篇主要介绍了对一个N*N的数组,如果进行90度的旋转 首先,定义一个一维数组很简单,如下: a = [i for i in range(10)] print(a) -----结果----- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 此写法与下面的写法是相同的效果: a = [] for i in range(10): a.append(i) print(a) -----结果----- [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 那么二维数组应该如何进行创建呢

练习-二维数组旋转

#slist = [[i for i in range(4)] for l in range(8)] # slist = [ # [4, 5, 2, 7], # [8, 7, 4, 2], # [1, 9, 7, 3], # [0, 4, 5, 6], # ] # 4*4 slist = [ [4, 5, 2, 7], [8, 7, 4, 2], [1, 9, 7, 3], [0, 4, 5, 6], [1, 9, 7, 3], ] # 4*5 # slist = [ # [1, 0, 1, 8

算法之二维数组旋转

现在有个二维数组:data=[0,1,2,3],[0,1,2,3],[0,1,2,3],[0,1,2,3],每一行都是[0,1,2,3],要求顺时针旋转90度,变成每一列都是[0,1,2,3]. #首先生成一维数组的语句是: row=[i for i in range(6)] print(row) #运行结果: #[0, 1, 2, 3, 4, 5] #生成二维数组的语句是: data=[[i for i in range(4)] for k in range(4)] for j in data

【剑指Offer面试题】九度OJ1384:二维数组中的查找

下决心AC全部剑指offer面试题. 九度OJ面试题地址:http://ac.jobdu.com/hhtproblems.php 书籍:何海涛--<剑指Offer:名企面试官精讲典型编程题> 对于面试题,面试官往往更希望我们能提出优化方法,这样更能体现我们的思维能力以及传说中的"内功".所以做剑指offer要着重训练这方面,多总结多细究,总是有优点的.加油~ 题目链接地址: http://ac.jobdu.com/problem.php?pid=1384 二维数组中的查找