python 来自函数的列表
python List from function
我遇到了一个问题,我正在一个函数中创建 2 个列表,但我无法在函数外使用它们,因为它说 "NameError: name "ListB" is not defined"。
我需要列表来创建元组并将元组写入字典:)
#
#create 2 lists and 1 dictonary with the same length
#Example: index length is 3
def adder():
ListB = list()
ListC = list()
while True:
insert1 = input("List2 add: ")
insert2 = input("List3 add: ")
ListB.append(insert1)
ListC.append(insert2)
print("""if list length is ok, write "ok" """)
inputPerson = str(input())
if inputPerson == "ok":
break
return ListB, ListC
#run adder
adder = adder()
list2 = [] # create list2/3 with same index length
list3 = [] # to add ListB to list2 and ListC to list3
list2.append(ListB) #add ListB to list2
list3.append(ListC) #add ListC to list3
tupleList = list(zip(list2, list3)) # take element from list2 and
print(tupleList) #Test # list3 in (x, y) order
#create a dictonary with they keyword KeyX X = 0,1,2,3...n : tupleList[0]..[n]
#depending on index length, but X = tupleList[n]!!
dict_List = { \
'Key0' : tupleList[0],
'Key1' : tupleList[1],
'Key2' : tupleList[2],
}
#print out the result
print("Dict_List:", dict_List)
print("Key0", dict_List['Key0'])
print("Key1", dict_List['Key1'])
print("Key2", dict_List['Key2'])
现在我不知道如何创建一个自动的字典
使用 KeyX 等创建一个新的 "entry"
希望有人能帮助我。
试试这样的东西:
ListB, ListC = adder()
作为你的函数 returns 两个值,你可以像元组一样解压它们。
您必须知道的是,从函数内部声明变量会使它成为局部变量并限制在函数的范围内。因此,您无法从外部访问它。
当您调用 adder()
时,返回值没有任何名称,它只是一个值,您必须像 adder = adder()
一样将它分配给一个新变量。这意味着变量 adder
现在包含两个返回的列表。
但是,您正在覆盖您的函数(因为名称相同),这被认为是不好的做法。你最好做一些像 lists = adder()
.
这样的事情
然后,您可以使用lists[0]
访问创建的ListB
。但正如我所说,你也可以直接解压它:ListB, ListC = adder()
.
我遇到了一个问题,我正在一个函数中创建 2 个列表,但我无法在函数外使用它们,因为它说 "NameError: name "ListB" is not defined"。
我需要列表来创建元组并将元组写入字典:)
#
#create 2 lists and 1 dictonary with the same length
#Example: index length is 3
def adder():
ListB = list()
ListC = list()
while True:
insert1 = input("List2 add: ")
insert2 = input("List3 add: ")
ListB.append(insert1)
ListC.append(insert2)
print("""if list length is ok, write "ok" """)
inputPerson = str(input())
if inputPerson == "ok":
break
return ListB, ListC
#run adder
adder = adder()
list2 = [] # create list2/3 with same index length
list3 = [] # to add ListB to list2 and ListC to list3
list2.append(ListB) #add ListB to list2
list3.append(ListC) #add ListC to list3
tupleList = list(zip(list2, list3)) # take element from list2 and
print(tupleList) #Test # list3 in (x, y) order
#create a dictonary with they keyword KeyX X = 0,1,2,3...n : tupleList[0]..[n]
#depending on index length, but X = tupleList[n]!!
dict_List = { \
'Key0' : tupleList[0],
'Key1' : tupleList[1],
'Key2' : tupleList[2],
}
#print out the result
print("Dict_List:", dict_List)
print("Key0", dict_List['Key0'])
print("Key1", dict_List['Key1'])
print("Key2", dict_List['Key2'])
现在我不知道如何创建一个自动的字典 使用 KeyX 等创建一个新的 "entry"
希望有人能帮助我。
试试这样的东西:
ListB, ListC = adder()
作为你的函数 returns 两个值,你可以像元组一样解压它们。
您必须知道的是,从函数内部声明变量会使它成为局部变量并限制在函数的范围内。因此,您无法从外部访问它。
当您调用 adder()
时,返回值没有任何名称,它只是一个值,您必须像 adder = adder()
一样将它分配给一个新变量。这意味着变量 adder
现在包含两个返回的列表。
但是,您正在覆盖您的函数(因为名称相同),这被认为是不好的做法。你最好做一些像 lists = adder()
.
然后,您可以使用lists[0]
访问创建的ListB
。但正如我所说,你也可以直接解压它:ListB, ListC = adder()
.