Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 生成菱形_Python_Python 2.7 - Fatal编程技术网

Python 生成菱形

Python 生成菱形,python,python-2.7,Python,Python 2.7,我正在写一个程序,它得到一个整数并生成一个菱形。输入是钻石的边长,例如整数是5,结果应该是: ----*---- ---***--- --*-*-*-- -*--*--*- ********* -*--*--*- --*-*-*-- ---***--- ----*---- 我现在的代码: side = int(raw_input()) x = (side * 2) - 1 middle = x // 2 a = "" for i in range(side - 1): t = ["-"

我正在写一个程序,它得到一个整数并生成一个菱形。输入是钻石的边长,例如整数是5,结果应该是:

----*----
---***---
--*-*-*--
-*--*--*-
*********
-*--*--*-
--*-*-*--
---***---
----*----
我现在的代码:

side = int(raw_input())
x = (side * 2) - 1
middle = x // 2
a = ""
for i in range(side - 1):
    t = ["-"] * x
    t[middle - i] = "*"
    t[middle + i] = "*"
    t[middle] = "*"
    a += "".join(t)
    a += "\n"
t=a[:-1]
a += "*" * x + "\n"
a += t[::-1]
print(a)
使用Python2.7生成此菱形的最快方法是什么

def Diamond(size):
    for n in (lambda x : x[:-1]+x[::-1])([size-i-1 for i in range(size)]):
        row = ["-" if i != n else "*" for i in range(size)]
        print(["".join(row[:-1] +["*"]+ row[size-2::-1]) if n != 0 else "*"*(size*2-1)][0])

这是家庭作业吗?你能给我们看一下你的代码并问一些具体的问题吗?我会马上添加代码。这是一场编程比赛中的挑战site@GiacomoDegliEsposti我添加了代码谁在乎最快的方法是什么。这是一项完全不现实、毫无意义的任务。我投票将这个问题作为离题题来结束,因为它无关紧要,不太可能帮助未来的读者。