Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 数组保持为空_Python - Fatal编程技术网

Python 数组保持为空

Python 数组保持为空,python,Python,结果是 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,

结果是

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)
我希望如果出现这种情况,应该创建数组,以便将被刮取的数据放在两个单独的数组中


我尝试了一些方法,但仍然无法从内部函数的if语句创建数组

您无法为全局变量单项数组、组件文本ID赋值,但您可以进行就地更改,如追加。

通常不赞成,但是,如果在较新版本的Python上显式声明全局变量,则可以在技术上从函数内部更改全局变量的赋值。全局变量通常被认为是一种语言特性,所以请谨慎使用,但您最好将其作为语言的一个特性来了解

[]
['Formfactor', 'PSU']
另见

就我个人而言,我会使用以下方法,用显式返回变量替换全局变量:

single_item_arrays = []
component_text_ids = []

def getText_identifiers(component_id) :
    global single_item_arrays # Notice the explicit declaration as a global variable
    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)

您打算如何处理单项目阵列=格式化,PSU=[],[]?按照我的解释,它总是将一个空列表分配给单个\u项\u数组,因为Formatten将被分配一个空列表。此函数应检查使用的参数,并且通过该输入,它应使用一个数组组件\u textids中的文本ID从网站获取数据,对于所有项的每个textid(例如formfactor),必须将其放入数组中。以及另一阵列中的psu。你知道这个函数有更多的条件,这只是其中的一个。对于每个组件,它具有相同的条件,只是不同的文本ID和不同的长度结果是相同的。。[],[]['Formfactor',PSU']emptyWait,这些结果不一样。以前,OP得到的是[]['Formfactor',PSU',,现在他得到的是[],[]['Formfactor',PSU']。您实际期望的输出是什么?
def getText_identifiers(component_id) :
    single_item_arrays, component_text_ids = [], []
    if component_id is 'powersupply':
        for i in ['Formfactor','PSU',]:
            component_text_ids.append(i)
        single_item_arrays = formaten,PSU = [],[]
    return single_item_arrays, component_text_ids

single_item_arrays, component_text_ids = getText_identifiers('powersupply')
print(single_item_arrays)
print(component_text_ids)