single_item_arrays = []
component_text_ids = []
def getText_identifiers(component_id) :
if component_id is 'powersupply':
for i in ['Formfactor','PSU',]:
component_text_ids.append(i)
single_item_arrays = formaten,PSU = [],[]
getText_identifiers('powersupply')
print(single_item_arrays)
print(component_text_ids)
结果是
[]
['Formfactor', 'PSU']
我希望如果出现这种情况,应该创建数组,以便将被刮取的数据放在两个不同的数组中。你知道吗
我尝试了一些方法,但仍然无法从函数内部的if语句创建数组
一般不赞成,但如果在较新版本的Python上显式声明变量
global
,则可以从函数内部从技术上更改全局变量的赋值。全局变量通常被认为是bad thing,因此请谨慎使用-但您最好了解它作为语言的一个特性。你知道吗另见Use of "global" keyword in Python
我个人会使用以下方法,用显式返回变量替换全局变量:
您不能为全局变量(
single_item_arrays
,component_text_ids
)赋值,但您可以进行像append
这样的就地更改。你知道吗相关问题 更多 >
编程相关推荐