string = "HELLOWORL"
a = [[1,0],[0, 2],[0, 4],[1,1],[0, 1],[0, 0],[1,3],[1,4],[1,2]]
def value(v): return v[1]
import itertools
s = sorted(zip(string, a), key=value)
s = ((l, r) for (l, (r, c)) in s)
s = itertools.groupby(s, key=value)
print '\n'.join(''.join(c for (c, r) in g[1]) for g in s)
a=[[1,0],[0, 2],[0, 4],[1,1],[0, 1],[0, 0],[1,3],[1,4],[1,2]]
string = "HELLOWORL"
# zip(*a) extracts the first and the last element of each elemnt of a
# in a different list
rows, cols = zip(*a)
# Find the maximum possible value and sum 1in order to use then xrange
maxrow=max(rows) + 1
maxcol=max(cols) + 1
# Create an empty list with placeholders fot he characteres
b=[["" for _ in xrange(maxcol)] for _ in xrange(maxrow)]
for i in xrange(len(string)):
letter=string[i]
row,col = a[i]
# Change the placeholder for the correct character
b[row][col]=letter
# Paste everything pith join
result = "\n".join(["".join(i) for i in b])
您可以使用这些函数来实现:
^{} 形成
s
和a
中元素的元组^{} 分隔第1行和第2行的字符
^{} 对每行的字符进行排序
输出:
也可以这样做:
这个
zip
是带有列表的字符串,然后列表元素sorted
将该列删除,并将项目groupby
放入行中,每个组中的每个字母在删除行后join
放入行中,然后将组join
放入行中。你知道吗打印内容:
我不知道第二个
E
消失在哪里了。你知道吗还有一个使用列表压缩和zip函数的选项。你知道吗
which results in result=“沃尔\nHLLOR”
相关问题 更多 >
编程相关推荐