Python根据现有列表的字母索引创建列表

发布于 2025-01-22 05:25:30 字数 543 浏览 3 评论 0原文

因此,我正在尝试使用一个脚本,该脚本使我可以使用文本文件, 旋转布局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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天赋异禀 2025-01-29 05:25:30

最短的方法使用列表(zip(*x))

x = ['the','boy','ate','jam','pie']
rotate_list = [''.join(i) for i in list(zip(*x))]
print(rotate_list)

output:

['tbajp', 'hotai', 'eyeme']

编辑:

您可以通过:

reversed_strings = [''.join(i)[::-1] for i in list(zip(*x))]

output:outpty:

['pjabt', 'iatoh', 'emeye']

Shortest method using list(zip(*x)):

x = ['the','boy','ate','jam','pie']
rotate_list = [''.join(i) for i in list(zip(*x))]
print(rotate_list)

Output:

['tbajp', 'hotai', 'eyeme']

Edit:

You can reverse the rotate_list by using:

reversed_strings = [''.join(i)[::-1] for i in list(zip(*x))]

Output:

['pjabt', 'iatoh', 'emeye']
晨曦÷微暖 2025-01-29 05:25:30

只需使用zip函数:

x = ['the','boy','ate','jam','pie']
list(zip(*x))
[('t', 'b', 'a', 'j', 'p'), ('h', 'o', 't', 'a', 'i'), ('e', 'y', 'e', 'm', 'e')]

注意:如果字符串并非全部相等,则可能需要使用itertools.zip_longest而不是

Just use the zip function:

x = ['the','boy','ate','jam','pie']
list(zip(*x))
[('t', 'b', 'a', 'j', 'p'), ('h', 'o', 't', 'a', 'i'), ('e', 'y', 'e', 'm', 'e')]

Note: if the strings are not all of equal length you may want to use itertools.zip_longest instead

铃予 2025-01-29 05:25:30
def rotate(input_list: list) -> list:
    print(f"Input List is: {input_list}")
    output_list = []
    for i in range(0, len(input_list)):
        output_string = ""
        for string in input_list:
            if i < len(string):
                output_string += string[i]

        if output_string:
            output_list.append(output_string)

    print(f"Output List is: {output_list}")
    return output_list


rotate(['the', 'boy', 'ate', 'jam', 'pie'])

如您在上面的代码中看到的那样,

  • 我们正在通过input_list和从每个字符串中的索引i进行迭代。
  • 由于所有字符串的长度可能并不相同,因此我们需要确保我们不会尝试获取字符串中不存在的索引。
  • 最后,我们检查是否生成了output_string,并将其附加到输出列表。

输出:

#python3 test5.py
Input List is: ['the', 'boy', 'ate', 'jam', 'pie']
Output List is: ['tbajp', 'hotai', 'eyeme']
def rotate(input_list: list) -> list:
    print(f"Input List is: {input_list}")
    output_list = []
    for i in range(0, len(input_list)):
        output_string = ""
        for string in input_list:
            if i < len(string):
                output_string += string[i]

        if output_string:
            output_list.append(output_string)

    print(f"Output List is: {output_list}")
    return output_list


rotate(['the', 'boy', 'ate', 'jam', 'pie'])

As you can see in the above code,

  • we are iterating over the input_list and fetch character at index i from each string.
  • As all strings may not be of the same length we need to make sure we are not trying to fetch the index which is not present within the string.
  • At the end, we check if the output_string is generated, and we append it to the output list.

Output:

#python3 test5.py
Input List is: ['the', 'boy', 'ate', 'jam', 'pie']
Output List is: ['tbajp', 'hotai', 'eyeme']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文