Python:List被新列表替换

时间:2015-03-25 13:54:44

标签: python python-3.x

我在python中有一个关于列表的问题。我使用append方法实际将值附加到我的列表中,现在它只用新值替换了列表。

这是我的代码:

def init(serial):
    serial_number = serial
    api_call = "http://wwww.herecomesmyhyperlink/"+serial_number    
    result = []
    with open('allserials.csv') as csvfile:
        reader = csv.reader(csvfile, delimiter=';', quotechar='|')
        for row in reader:
            if row[0].strip() == api_call:
                result.append(row[1].strip())

    call_api(serial_number,result)
    return

def call_api(get_serial,get_result):
    list_serial = []

    for i in range(len(get_result)):
        # do an api call
        ....
        # get result of api call
        list_serial.append(api_result)

    sort_serials(list_serial)
    return

def sort_serials(get_list_serial)
    sorted_list_serial = sorted(get_list_serial, reverse=True)
    print(sorted_list_serial)
    max_results = 10
    length_of_sorted_list_serial = len(get_list_serial)

    if length_of_sorted_list_serial < max_results:
        get_first_list_element = sorted_list_serial[0]
        get_second_element_of_that_list = get_first_list_element[1]
        init(get_second_element_of_that_list)
    else:
        print("it is not smaller")
    return

print(init('1320739'))

sorted_list_serial将包含以下内容:[rankingid,serial,title]

get_second_element_of_that_list[serial]

问题在于,当我运行我的代码时,我得到了以下结果:

s: 1320739,      max result:10       length of the list:3
s: 1523039,      max result:10       length of the list:9

代码所做的是,它不是拥有12个项目的列表,而是用3个项目替换列表,新项目列表包含9个项目。

我想要的是实际上有一个包含12个项目的新列表,这样前3个项目仍然在列表中,其他9个元素被添加到原始列表中。

1 个答案:

答案 0 :(得分:1)

该列表的范围是函数call_api(),因此请将其拉出,或将其传递给每个函数,或者创建一个类。

def init(serial):
    serial_number = serial

    result = []
    with open('allserials.csv') as csvfile:
      result.append()


    return result

def call_api(get_serial,get_result):
    # list_serial = []
    #
    # Move this out
    # Or pass it along to each function

    for i in range(len(get_result)):
        # do an api call
        ....
        # get result of api call
        list_serial.append(api_result)


    return list_serial

def sort_serials(get_list_serial)
    sorted_list_serial = sorted(get_list_serial, reverse=True)

    max_results = 10
    length_of_sorted_list_serial = len(get_list_serial)

    if length_of_sorted_list_serial < max_results:
        get_first_list_element = sorted_list_serial[0]
        get_second_element_of_that_list = get_first_list_element[1]

    else:
        print("it is not smaller")
    return {'get_second_element_of_that_list':get_second_element_of_that_list, 'sorted_serial_list':sorted_serial_list}

因此将其范围扩展到相同的函数,并让其他函数返回结果:

def run():
    list_serial = []
    serial_number = '1320739'
    result = init(serial_number)
    # here the items get set
    list_serial = call_api(serial_number,result)
    # here they get sorted
    serial_sorted = sort_serials(list_serial)
    # list serial is now the sorted list
    list_serial = serial_sorted['sorted_serial_list']
    get_second_element_of_that_list = serial_sorted['get_second_element_of_that_list']

    init(get_second_element_of_that_list)

或者重新定义它的传递方式:

    serial_number = '1320739'
    init(serial_number, list_serial)
    call_api(serial_number,result, list_serial)
    sort_serials(list_serial)

    init(get_second_element_of_that_list, list_serial)

或者只是拉出来:

.
.
.
list_serial = []
print(init('1320739'))

或创建一个类:

class SomeClassNameHere(object):
    def __init__(self,serialnumber=None, item2=''):
        self.serialnumber = serialnumber
        self.item3 = item2
        self.listserial = []
        self.run(item2)

    def doOtherStuff(self):
        # self.listserial will be updated
        self.listserial = [1,2,3]
        print(self.item3)
        print(self.serialnumber)

    def run(self,passeditem2):
        print('Item2 has been passed: {0}'.format(passeditem2))
        print('listserial not updated:',self.listserial)
        self.doOtherStuff()
        print('listserial updated:',self.listserial)



 here = SomeClassNameHere(serialnumber='456',item2='somestring')
 print(here.serialnumber)
 print(here.item3)
 here.run()
 here.doOtherStuff()