Python根据现有列表的字母索引创建列表
因此,我正在尝试使用一个脚本,该脚本使我可以使用文本文件, 旋转布局90度,并将所有字母保持在正确的方向(而不是让字母向侧面转动),
因此,如果我有这样的列表:
x = ['the','boy','ate','jam','pie']
我如何根据字母内部的索引位置创建一个列表每个列表项目。
letters[0] = t,b,a,j,p
letters[1] = h,o,t,a,i
letters[2] = e,y,e,m,e
x = ['tbajp','hotai','eyeme']
我在哪里:
new_list = []
x = ["the","boy","ate","jam","pie"]
y = len(x)
t = 0
while y > 0:
z = len(x[0])
c = 0
while z > 0:
print(x[t][c])
z -= 1
c += 1
y -= 1
t += 1
So I am trying to work on a script which allows me to take a text file,
rotate the layout 90 degrees and keep all the letters in the correct orientation (as opposed to having the letters turn sideways)
Right so If I have a list like so:
x = ['the','boy','ate','jam','pie']
How can I create a list based off of the index position of the letters within each list item.
letters[0] = t,b,a,j,p
letters[1] = h,o,t,a,i
letters[2] = e,y,e,m,e
x = ['tbajp','hotai','eyeme']
Where I am at:
new_list = []
x = ["the","boy","ate","jam","pie"]
y = len(x)
t = 0
while y > 0:
z = len(x[0])
c = 0
while z > 0:
print(x[t][c])
z -= 1
c += 1
y -= 1
t += 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最短的方法使用
列表(zip(*x))
:output:
编辑:
您可以通过:
output:outpty:
Shortest method using
list(zip(*x))
:Output:
Edit:
You can reverse the
rotate_list
by using:Output:
只需使用
zip
函数:注意:如果字符串并非全部相等,则可能需要使用
itertools.zip_longest
而不是Just use the
zip
function:Note: if the strings are not all of equal length you may want to use
itertools.zip_longest
instead如您在上面的代码中看到的那样,
input_list
和从每个字符串中的索引i进行迭代。output_string
,并将其附加到输出列表。输出:
As you can see in the above code,
input_list
and fetch character at index i from each string.output_string
is generated, and we append it to the output list.Output: