Python: two dimensional array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | # 假设puzzle是一个包含多个字符串的列表,每个字符串都是同一长度的,代表字母网格的一行 puzzle = [ "JGJGDDAOYD" , "IDGFHSPOSA" , "FGDIOSAFSC" , "INTERNETSO" , "FJKCOSAFSM" , "DJSGAPAHDP" , "HAUSTRFBFU" , "KDGFUCNSKT" , "WSJDYCFXDE" , "ODVFKXJVCR" ] # 获取网格的宽和高(即每一行的长度和行数) grid_width = len (puzzle[ 0 ]) grid_height = len (puzzle) # 定义要搜索的单词 word = input ( "Enter a word to search for: " ) # 搜索函数 def solve(puzzle, word): found = False for i in range (grid_height - len (word) + 1 ): # 确保从第一行到能放下整个单词的行 for j in range (grid_width - len (word) + 1 ): # 确保从第一列到能放下整个单词的列 # 检查在i, j位置开始的子网格是否包含目标单词 for k in range ( len (word)): if puzzle[i + k][j + k] ! = word[k]: break # 如果字符不匹配,则跳出内层循环 else : # 只有当内层循环正常结束时(即没有break),才说明找到了单词 found = True print (f "Word '{word}' found at ({i}, {j})" ) break # 找到后,可以退出外层循环,因为不需要继续搜索 return found # 调用搜索函数 found = solve(puzzle, word) if not found: print (f "Word '{word}' not found in the puzzle." ) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | def find_diagonal_word(letter_array, word): """ 查找字列表中的字符串 :param letter_array: 字符串列表 :param word: 所需要查找字符 :return: """ rows = len (letter_array) cols = len (letter_array[ 0 ]) if rows > 0 else 0 # Iterate over all possible starting positions for the diagonal for i in range (rows): for j in range (cols): # Check if the current position is within the bounds of the array # and if the word can fit in the diagonal if i + len (word) < = rows and j + len (word) < = cols: # Check if the word matches the diagonal match = True for k in range ( len (word)): if letter_array[i + k][j + k] ! = word[k]: match = False break if match: return True return False def find_column_word(letter_array, word): """ :param letter_array: :param word: :return: """ cols = len (letter_array[ 0 ]) if letter_array else 0 # Iterate over all columns for j in range (cols): # Check if the word matches the current column match = True for i in range ( len (letter_array)): # Check if the current row is within the bounds of the array # and if the character matches the word if i < len (letter_array) and letter_array[i][j] ! = word[i]: match = False break # If the word matches the column, return True if match and i = = len (word) - 1 : return True return False def suc(score): s6grade = '' if score > = 80 : s6grade = 'A' if score > = 65 and score < = 79 : s6grade = 'B' if score > = 50 and score < 65 : s6grade = 'C' if score < 50 : s6grade = 'F' return s6grade |
调用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | if __name__ = = '__main__' : letter_array = [ "JGJGDDAOYD" , "IDGFHSPOSA" , "FGDIOSAFSC" , "INTERNETSO" , "FJKCOSAFSM" , "DJSGAPAHDP" , "HAUSTRFBFU" , "KDGFUCNSKT" , "WSJDYCFXDE" , "ODVFKXJVCR" ] word = "coMPUTER" word = input ( "please word:" ) # Convert the word to uppercase since the letter array seems to be in uppercase word = word.upper() if find_column_word(letter_array, word): print (f "The word '{word}' exists in the letter array as a column." ) else : print (f "The word '{word}' does not exist in the letter array as a column." ) letter_array = [ "JGJGDDAOYD" , "IDGFHSPOSA" , "FGDIOSAFSC" , "INT ERNETSO" , "FJKCOSAFSM" , "DJSGAPAHDP9" , "HAUSTRFBFU" , "KDGFUCNSKT" , "WSJDYCFXDE" , "ODVFKXJVCR" ] word = "CAR" word = input ( "please word:" ) if find_diagonal_word(letter_array, word): print (f "The word '{word}' exists in the letter array as a diagonal." ) else : print (f "The word '{word}' does not exist in the letter array as a diagonal." ) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | score = [ [ 58 , 80 , 74 , 90 , 45 , 82 ], [ 71 , 70 , 64 , 85 , 50 , 86 ], [ 87 , 63 , 65 , 84 , 62 , 83 ], [ 91 , 66 , 67 , 92 , 65 , 90 ], [ 83 , 74 , 81 , 82 , 57 , 82 ]] k = 0 while k< 5 : subavg = 0 a = 0 while a< 6 : subavg = subavg + score[k][a] a = a + 1 subavg = subavg / 6 print (subavg) k = k + 1 print ( "***************8" ) rows, cols = ( 5 , 6 ) s6grade = [[ '0' ] * cols] * rows # 赋值为零 #[[]] # '''''' for i in range ( 0 , 5 ): for j in range ( 0 , 6 ): s6grade[i][j] = suc(score[i][j]) print (s6grade[i][j], end = " " ) print () print ( '**********' ) for i in range ( 0 , 5 ): col = [] for j in range ( 0 , 6 ): col.append(suc(score[i][j])) #s6grade[i][j] =suc(score[i][j]) # 出问题 s6grade.insert(i,col) #print(s6grade) #print(suc(score[0][4])) # 0,4 45 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | score = [ [ 58 , 80 , 74 , 90 , 45 , 82 ], [ 71 , 70 , 64 , 85 , 50 , 86 ], [ 87 , 63 , 65 , 84 , 62 , 83 ], [ 91 , 66 , 67 , 92 , 65 , 90 ], [ 83 , 74 , 81 , 82 , 57 , 82 ]] k = 0 while k< 5 : subavg = 0 a = 0 while a< 6 : subavg = subavg + score[k][a] a = a + 1 subavg = subavg / 6 print (subavg) k = k + 1 print ( "***************8" ) rows, cols = ( 5 , 6 ) s6grade = [[]] #[['0'] * cols] * rows # 赋值为零 ''' for i in range(0,5): for j in range(0,6): s6grade[i][j]='1' print(s6grade[i][j], end=" ") print() ''' for ii in range ( 0 , 5 ): col = [] for ji in range ( 0 , 6 ): d = suc(score[ii][ji]) col.append(d) #print(d) #s6grade[ii][ji] = d print (col) s6grade.insert(ii,col) print (s6grade) #print(suc(score[0][4])) # 0,4 45 for i in range ( 0 , 5 ): for j in range ( 0 , 6 ): print (s6grade[i][j],end = " " ) print () |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | while True : char = [ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' , 'j' , 'k' , 'l' , 'm' , 'n' , 'o' , 'p' , 'q' , 'r' , 's' , 't' , 'u' , 'v' , 'w' , 'x' , 'y' , 'z' ] morsecode = [ '.-' , '-...' , '-.-.' , '-..' , '.' , '..-.' , '--.' , '....' , '..' , '.---' , '-.-' , '.-..' , '--' , '-.' , '---' , '.--.' , '--.-' , '.-.' , '...' , '-' , '..-' , '...-' , '.--' , '-..-' , '-.--' , '--..' ] message = input ( 'please word' ) l = len (message) code = "" mos = "" for k in range ( 0 ,l): for index in range ( 0 , len (char)): if (message[k] = = char[index]): code = code + " " + str (index + 1 ) mos = mos + " " + morsecode[index] print (code) print (mos) yn = input ( 'y/N' ) if yn = = 'Y' or yn = = 'y' : continue else : break |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | def find_column_word(letter_array, word): cols = len (letter_array[ 0 ]) if letter_array else 0 # Iterate over all columns for j in range (cols): # Check if the word matches the current column match = True for i in range ( len (letter_array)): # Check if the current row is within the bounds of the array # and if the character matches the word if i < len (letter_array) and letter_array[i][j] ! = word[i]: match = False break # If the word matches the column, return True if match and i = = len (word) - 1 : return True return False # Example usage letter_array = [ "JGJGDDAOYD" , "IDGFHSPOSA" , "FGDIOSAFSC" , "INTERNETSO" , "FJKCOSAFSM" , "DJSGAPAHDP" , "HAUSTRFBFU" , "KDGFUCNSKT" , "WSJDYCFXDE" , "ODVFKXJVCR" ] word = "coMPUTER" # Convert the word to uppercase since the letter array seems to be in uppercase word = word.upper() if find_column_word(letter_array, word): print (f "The word '{word}' exists in the letter array as a column." ) else : print (f "The word '{word}' does not exist in the letter array as a column." ) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | def getMoseCode(): """ :return: """ char = [ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' , 'j' , 'k' , 'l' , 'm' , 'n' , 'o' , 'p' , 'q' , 'r' , 's' , 't' , 'u' , 'v' , 'w' , 'x' , 'y' , 'z' ] morsecode = [ '.-' , '-...' , '-.-.' , '-..' , '.' , '..-.' , '--.' , '....' , '..' , '.---' , '-.-' , '.-..' , '--' , '-.' , '---' , '.--.' , '--.-' , '.-.' , '...' , '-' , '..-' , '...-' , '.--' , '-..-' , '-.--' , '--..' ] message = input ( 'please word' ) l = len (message) code = "" mos = "" for k in range ( 0 , l): for index in range ( 0 , len (char)): if (message[k] = = char[index]): code = code + " " + str (index + 1 ) mos = mos + " " + morsecode[index] break # 加这行效率更快Geovin Du print (code) print (mos) def getMoseCodeWhile(): """ 这个快1秒 :return: """ char = [ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' , 'j' , 'k' , 'l' , 'm' , 'n' , 'o' , 'p' , 'q' , 'r' , 's' , 't' , 'u' , 'v' , 'w' , 'x' , 'y' , 'z' ] morsecode = [ '.-' , '-...' , '-.-.' , '-..' , '.' , '..-.' , '--.' , '....' , '..' , '.---' , '-.-' , '.-..' , '--' , '-.' , '---' , '.--.' , '--.-' , '.-.' , '...' , '-' , '..-' , '...-' , '.--' , '-..-' , '-.--' , '--..' ] message = input ( 'please word' ) l = len (message) code = "" mos = "" j = 0 while j<l: find = False index = 0 while index< len (char) and find = = False : # 加这行效率更快 if (message[j] = = char[index]): code = code + " " + str (index + 1 ) mos = mos + " " + morsecode[index] find = True index = index + 1 j = j + 1 print (code) print (mos) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | # Press the green button in the gutter to run the script. if __name__ = = '__main__' : #print_hi('PyCharm,python language ,Geovin Du') while True : time_start = time.time() # 记录开始时间 #getMoseCode() getMoseCodeWhile() time_end = time.time() # 记录结束时间 time_sum = time_end - time_start # 计算的时间差为程序的执行时间,单位为秒/s print (time_sum) yn = input ( 'y/N' ) if yn = = 'Y' or yn = = 'y' : continue else : break |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | def find_column_wordPart(letter_array, word): """ 在给定的字符串数组列表中按列查找指定的单词的部分位置列中。 :param letter_array: 字符串数组列表 :param word: 所需要查找的单词 :return: 如果找到单词的部分则返回True,否则返回False """ if not letter_array or not word: return False cols = len (letter_array[ 0 ]) rows = len (letter_array) word_length = len (word) # 遍历所有列 for j in range (cols): # 在每一列中,从顶部到底部滑动单词长度的窗口 for start_row in range (rows - word_length + 1 ): # 检查当前窗口的字符串是否与单词匹配 match = True for i in range (word_length): if letter_array[start_row + i][j] ! = word[i]: match = False break if match: return True # 如果在某一列的窗口中找到单词,则返回True return False # 如果检查完所有列都没有找到匹配的单词,则返回False def find_column_word(letter_array, word): """ 在给定的字符串数组列表中按一列查找指定的单词。 :param letter_array: 字符串数组列表 :param word: 所需要查找的单词 :return: 如果找到单词则返回True,否则返回False """ if not letter_array or not word: return False cols = len (letter_array[ 0 ]) rows = len (letter_array) word_length = len (word) # 如果单词长度大于列数,则无法找到该单词 if word_length > cols: return False # 遍历所有列 for j in range (cols): # 检查单词是否与当前列匹配 match = True for i in range (word_length): # 检查当前列的字符是否与单词的对应字符匹配 if letter_array[i][j] ! = word[i]: match = False break if match: return True # 如果单词与某一列完全匹配,则返回True return False # 如果检查完所有列都没有找到匹配的单词,则返回False # Press the green button in the gutter to run the script. C:\Users\geovindu\PycharmProjects\EssentialAlgorithms\ if __name__ = = '__main__' : print_hi( 'PyCharm,python,geovindu,Geovin Du,涂聚文,你好!' ) letter_array = [ "JGJGDDAOYD" , "IDGFHSPOSA" , "FGDIOSAFSC" , "INTERNETSO" , "FJKCOSAFSM" , "DJSGAPAHDP" , "HAUSTRFBFU" , "KDGFUCNSKT" , "WSJDYCFXDE" , "ODVFKXJVCR" ] word = "dacoMPUTER" #整个一列中 # 转换为大写以便与letter_array中的字符进行比较 word = word.upper() if find_column_word(letter_array, word): print (f "The word '{word}' exists in the letter array as a column." ) else : print (f "The word '{word}' does not exist in the letter array as a column." ) word = "MPUTER" # 在一列部分的位置 # 转换为大写以便与letter_array中的字符进行比较 word = word.upper() if find_column_wordPart(letter_array, word): print (f "The word Part '{word}' exists in the letter array as a column." ) else : print (f "The word Part '{word}' does not exist in the letter array as a column." ) |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2023-06-09 cpp: Visitor Pattern
2011-06-09 CSS Vertical Text(三种方式)