Hướng dẫn how to print the name of a list in python - làm thế nào để in tên của một danh sách trong python

Tôi đang chạy Python 3.6 và Pandas 0.19.2 và muốn truy cập tên của một danh sách như sau:

# work on the following two lists
for list in [list1, list2]: 
    # loop through items in each list
    for entry in bucket_set: 
        # do stuff
    # let user know I finished working on list x
    print('i just finished working on: '+list)

Trong lần lặp đầu tiên, điều này nên làm công cụ và sau đó in ra: 'Tôi vừa hoàn thành làm việc trên List1'. Tuy nhiên: khi tôi chạy mã của mình, tôi sẽ gặp lỗi sau:

TypeError: must be str, not list

Điều này có ý nghĩa hoàn hảo (một danh sách không phải là một chuỗi), nhưng có cách nào tôi có thể lấy tên của danh sách của mình dưới dạng chuỗi ('list1') không? Tôi biết tôi có thể sử dụng một dict (đó là những gì tôi đang làm ngay bây giờ), nhưng dù sao tôi cũng quan tâm.is there any way I can get the name of my list as a string ('list1')? I know I can use a dict instead (which is what I'm doing right now), but I'm interested nonetheless.

Đã hỏi ngày 2 tháng 2 năm 2017 lúc 16:41Feb 2, 2017 at 16:41

Hướng dẫn how to print the name of a list in python - làm thế nào để in tên của một danh sách trong python

Martin Reindlmartin ReindlMartin Reindl

7692 Huy hiệu vàng7 Huy hiệu bạc26 Huy hiệu đồng2 gold badges7 silver badges26 bronze badges

6

Bạn có thể tạo một lớp để thường gói gọn một cái tên, nhưng đây là rất nhiều chi phí khi một từ điển sẽ đủ.

class NamedObject:
    def __init__(self, name, obj):
        self.name = name
        self.obj = obj

    def __getattr__(self, attr):
        if attr == 'name':
            return self.name
        else:
            return getattr(self.obj, attr)


unnamed_list = [1, 2, 3]
named_list = NamedObject('named_list', unnamed_list)

print(named_list) # [1, 2, 3]
print(named_list.name) # 'named_list'

Đã trả lời ngày 2 tháng 2 năm 2017 lúc 16:52Feb 2, 2017 at 16:52

Hướng dẫn how to print the name of a list in python - làm thế nào để in tên của một danh sách trong python

Jared Goguenjared GoguenJared Goguen

8.6132 Huy hiệu vàng15 Huy hiệu bạc35 Huy hiệu Đồng2 gold badges15 silver badges35 bronze badges

1

Bạn có thể tạo lớp hoặc từ điển, hoặc bạn có thể lặp lại các biến của bạn.

my_list = [1,2]
my_var_name = [k for k,v in locals().items() if v == my_list][0]
print(my_var_name)

Nó cho 'my_list'.

Hãy coi chừng liệt kê các giá trị cần phải là duy nhất!

Đã trả lời ngày 26 tháng 4 năm 2019 lúc 7:14Apr 26, 2019 at 7:14

Hướng dẫn how to print the name of a list in python - làm thế nào để in tên của một danh sách trong python

Có nhiều tùy chọn, một trong số đó là repr (danh sách)

Đã trả lời ngày 2 tháng 2 năm 2017 lúc 16:45Feb 2, 2017 at 16:45

Hướng dẫn how to print the name of a list in python - làm thế nào để in tên của một danh sách trong python

3

Sử dụng danh sách hiểu: Sử dụng danh sách hiểu để chuyển từng cái một vào mỗi yếu tố trong danh sách và in. & NBSP;

Các

  • Tên của một danh sách trong Python là gì?
  • Sử dụng danh sách hiểu: Sử dụng danh sách hiểu để chuyển từng cái một vào mỗi yếu tố trong danh sách và in. & NBSP;

    Các

    list in python can be done is following ways:

    • Tên của một danh sách trong Python là gì?for loop : Traverse from 0 to len(list) and print all elements of the list one by one using a for loop, this is the standard practice of doing it. 

    Python

    a

    TypeError: must be str, not list
    
    0
    TypeError: must be str, not list
    
    1
    TypeError: must be str, not list
    
    2
    TypeError: must be str, not list
    
    3
    TypeError: must be str, not list
    
    4
    TypeError: must be str, not list
    
    3
    TypeError: must be str, not list
    
    6
    TypeError: must be str, not list
    
    3
    TypeError: must be str, not list
    
    8
    TypeError: must be str, not list
    
    3__

    Các

    Sử dụng danh sách hiểu: Sử dụng danh sách hiểu để chuyển từng cái một vào mỗi yếu tố trong danh sách và in. & NBSP;

    • Các * symbol is use to print the list elements in a single line with space. To print all elements in new lines or separated by comma use sep=”\n” or sep=”, ” respectively. 

    Python

    a

    TypeError: must be str, not list
    
    0
    TypeError: must be str, not list
    
    1
    TypeError: must be str, not list
    
    2
    TypeError: must be str, not list
    
    3
    TypeError: must be str, not list
    
    4
    TypeError: must be str, not list
    
    3
    TypeError: must be str, not list
    
    6
    TypeError: must be str, not list
    
    3
    TypeError: must be str, not list
    
    8
    TypeError: must be str, not list
    
    3__

    my_list = [1,2]
    my_var_name = [k for k,v in locals().items() if v == my_list][0]
    print(my_var_name)
    
    0
    class NamedObject:
        def __init__(self, name, obj):
            self.name = name
            self.obj = obj
    
        def __getattr__(self, attr):
            if attr == 'name':
                return self.name
            else:
                return getattr(self.obj, attr)
    
    
    unnamed_list = [1, 2, 3]
    named_list = NamedObject('named_list', unnamed_list)
    
    print(named_list) # [1, 2, 3]
    print(named_list.name) # 'named_list'
    
    6
    1 2 3 4 5
    printing lists separated by commas
    1, 2, 3, 4, 5
    printing lists in new line
    1
    2
    3
    4
    5
    7
    1 2 3 4 5
    printing lists separated by commas
    1, 2, 3, 4, 5
    printing lists in new line
    1
    2
    3
    4
    5
    8

    my_list = [1,2]
    my_var_name = [k for k,v in locals().items() if v == my_list][0]
    print(my_var_name)
    
    0
    class NamedObject:
        def __init__(self, name, obj):
            self.name = name
            self.obj = obj
    
        def __getattr__(self, attr):
            if attr == 'name':
                return self.name
            else:
                return getattr(self.obj, attr)
    
    
    unnamed_list = [1, 2, 3]
    named_list = NamedObject('named_list', unnamed_list)
    
    print(named_list) # [1, 2, 3]
    print(named_list.name) # 'named_list'
    
    6
    Geeks for Geeks
    1, 2, 3, 4, 5
    1
    Geeks for Geeks
    1, 2, 3, 4, 5
    2

    Các

    my_list = [1,2]
    my_var_name = [k for k,v in locals().items() if v == my_list][0]
    print(my_var_name)
    
    0
    class NamedObject:
        def __init__(self, name, obj):
            self.name = name
            self.obj = obj
    
        def __getattr__(self, attr):
            if attr == 'name':
                return self.name
            else:
                return getattr(self.obj, attr)
    
    
    unnamed_list = [1, 2, 3]
    named_list = NamedObject('named_list', unnamed_list)
    
    print(named_list) # [1, 2, 3]
    print(named_list.name) # 'named_list'
    
    6
    1 2 3 4 5
    in new line
    1
    2
    3
    4
    5
    2
    Geeks for Geeks
    1, 2, 3, 4, 5
    2

    Sử dụng danh sách hiểu: Sử dụng danh sách hiểu để chuyển từng cái một vào mỗi yếu tố trong danh sách và in. & NBSP;

    Đầu ra

    1 2 3 4 5
    printing lists separated by commas
    1, 2, 3, 4, 5
    printing lists in new line
    1
    2
    3
    4
    5

    • CácIf it is a list of strings we can simply join them using join() function, but if the list contains integers then convert it into string and then use join() function to join them to a string and print the string. 

    Python

    my_list = [1,2]
    my_var_name = [k for k,v in locals().items() if v == my_list][0]
    print(my_var_name)
    
    0
    class NamedObject:
        def __init__(self, name, obj):
            self.name = name
            self.obj = obj
    
        def __getattr__(self, attr):
            if attr == 'name':
                return self.name
            else:
                return getattr(self.obj, attr)
    
    
    unnamed_list = [1, 2, 3]
    named_list = NamedObject('named_list', unnamed_list)
    
    print(named_list) # [1, 2, 3]
    print(named_list.name) # 'named_list'
    
    6'my_list'2'my_list'3

    a

    TypeError: must be str, not list
    
    0
    TypeError: must be str, not list
    
    1
    TypeError: must be str, not list
    
    2
    TypeError: must be str, not list
    
    3
    TypeError: must be str, not list
    
    4
    TypeError: must be str, not list
    
    3
    TypeError: must be str, not list
    
    6
    TypeError: must be str, not list
    
    3
    TypeError: must be str, not list
    
    8
    TypeError: must be str, not list
    
    3__

    my_list = [1,2]
    my_var_name = [k for k,v in locals().items() if v == my_list][0]
    print(my_var_name)
    
    0 a 8a 9__12

    Hướng dẫn how to print the name of a list in python - làm thế nào để in tên của một danh sách trong python

    Đầu ra

    Geeks for Geeks
    1, 2, 3, 4, 5

    • Sử dụng bản đồ: Sử dụng map () để chuyển đổi từng mục trong danh sách thành một chuỗi nếu danh sách không phải là một chuỗi, sau đó tham gia chúng: & nbsp; Use map() to convert each item in the list to a string if list is not a string, and then join them: 

    Python

    a

    TypeError: must be str, not list
    
    0
    TypeError: must be str, not list
    
    1
    TypeError: must be str, not list
    
    2
    TypeError: must be str, not list
    
    3
    TypeError: must be str, not list
    
    4
    TypeError: must be str, not list
    
    3
    TypeError: must be str, not list
    
    6
    TypeError: must be str, not list
    
    3
    TypeError: must be str, not list
    
    8
    TypeError: must be str, not list
    
    3__

    my_list = [1,2]
    my_var_name = [k for k,v in locals().items() if v == my_list][0]
    print(my_var_name)
    
    0
    class NamedObject:
        def __init__(self, name, obj):
            self.name = name
            self.obj = obj
    
        def __getattr__(self, attr):
            if attr == 'name':
                return self.name
            else:
                return getattr(self.obj, attr)
    
    
    unnamed_list = [1, 2, 3]
    named_list = NamedObject('named_list', unnamed_list)
    
    print(named_list) # [1, 2, 3]
    print(named_list.name) # 'named_list'
    
    6'my_list'2
    TypeError: must be str, not list
    
    21
    TypeError: must be str, not list
    
    22
    class NamedObject:
        def __init__(self, name, obj):
            self.name = name
            self.obj = obj
    
        def __getattr__(self, attr):
            if attr == 'name':
                return self.name
            else:
                return getattr(self.obj, attr)
    
    
    unnamed_list = [1, 2, 3]
    named_list = NamedObject('named_list', unnamed_list)
    
    print(named_list) # [1, 2, 3]
    print(named_list.name) # 'named_list'
    
    6a 8
    TypeError: must be str, not list
    
    25

    my_list = [1,2]
    my_var_name = [k for k,v in locals().items() if v == my_list][0]
    print(my_var_name)
    
    0
    TypeError: must be str, not list
    
    27

    Các

    Đầu ra

    1 2 3 4 5
    in new line
    1
    2
    3
    4
    5

    • Sử dụng bản đồ: Sử dụng map () để chuyển đổi từng mục trong danh sách thành một chuỗi nếu danh sách không phải là một chuỗi, sau đó tham gia chúng: & nbsp;Use list comprehension to go one by one to each element in list and print. 

    Python3

    a

    TypeError: must be str, not list
    
    0
    TypeError: must be str, not list
    
    1
    TypeError: must be str, not list
    
    2
    TypeError: must be str, not list
    
    3
    TypeError: must be str, not list
    
    4
    TypeError: must be str, not list
    
    3
    TypeError: must be str, not list
    
    6
    TypeError: must be str, not list
    
    3
    TypeError: must be str, not list
    
    8
    TypeError: must be str, not list
    
    3__

    Các

    my_list = [1,2]
    my_var_name = [k for k,v in locals().items() if v == my_list][0]
    print(my_var_name)
    
    0
    class NamedObject:
        def __init__(self, name, obj):
            self.name = name
            self.obj = obj
    
        def __getattr__(self, attr):
            if attr == 'name':
                return self.name
            else:
                return getattr(self.obj, attr)
    
    
    unnamed_list = [1, 2, 3]
    named_list = NamedObject('named_list', unnamed_list)
    
    print(named_list) # [1, 2, 3]
    print(named_list.name) # 'named_list'
    
    6
    TypeError: must be str, not list
    
    61
    Geeks for Geeks
    1, 2, 3, 4, 5
    2

    Sử dụng danh sách hiểu: Sử dụng danh sách hiểu để chuyển từng cái một vào mỗi yếu tố trong danh sách và in. & NBSP;

    Đầu ra

    1 2 3 4 5 
    In new line
    1
    2
    3
    4
    5


    Tên của một danh sách trong Python là gì?

    Python có một loại danh sách tích hợp tuyệt vời có tên là "Danh sách".Danh sách chữ được viết trong dấu ngoặc vuông [].Danh sách hoạt động tương tự như các chuỗi - sử dụng hàm Len () và dấu ngoặc vuông [] để truy cập dữ liệu, với phần tử đầu tiên tại INDEX 0.list". List literals are written within square brackets [ ]. Lists work similarly to strings -- use the len() function and square brackets [ ] to access data, with the first element at index 0.

    In là gì ('') trong Python?

    Định nghĩa và cách sử dụng.Hàm in () in thông báo được chỉ định lên màn hình hoặc thiết bị đầu ra tiêu chuẩn khác.Thông báo có thể là một chuỗi hoặc bất kỳ đối tượng nào khác, đối tượng sẽ được chuyển đổi thành một chuỗi trước khi được ghi vào màn hình.prints the specified message to the screen, or other standard output device. The message can be a string, or any other object, the object will be converted into a string before written to the screen.