Python3:逗号代码 和 字符图网格
来源:互联网 发布:电脑的网络有个感叹号 编辑:程序博客网 时间:2024/05/23 13:29
逗号代码
假定有下面这样的列表:
spam=['apples','bananas','tofu',' cats']
编写一个函数,它以一个列表值作为参数,返回一个字符串。该字符串包含所有表项,表项之间以逗号和空格分隔,并在最后一个表项之前插入 and 。例如,将前面的spam列表传递给函数,将返回'apples,bananas,tofu,and cats'。但是你的函数应该能够传递给它的任何列表。
代码如下:
import copydef conFun(nameList): n=len(nameList) newList=copy.copy(nameList) newList.insert(n-1,'and') # print(newList) a=str(newList.pop()) b=str(newList.pop()) c='' c=b+' '+a newOne='' newOne=newList[0] i=1 for j in newList: newOne=newOne+','+newList[i] i=i+1 if i==len(newList): break print(newOne+','+c)
验证代码:
================== RESTART: /Users/valen/Documents/test.py ==================>>> spam=['apple','bananas','tofu','cats']>>> conFun(spam)apple,bananas,tofu,and cats>>>
字符图网格
假定有一个列表的列表,内层列表的每个值都是包含一个字符的字符串,像这样:
grid = [ ['.', '.', '.', '.', '.','.'], ['.', '0', '0', '.', '.','.'], ['0', '0', '0', '0', '.','.'], ['0', '0', '0', '0', '0','.'], ['.', '0', '0', '0', '0','0'], ['0', '0', '0', '0', '0','.'], ['0', '0', '0', '0', '.','.'], ['.', '0', '0', '.', '.','.'], ['.', '.', '.', '.', '.','.']]
你可以认为grid[x][y]是一幅“图”在x,y坐标处的字符,该图由文本字符组成。原点(0,0)在左上角,向右x坐标增加,向下y坐标增加。
复制前面的网格值,编写代码用它打印图像。
..OO.OO...OOOOOOO..OOOOOOO...OOOOO.....OOO.......O....
提示:你需要使用循环嵌套循环,打印出grid[0][0],然后grid[1][0],然后grid[2][1],以此类推,知道grid[8][0]。这就完成第一行,所以接下来打印换行。然后程序将打印出grid[0][1],然后grid[1][1],然后grid[2][1],以此类推。程序最后将打印出grid[8][5]。
而且,如果你不希望在每次print()调用后都自动打印换行,记得向print()传递end关键字参数。
import copygrid = [ ['.', '.', '.', '.', '.','.'], ['.', '0', '0', '.', '.','.'], ['0', '0', '0', '0', '.','.'], ['0', '0', '0', '0', '0','.'], ['.', '0', '0', '0', '0','0'], ['0', '0', '0', '0', '0','.'], ['0', '0', '0', '0', '.','.'], ['.', '0', '0', '.', '.','.'], ['.', '.', '.', '.', '.','.']]c=[]c=copy.deepcopy(grid)#print(c)gridLen=len(grid)cyctime=len(grid[0])#print(cyctime) i=0j=0for j in range(cyctime): if j < cyctime : for i in range(gridLen): if i < gridLen : print(c[i][j],end=' ') i=i+1 print('\n') j=j+1输出如下:
================== RESTART: /Users/valen/Documents/test.py ==================. . 0 0 . 0 0 . . . 0 0 0 0 0 0 0 . . 0 0 0 0 0 0 0 . . . 0 0 0 0 0 . . . . . 0 0 0 . . . . . . . 0 . . . . >>>
0 0
- Python3:逗号代码 和 字符图网格
- 逗号代码,字符图片网格-用Python自动化无聊的东西-chapter4
- python程序-->字符图网格
- 4.10.2 字符图网格 程序答案
- 导航图和导航网格
- 一行代码实现EditText数字输入逗号和位数限制
- 多重网格、自适应网格和无网格
- sqlserver根据逗号截取字符
- 取逗号之间的字符
- 代码中创建网格
- 逗号运算符和逗号表达式
- 逗号运算符和逗号表达式
- 逗号运算符和逗号表达式
- 逗号运算符和逗号表达式
- 逗号表达式和逗号运算符
- 逗号和分号
- 数组和逗号表达式
- Python3 字符编码
- Android 使用ContentProvider扫描手机中的图片,仿微信显示本地图片效果
- 广度优先搜索(BFS)详解
- opecv进行简单的进行图片分割2
- ArchLinux Emacs fcitx中文输入法
- Java之多态
- Python3:逗号代码 和 字符图网格
- 【OpenCV入门教程之四】 ROI区域图像叠加&初级图像混合 全剖析
- iOS - TableView 去掉多余行数以及最后一行Cell分割线的处理
- 性能测试面试常见问题
- 【OpenCV入门教程之五】 分离颜色通道&多通道图像混合
- NoSql入门和概述
- Spring注解详解
- 初始化链表,插入删除节点,遍历链表,链表长度,找出中间节点
- EditorConfig使用和介绍