-2
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)

the result is

[]
['Formfactor', 'PSU']

i want that that if the condition occurs the arrays should be created so that the data thats being scraped would be put in two seperate arrays.

i tried a couple things still can't create the arrays from the inside function's if statement

Mazdak
  • 105,000
  • 18
  • 159
  • 188
Y_Lakdime
  • 825
  • 2
  • 15
  • 33
  • What are you intending to do with `single_item_arrays = formaten,PSU = [],[]`? The way I interpret it, it will always be assigning an empty list to `single_item_arrays`, since `formaten` will be assigned an empty list. – Makoto Jan 01 '15 at 19:02
  • this function should check what argument is used, and by that input it should use the text_ids thats in the one array component_textids for getting data from the website, and for each textid for example formfactor of all the items it must be put in an array. and the psu in another array. you know – Y_Lakdime Jan 01 '15 at 19:12
  • this function has more conditions this is just one of the ten. for each component it has the same condition just different text_ids and different length – Y_Lakdime Jan 01 '15 at 19:15

2 Answers2

0

You couldn't do assignment for global variables (single_item_arrays,component_text_ids), but you can do in place change like append.

Mazdak
  • 105,000
  • 18
  • 159
  • 188
0

Generally frowned upon, but you can technically change the assigned value of a global variable from inside a function, if you explicitly declare the variable global, on more recent versions of Python. Global variables are generally considered a bad thing, so use sparingly and carefully - but you might as well know about it as a feature of the language.

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)

See also Use of "global" keyword in Python

Personally I would use the following, replacing global variables with explicit return variables:

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)
Community
  • 1
  • 1
zehnpaard
  • 6,003
  • 2
  • 25
  • 40
  • Still result are same.. ([], []) ['Formfactor', 'PSU'] empty – Shahriar Jan 01 '15 at 20:57
  • Wait, those results aren't the same. Before, OP was getting [] ['Formfactor', 'PSU'], now he is getting [[], []] ['Formfactor', 'PSU']. What are you actually expecting as output? – zehnpaard Jan 01 '15 at 20:59