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
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
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
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
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
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
1TypeError: must be str, not list
2TypeError: must be str, not list
3TypeError: must be str, not list
4TypeError: must be str, not list
3TypeError: must be str, not list
6TypeError: must be str, not list
3TypeError: must be str, not list
8TypeError: 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
1TypeError: must be str, not list
2TypeError: must be str, not list
3TypeError: must be str, not list
4TypeError: must be str, not list
3TypeError: must be str, not list
6TypeError: must be str, not list
3TypeError: must be str, not list
8TypeError: 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]
0class 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'
61 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 57
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 58
my_list = [1,2]
my_var_name = [k for k,v in locals[].items[] if v == my_list][0]
print[my_var_name]
0class 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'
6Geeks for Geeks 1, 2, 3, 4, 51
Geeks for Geeks 1, 2, 3, 4, 52
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]
0class 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'
61 2 3 4 5 in new line 1 2 3 4 52
Geeks for Geeks 1, 2, 3, 4, 52
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]
0class 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'
3a
TypeError: must be str, not list
0 TypeError: must be str, not list
1TypeError: must be str, not list
2TypeError: must be str, not list
3TypeError: must be str, not list
4TypeError: must be str, not list
3TypeError: must be str, not list
6TypeError: must be str, not list
3TypeError: must be str, not list
8TypeError: 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Đầ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
1TypeError: must be str, not list
2TypeError: must be str, not list
3TypeError: must be str, not list
4TypeError: must be str, not list
3TypeError: must be str, not list
6TypeError: must be str, not list
3TypeError: must be str, not list
8TypeError: 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]
0class 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'
2TypeError: must be str, not list
21TypeError: must be str, not list
22class 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
8TypeError: must be str, not list
25my_list = [1,2]
my_var_name = [k for k,v in locals[].items[] if v == my_list][0]
print[my_var_name]
0TypeError: must be str, not list
27Cá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
1TypeError: must be str, not list
2TypeError: must be str, not list
3TypeError: must be str, not list
4TypeError: must be str, not list
3TypeError: must be str, not list
6TypeError: must be str, not list
3TypeError: must be str, not list
8TypeError: 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]
0class 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'
6TypeError: must be str, not list
61Geeks for Geeks 1, 2, 3, 4, 52
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