开发者

Accessing only part of a dictionary in a for using Python

My example dictionary is this

data_dictionary = {1:'blue',2:'green',3:'red',4:'orange',5:'purple',6:'mauve'}

The data_dictionary can have more elements depending on the incoming data . The first value is what we call a payload_index. I always get payload_index 1 to 4 .

I need to assemble a list from this. Pretty easy:

for payload_index in data_dictionary :
            assembled_packet.append(data_dictionary[payload_index])

My problem is I need to always skip the 3rd element. I guess I could do an if but that would be inefficient:

for payload_index in data_dictionary :
            if payload_index <> 3:
                assembled_packet.append(data_dictionary[payload_index])

I could do it in two steps and do the first three elements but the problem is I cannot figure out how to get the rest since the number of elements after the 3rd varies. I tried using an impossibly high index (below) but that obviously fails:

#get element 1 an开发者_如何学Pythond two
for index in range(0,3):
            assembled_packet.append(data_dictionary[index])

#get element 1 and two
for index in range(4,999):
            assembled_packet.append(data_dictionary[index])

Any ideas? Thanks!


inefficient? That seems like a premature optimisation! Just use normal code:

for payload_index in data_dictionary:
    if payload_index != 3:
        assembled_packet.append(data_dictionary[payload_index])

or even better:

assembled_packet = [data_dictionary[index] for index in data_dictionary if index != 3]

Of course, you could just simply do:

>>> d = {1:'blue',2:'green',3:'red',4:'orange',5:'purple',6:'mauve'}
>>> d.pop(3)
'red'
>>> list(d.values())        # in py3k; in python-2.x d.values() would do
['blue', 'green', 'orange', 'purple', 'mauve']

P.S. <> is long since deprecated.


To make those for loops work you can do this:

# Get the remaining elements
size = len(data_dictionary)
for index in range(4,size):
            assembled_packet.append(data_dictionary[index])


The best way i can think of is :

output = [elem for elem in data_dictionary.items() if elem[0]!=3]


from you answer is not obvious if you want to skip firts three items or just the 3rd item. if the former is the case, and you are not sure, that your keys are always 1, 2, 3, you can do something like this:

keys = list(sorted(data_dictionary.keys()))[3:]
for payload_index in keys:
    assembled_packet.append(data_dictionary[payload_index])


My problem is I need to always skip the 3rd element. I guess I could do an if but that would be inefficient:

This is even more inefficient:

for payload_index in data_dictionary :
    ...
        assembled_packet.append(data_dictionary[payload_index])

I'd do it this way (Python >= 2.5):

assembled.extend(color for (number, color) in data.iteritems() if number != 3)

For Python 3.x change iteritems to items.

And don't encode type names/descriptions in variable names. It's evil.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜