开发者

Modifying specific array elements in Python 3.1

I have two arrays: array and least_common (filter array)

The following code iterates through array, checks for elements that match least_common, and if finds it, modifies it and appends it to a new array.

 for i in range (len(array)):
        for j in range(len(least_common)):
            if array[i] is least_common[j][0]:                
                new_array.append ((array[i]) + (array[i] * (mod[1]/100)))

However, if the element in array does not match any of the elements 开发者_开发知识库in least_common I wan't to append it to new_array, then iterate to the next element in array to begin the checking process again.


This code is a bit wonky to me -- I think you want to start with something more like:

lookup = set([x[0] for x in least_common])
new_array = []
for elem in array:
    if elem in lookup:
        new_array.append(elem + (elem * (mod[1]/100)))
    else:
        new_array.append(elem)


In Python, what you are trying to do is done using lists. There is another separate data type called arrays, but that is for totally different purpose. Please don't confuse and use the proper terminology, list.

Lists can be iterated through. You need to not index the elements out of the list and then access them using the index. That is C or C++ way of doing things and not python.

You use a list or a dictionary called mod in your original code. It is a bad idea to override builtin names. I tried to understand what you are trying, came up with the following code. Take it further, but before that, I think some beginner tutorials might help you as well.

new_array = []
somevalue = 0.001

for elem in array:
   for anotherelem in least_common:
            if elem == anotherelem[0]:
                    new_array.append(elem + (elem * somevalue))


Keep track of whether you found a match using a boolean, which you set to False before each inner loop and set to True within your if. After each iteration, if it's still False it means you found no matches and should then do your appending.

You should also follow what @Andrew says and iterate over lists using for a in array:. If you need the index, use for i, a in enumerate(array):. And be aware that is is not the same as ==.

new_array = []
for array_item in array:
    found = False
    for least_common_item in least_common:
        if array_item is least_common_item:
            found = True
    if not found:
        new_array.append (array_item * (1 + mod[1]/100))

You can also greatly shorten this code using in if you meant to use == instead of is:

for array_item in array:
    if array_item not in least_common:
        new_array.append (array_item * (1 + mod[1]/100))


Why not this:

least_common_set = frozenset(x[0] for x in least_common)
for e in array:
    if e is not in least_common_set:
        new_array.append(e + (e * (mod[1]/100)))


If I understand correctly your problem, here is a possible solution:

for e in array:
    for lc in least_common:
        if e is lc[0]:
            new_array.append(e + e * (md[1] / 100))
            break
    else:
        new_array.append(e)

The else clause in the for loop is executed when the loop terminates through exhaustion of the list, but not when the loop is terminated by a break statement.

Note that there is no need to use range or len, in Python you can just iterate on the elements of a sequence without involving indexes - you may use enumerate for that, but in this case you don't need to. Also, please don't use built-in names such as mod for your variables: here, I have renamed your dictionary md.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜