Hướng dẫn python list vertical to horizontal - danh sách python dọc sang ngang

Có vẻ như bạn đang tạo một chuỗi từ một mảng? Hoặc có thể là một chuỗi từ một chuỗi?

>>> from_list = ['h', 'e', 'l', 'l', 'o']
>>> print ''.join[from_list]
hello
>>> print ':'.join[from_list]
h:e:l:l:o

Hàm tham gia [] của các chuỗi lấy một danh sách và trả về một chuỗi với đối số của nó bị kẹt giữa mỗi mục.

Bạn có thể đang sử dụng một chuỗi:

>>> from_string = "h\ne\nl\nl\no"
>>> print from_string
h
e
l
l
o
>>> print from_string.split[]
['h', 'e', 'l', 'l', 'o']
>>> print "".join[from_string.split[]]
hello
>>> print " [pause] ".join["one, two, three, five, no four!".split[","]]
one [pause]  two [pause]  three [pause]  five [pause]  no four!

Điều này sử dụng hàm chia [] của các chuỗi, chia một chuỗi thành một danh sách các mục. Kết quả sau đó được nối trở lại thành một chuỗi. Bạn có thể phân chia trên bất kỳ chuỗi nào, nhưng khoảng trắng là mặc định.

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Cải thiện bài viết

    Lưu bài viết

    Đọc

    Bàn luận : test_str = ‘geeksforgeeks’ 

    Cho một chuỗi, chuyển đổi sang dọc nếu ngang và ngược lại. : g e e k s 

    Đầu vào: test_str = geeksforgeeks, & nbsp; : Horizontal String converted to Vertical. 

    Đầu ra: g e e k s & nbsp; : test_str = g e e k s 

    Giải thích: Chuỗi ngang được chuyển đổi sang dọc. & NBSP; : ‘geeks’ 

    Đầu vào: test_str = g e e k s & nbsp; : Vertical String converted to Horizontal.

    Đầu ra: Geeks, & nbsp;

    Giải thích: Chuỗi dọc được chuyển đổi sang ngang.

    Python3

    Phương pháp số 1: [Ti ngang đến dọc] bằng cách sử dụng vòng lặp +

    Trong đó, chúng tôi thêm ký tự mới sau mỗi ký tự để mỗi phần tử được hiển thị ở dòng tiếp theo. & NBSP;

    test_str = 'geeksforgeeks'

    print____1010____11

    >>> from_string = "h\ne\nl\nl\no"
    >>> print from_string
    h
    e
    l
    l
    o
    >>> print from_string.split[]
    ['h', 'e', 'l', 'l', 'o']
    >>> print "".join[from_string.split[]]
    hello
    >>> print " [pause] ".join["one, two, three, five, no four!".split[","]]
    one [pause]  two [pause]  three [pause]  five [pause]  no four!
    
    2
    >>> from_string = "h\ne\nl\nl\no"
    >>> print from_string
    h
    e
    l
    l
    o
    >>> print from_string.split[]
    ['h', 'e', 'l', 'l', 'o']
    >>> print "".join[from_string.split[]]
    hello
    >>> print " [pause] ".join["one, two, three, five, no four!".split[","]]
    one [pause]  two [pause]  three [pause]  five [pause]  no four!
    
    3
    >>> from_string = "h\ne\nl\nl\no"
    >>> print from_string
    h
    e
    l
    l
    o
    >>> print from_string.split[]
    ['h', 'e', 'l', 'l', 'o']
    >>> print "".join[from_string.split[]]
    hello
    >>> print " [pause] ".join["one, two, three, five, no four!".split[","]]
    one [pause]  two [pause]  three [pause]  five [pause]  no four!
    
    4

    >>> from_string = "h\ne\nl\nl\no"
    >>> print from_string
    h
    e
    l
    l
    o
    >>> print from_string.split[]
    ['h', 'e', 'l', 'l', 'o']
    >>> print "".join[from_string.split[]]
    hello
    >>> print " [pause] ".join["one, two, three, five, no four!".split[","]]
    one [pause]  two [pause]  three [pause]  five [pause]  no four!
    
    5=
    >>> from_string = "h\ne\nl\nl\no"
    >>> print from_string
    h
    e
    l
    l
    o
    >>> print from_string.split[]
    ['h', 'e', 'l', 'l', 'o']
    >>> print "".join[from_string.split[]]
    hello
    >>> print " [pause] ".join["one, two, three, five, no four!".split[","]]
    one [pause]  two [pause]  three [pause]  five [pause]  no four!
    
    7

    >>> from_string = "h\ne\nl\nl\no"
    >>> print from_string
    h
    e
    l
    l
    o
    >>> print from_string.split[]
    ['h', 'e', 'l', 'l', 'o']
    >>> print "".join[from_string.split[]]
    hello
    >>> print " [pause] ".join["one, two, three, five, no four!".split[","]]
    one [pause]  two [pause]  three [pause]  five [pause]  no four!
    
    8
    >>> from_string = "h\ne\nl\nl\no"
    >>> print from_string
    h
    e
    l
    l
    o
    >>> print from_string.split[]
    ['h', 'e', 'l', 'l', 'o']
    >>> print "".join[from_string.split[]]
    hello
    >>> print " [pause] ".join["one, two, three, five, no four!".split[","]]
    one [pause]  two [pause]  three [pause]  five [pause]  no four!
    
    9
    The original string is : geeksforgeeks
    The converted string : g
    e
    e
    k
    s
    f
    o
    r
    g
    e
    e
    k
    s
    0
    The original string is : geeksforgeeks
    The converted string : g
    e
    e
    k
    s
    f
    o
    r
    g
    e
    e
    k
    s
    1

    The original string is : geeksforgeeks
    The converted string : g
    e
    e
    k
    s
    f
    o
    r
    g
    e
    e
    k
    s
    2
    The original string is : geeksforgeeks
    The converted string : g
    e
    e
    k
    s
    f
    o
    r
    g
    e
    e
    k
    s
    3
    >>> from_string = "h\ne\nl\nl\no"
    >>> print from_string
    h
    e
    l
    l
    o
    >>> print from_string.split[]
    ['h', 'e', 'l', 'l', 'o']
    >>> print "".join[from_string.split[]]
    hello
    >>> print " [pause] ".join["one, two, three, five, no four!".split[","]]
    one [pause]  two [pause]  three [pause]  five [pause]  no four!
    
    2=
    >>> from_string = "h\ne\nl\nl\no"
    >>> print from_string
    h
    e
    l
    l
    o
    >>> print from_string.split[]
    ['h', 'e', 'l', 'l', 'o']
    >>> print "".join[from_string.split[]]
    hello
    >>> print " [pause] ".join["one, two, three, five, no four!".split[","]]
    one [pause]  two [pause]  three [pause]  five [pause]  no four!
    
    9__12
    The original string is : geeksforgeeks
    The converted string : g
    e
    e
    k
    s
    f
    o
    r
    g
    e
    e
    k
    s
    8

    The original string is : geeksforgeeks
    The converted string : g
    e
    e
    k
    s
    f
    o
    r
    g
    e
    e
    k
    s

    print____1010____31

    >>> from_string = "h\ne\nl\nl\no"
    >>> print from_string
    h
    e
    l
    l
    o
    >>> print from_string.split[]
    ['h', 'e', 'l', 'l', 'o']
    >>> print "".join[from_string.split[]]
    hello
    >>> print " [pause] ".join["one, two, three, five, no four!".split[","]]
    one [pause]  two [pause]  three [pause]  five [pause]  no four!
    
    2
    >>> from_string = "h\ne\nl\nl\no"
    >>> print from_string
    h
    e
    l
    l
    o
    >>> print from_string.split[]
    ['h', 'e', 'l', 'l', 'o']
    >>> print "".join[from_string.split[]]
    hello
    >>> print " [pause] ".join["one, two, three, five, no four!".split[","]]
    one [pause]  two [pause]  three [pause]  five [pause]  no four!
    
    3
    The original string is : g
    e
    e
    k
    s
    f
    o
    r
    g
    e
    e
    k
    s
    
    The converted string : geeksforgeeks
    4
    O[n]

    Đầu raO[n]

    Độ phức tạp về thời gian: O [n]

    Không gian phụ trợ: O [n]

    Python3

    Phương pháp số 2: [dọc đến ngang] bằng cách sử dụng thay thế [] +

    Trong đó, chúng tôi thêm ký tự mới sau mỗi ký tự để mỗi phần tử được hiển thị ở dòng tiếp theo. & NBSP;

    test_str = 'geeksforgeeks'

    >>> from_string = "h\ne\nl\nl\no"
    >>> print from_string
    h
    e
    l
    l
    o
    >>> print from_string.split[]
    ['h', 'e', 'l', 'l', 'o']
    >>> print "".join[from_string.split[]]
    hello
    >>> print " [pause] ".join["one, two, three, five, no four!".split[","]]
    one [pause]  two [pause]  three [pause]  five [pause]  no four!
    
    8
    >>> from_string = "h\ne\nl\nl\no"
    >>> print from_string
    h
    e
    l
    l
    o
    >>> print from_string.split[]
    ['h', 'e', 'l', 'l', 'o']
    >>> print "".join[from_string.split[]]
    hello
    >>> print " [pause] ".join["one, two, three, five, no four!".split[","]]
    one [pause]  two [pause]  three [pause]  five [pause]  no four!
    
    9
    The original string is : geeksforgeeks
    The converted string : g
    e
    e
    k
    s
    f
    o
    r
    g
    e
    e
    k
    s
    0
    The original string is : geeksforgeeks
    The converted string : g
    e
    e
    k
    s
    f
    o
    r
    g
    e
    e
    k
    s
    1

    The original string is : geeksforgeeks
    The converted string : g
    e
    e
    k
    s
    f
    o
    r
    g
    e
    e
    k
    s
    2
    The original string is : geeksforgeeks
    The converted string : g
    e
    e
    k
    s
    f
    o
    r
    g
    e
    e
    k
    s
    3
    >>> from_string = "h\ne\nl\nl\no"
    >>> print from_string
    h
    e
    l
    l
    o
    >>> print from_string.split[]
    ['h', 'e', 'l', 'l', 'o']
    >>> print "".join[from_string.split[]]
    hello
    >>> print " [pause] ".join["one, two, three, five, no four!".split[","]]
    one [pause]  two [pause]  three [pause]  five [pause]  no four!
    
    2=
    >>> from_string = "h\ne\nl\nl\no"
    >>> print from_string
    h
    e
    l
    l
    o
    >>> print from_string.split[]
    ['h', 'e', 'l', 'l', 'o']
    >>> print "".join[from_string.split[]]
    hello
    >>> print " [pause] ".join["one, two, three, five, no four!".split[","]]
    one [pause]  two [pause]  three [pause]  five [pause]  no four!
    
    9__12
    The original string is : geeksforgeeks
    The converted string : g
    e
    e
    k
    s
    f
    o
    r
    g
    e
    e
    k
    s
    8

    The original string is : g
    e
    e
    k
    s
    f
    o
    r
    g
    e
    e
    k
    s
    
    The converted string : geeksforgeeks

    print____1010____31

    >>> from_string = "h\ne\nl\nl\no"
    >>> print from_string
    h
    e
    l
    l
    o
    >>> print from_string.split[]
    ['h', 'e', 'l', 'l', 'o']
    >>> print "".join[from_string.split[]]
    hello
    >>> print " [pause] ".join["one, two, three, five, no four!".split[","]]
    one [pause]  two [pause]  three [pause]  five [pause]  no four!
    
    2
    >>> from_string = "h\ne\nl\nl\no"
    >>> print from_string
    h
    e
    l
    l
    o
    >>> print from_string.split[]
    ['h', 'e', 'l', 'l', 'o']
    >>> print "".join[from_string.split[]]
    hello
    >>> print " [pause] ".join["one, two, three, five, no four!".split[","]]
    one [pause]  two [pause]  three [pause]  five [pause]  no four!
    
    3
    The original string is : g
    e
    e
    k
    s
    f
    o
    r
    g
    e
    e
    k
    s
    
    The converted string : geeksforgeeks
    4
    O[n] -> replace function

    Đầu raO[n]


    Cải thiện bài viết

    Lưu bài viết

  • Đọc
  • Bàn luận
  • Cải thiện bài viết

    Lưu bài viết

    Đọc

    Bàn luận
    The naive method can be used to print the list vertically vis. using the loops and printing each index element of each list successively will help us achieve this task.

    In danh sách đã được xử lý nhiều lần. Nhưng đôi khi chúng ta cần một định dạng khác nhau để có được đầu ra của danh sách. Điều này cũng có ứng dụng trong việc chuyển đổi ma trận. Danh sách in theo chiều dọc cũng có ứng dụng trong phát triển web. Hãy thảo luận về những cách nhất định trong đó nhiệm vụ này có thể đạt được.

    print

    >>> from_string = "h\ne\nl\nl\no"
    >>> print from_string
    h
    e
    l
    l
    o
    >>> print from_string.split[]
    ['h', 'e', 'l', 'l', 'o']
    >>> print "".join[from_string.split[]]
    hello
    >>> print " [pause] ".join["one, two, three, five, no four!".split[","]]
    one [pause]  two [pause]  three [pause]  five [pause]  no four!
    
    0=8
    >>> from_string = "h\ne\nl\nl\no"
    >>> print from_string
    h
    e
    l
    l
    o
    >>> print from_string.split[]
    ['h', 'e', 'l', 'l', 'o']
    >>> print "".join[from_string.split[]]
    hello
    >>> print " [pause] ".join["one, two, three, five, no four!".split[","]]
    one [pause]  two [pause]  three [pause]  five [pause]  no four!
    
    2
    >>> from_string = "h\ne\nl\nl\no"
    >>> print from_string
    h
    e
    l
    l
    o
    >>> print from_string.split[]
    ['h', 'e', 'l', 'l', 'o']
    >>> print "".join[from_string.split[]]
    hello
    >>> print " [pause] ".join["one, two, three, five, no four!".split[","]]
    one [pause]  two [pause]  three [pause]  five [pause]  no four!
    
    3'geeksforgeeks'1

    Phương pháp số 1: Sử dụng phương thức ngây thơ Phương pháp ngây thơ có thể được sử dụng để in danh sách theo chiều dọc vis. Sử dụng các vòng lặp và in từng phần tử chỉ mục của mỗi danh sách liên tiếp sẽ giúp chúng tôi đạt được nhiệm vụ này.

    Các

    print4printprint6=print8print9

    The original string is : geeksforgeeks
    The converted string : g
    e
    e
    k
    s
    f
    o
    r
    g
    e
    e
    k
    s
    2print
    >>> from_string = "h\ne\nl\nl\no"
    >>> print from_string
    h
    e
    l
    l
    o
    >>> print from_string.split[]
    ['h', 'e', 'l', 'l', 'o']
    >>> print "".join[from_string.split[]]
    hello
    >>> print " [pause] ".join["one, two, three, five, no four!".split[","]]
    one [pause]  two [pause]  three [pause]  five [pause]  no four!
    
    02

    Đầu ra:

    The original list is : [[1, 4, 5], [4, 6, 8], [8, 3, 10]]
    1 4 8 
    4 6 3 
    5 8 10 
    

    Cải thiện bài viết
    Using zip function, we map the elements at respective index to one other and after that print each of them. This performs the task of vertical printing.

    In danh sách đã được xử lý nhiều lần. Nhưng đôi khi chúng ta cần một định dạng khác nhau để có được đầu ra của danh sách. Điều này cũng có ứng dụng trong việc chuyển đổi ma trận. Danh sách in theo chiều dọc cũng có ứng dụng trong phát triển web. Hãy thảo luận về những cách nhất định trong đó nhiệm vụ này có thể đạt được.

    print

    >>> from_string = "h\ne\nl\nl\no"
    >>> print from_string
    h
    e
    l
    l
    o
    >>> print from_string.split[]
    ['h', 'e', 'l', 'l', 'o']
    >>> print "".join[from_string.split[]]
    hello
    >>> print " [pause] ".join["one, two, three, five, no four!".split[","]]
    one [pause]  two [pause]  three [pause]  five [pause]  no four!
    
    0=8
    >>> from_string = "h\ne\nl\nl\no"
    >>> print from_string
    h
    e
    l
    l
    o
    >>> print from_string.split[]
    ['h', 'e', 'l', 'l', 'o']
    >>> print "".join[from_string.split[]]
    hello
    >>> print " [pause] ".join["one, two, three, five, no four!".split[","]]
    one [pause]  two [pause]  three [pause]  five [pause]  no four!
    
    2
    >>> from_string = "h\ne\nl\nl\no"
    >>> print from_string
    h
    e
    l
    l
    o
    >>> print from_string.split[]
    ['h', 'e', 'l', 'l', 'o']
    >>> print "".join[from_string.split[]]
    hello
    >>> print " [pause] ".join["one, two, three, five, no four!".split[","]]
    one [pause]  two [pause]  three [pause]  five [pause]  no four!
    
    3'geeksforgeeks'1

    >>> from_string = "h\ne\nl\nl\no"
    >>> print from_string
    h
    e
    l
    l
    o
    >>> print from_string.split[]
    ['h', 'e', 'l', 'l', 'o']
    >>> print "".join[from_string.split[]]
    hello
    >>> print " [pause] ".join["one, two, three, five, no four!".split[","]]
    one [pause]  two [pause]  three [pause]  five [pause]  no four!
    
    8
    >>> from_string = "h\ne\nl\nl\no"
    >>> print from_string
    h
    e
    l
    l
    o
    >>> print from_string.split[]
    ['h', 'e', 'l', 'l', 'o']
    >>> print "".join[from_string.split[]]
    hello
    >>> print " [pause] ".join["one, two, three, five, no four!".split[","]]
    one [pause]  two [pause]  three [pause]  five [pause]  no four!
    
    32
    The original string is : geeksforgeeks
    The converted string : g
    e
    e
    k
    s
    f
    o
    r
    g
    e
    e
    k
    s
    0
    >>> from_string = "h\ne\nl\nl\no"
    >>> print from_string
    h
    e
    l
    l
    o
    >>> print from_string.split[]
    ['h', 'e', 'l', 'l', 'o']
    >>> print "".join[from_string.split[]]
    hello
    >>> print " [pause] ".join["one, two, three, five, no four!".split[","]]
    one [pause]  two [pause]  three [pause]  five [pause]  no four!
    
    34
    >>> from_string = "h\ne\nl\nl\no"
    >>> print from_string
    h
    e
    l
    l
    o
    >>> print from_string.split[]
    ['h', 'e', 'l', 'l', 'o']
    >>> print "".join[from_string.split[]]
    hello
    >>> print " [pause] ".join["one, two, three, five, no four!".split[","]]
    one [pause]  two [pause]  three [pause]  five [pause]  no four!
    
    0
    >>> from_string = "h\ne\nl\nl\no"
    >>> print from_string
    h
    e
    l
    l
    o
    >>> print from_string.split[]
    ['h', 'e', 'l', 'l', 'o']
    >>> print "".join[from_string.split[]]
    hello
    >>> print " [pause] ".join["one, two, three, five, no four!".split[","]]
    one [pause]  two [pause]  three [pause]  five [pause]  no four!
    
    36
    >>> from_string = "h\ne\nl\nl\no"
    >>> print from_string
    h
    e
    l
    l
    o
    >>> print from_string.split[]
    ['h', 'e', 'l', 'l', 'o']
    >>> print "".join[from_string.split[]]
    hello
    >>> print " [pause] ".join["one, two, three, five, no four!".split[","]]
    one [pause]  two [pause]  three [pause]  five [pause]  no four!
    
    37

    The original string is : geeksforgeeks
    The converted string : g
    e
    e
    k
    s
    f
    o
    r
    g
    e
    e
    k
    s
    2print
    >>> from_string = "h\ne\nl\nl\no"
    >>> print from_string
    h
    e
    l
    l
    o
    >>> print from_string.split[]
    ['h', 'e', 'l', 'l', 'o']
    >>> print "".join[from_string.split[]]
    hello
    >>> print " [pause] ".join["one, two, three, five, no four!".split[","]]
    one [pause]  two [pause]  three [pause]  five [pause]  no four!
    
    40

    Đầu ra:

    The original list is : [[1, 4, 5], [4, 6, 8], [8, 3, 10]]
    1 4 8
    4 6 3
    5 8 10
    

    Bài Viết Liên Quan

    Chủ Đề