I am trying to learn dynamic programming by followin an online video. The original video is using javascript and I am trying to use python to implement the same. However, I am not able to locate the error in my python implementation.
The question is as follows
write a fn. bestsum(targetsum, numbers) that takes in a targetsum and
an array of numbers as arguments.
The fn. should return an array containing the shortest combination of
numbers that add up to exactly the targetsum.
If there is a tie for the shortest combination, you may return any of
the shortest.
The javascript implementation is as follows.
const bestSum = (targetSum, numbers, memo={}) => {
if (targetSum in memo) return memo[targetSum];
if (targetSum === 0) return [];
if (targetSum < 0) return null;
let shortest_com = null;
for (let num of numbers) {
const remainder = targetSum - num;
const remainder_com = bestSum(remainder, numbers, memo);
if (remainder_com !== null) {
const combination = [...remainder_com, num];
if (shortest_com === null || combination.length < shortest_com.length) {
shortest_com = combination;
}
}
}
memo[targetSum] = shortest_com
return shortest_com;
};
console.log(bestSum(7, [5, 3, 4, 7]));
console.log(bestSum(8, [2, 3, 5]));
console.log(bestSum(8, [1, 4, 5]));
console.log(bestSum(100, [1, 2, 5, 25]));
Python code I implemented is
from typing import Any, Dict, List, Optional
def best_sum(target: int, numbers: List[int], memo:Dict[int, Any]={}) -> Optional[List[int]]:
if target in memo.keys():
return memo.get(target)
if target == 0:
return []
if target < 0:
return None
shortest_combination: Optional[List] = None
for num in numbers:
partial = best_sum(target=target - num, numbers=numbers, memo=memo)
if partial != None:
print(num)
partial.append(num)
if (shortest_combination == None) or (len(partial) < len(shortest_combination)):
shortest_combination = partial
memo[target] = shortest_combination
return shortest_combination
if __name__ == "__main__":
print(best_sum(target=100, numbers=[1, 2, 5, 25]))
For the test case: target=100, numbers=[1, 2, 5, 25].
Javascript implementation gives.
[ 25, 25, 25, 25 ]
But Python gives.
[25, 1, 1, 2, 1, 2, 1, 2, 1, 2, 5, 1, 2, 5, 1, 2, 5, 1, 2, 5, 1, 2, 5, 1, 2, 5, 1, 2, 5, 1, 2, 5, 1, 2, 5, 1, 2, 5, 1, 2, 5, 1, 2, 5, 1, 2, 5, 1, 2, 5, 1, 2, 5, 1, 2, 5, 1, 2, 5, 1, 2, 5, 1, 2, 5, 1, 2, 5, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25, 1, 2, 5, 25]
The problem is in this snippet:
if partial != None:
partial.append(num)
if (shortest_combination == None) or (len(partial) < len(shortest_combination)):
shortest_combination = partial
The Javascript appoach creates a copy of the list remainder_com with the element num appended. In your approach, you're appending to partial directly without creating a copy. Thus, in every iteration the same list will be used to modifications, which is not desired. Change it to
# Creates a copy of `partial` with `num` appended
combination = partial[:] + [num]
if (shortest_combination == None) or (len(combination) < len(shortest_combination)):
shortest_combination = combination
This outputs [25, 25, 25, 25] as expected.
I need to find the key-value pair in a dictionary, based on keys.
Could someone explain it how can it be done, please?
Sort the keys of word_freq ascendingly.
Please create a new dictionary called word_freq2 based on word_freq with the keys sorted ascendingly.
There are several ways to achieve that goal but many of the ways are beyond what we have covered so far in the course. There is one way that we'll describe employing what you have learned. Please feel free to use this way or any other way you want.
First, extract the keys of word_freq and convert it to a list called keys.
Sort the keys list.
Create an empty dictionary word_freq2.
I am not able to write the for loop for the below question. Any help would be highly appreciated
Use a FOR loop to iterate each value in keys. For each key iterated, find the corresponding value in word_freq and insert the key-value pair to word_freq2.
word_freq = {'love': 25, 'conversation': 1, 'every': 6, "we're": 1, 'plate': 1, 'sour': 1, 'jukebox': 1, 'now': 11, 'taxi': 1, 'fast': 1, 'bag': 1, 'man': 1, 'push': 3, 'baby': 14, 'going': 1, 'you': 16, "don't": 2, 'one': 1, 'mind': 2, 'backseat': 1, 'friends': 1, 'then': 3, 'know': 2, 'take': 1, 'play': 1, 'okay': 1, 'so': 2, 'begin': 1, 'start': 2, 'over': 1, 'body': 17, 'boy': 2, 'just': 1, 'we': 7, 'are': 1, 'girl': 2, 'tell': 1, 'singing': 2, 'drinking': 1, 'put': 3, 'our': 1, 'where': 1, "i'll": 1, 'all': 1, "isn't": 1, 'make': 1, 'lover': 1, 'get': 1, 'radio': 1, 'give': 1, "i'm": 23, 'like': 10, 'can': 1, 'doing': 2, 'with': 22, 'club': 1, 'come': 37, 'it': 1, 'somebody': 2, 'handmade': 2, 'out': 1, 'new': 6, 'room': 3, 'chance': 1, 'follow': 6, 'in': 27, 'may': 2, 'brand': 6, 'that': 2, 'magnet': 3, 'up': 3, 'first': 1, 'and': 23, 'pull': 3, 'of': 6, 'table': 1, 'much': 2, 'last': 3, 'i': 6, 'thrifty': 1, 'grab': 2, 'was': 2, 'driver': 1, 'slow': 1, 'dance': 1, 'the': 18, 'say': 2, 'trust': 1, 'family': 1, 'week': 1, 'date': 1, 'me': 10, 'do': 3, 'waist': 2, 'smell': 3, 'day': 6, 'although': 3, 'your': 21, 'leave': 1, 'want': 2, "let's": 2, 'lead': 6, 'at': 1, 'hand': 1, 'how': 1, 'talk': 4, 'not': 2, 'eat': 1, 'falling': 3, 'about': 1, 'story': 1, 'sweet': 1, 'best': 1, 'crazy': 2, 'let': 1, 'too': 5, 'van': 1, 'shots': 1, 'go': 2, 'to': 2, 'a': 8, 'my': 33, 'is': 5, 'place': 1, 'find': 1, 'shape': 6, 'on': 40, 'kiss': 1, 'were': 3, 'night': 3, 'heart': 3, 'for': 3, 'discovering': 6, 'something': 6, 'be': 16, 'bedsheets': 3, 'fill': 2, 'hours': 2, 'stop': 1, 'bar': 1}
keys = list(word_freq.keys()) #extract the keys of word_freq and convert it to a list called keys
print(keys)
for i in sorted (word_freq.keys()): #Sort the keys list.
print(i)
word_freq2 = {} #Create an empty dictionary word_freq2
It turns out to be a very simple solution after going through the Python Dictionary Examples and Methods
for value in keys:
word_freq2[value] = word_freq.get(value)
print(word_freq2)
Python Dictionary Examples and Methods.
I'm trying to knowing which is the color of a pixel through it's x and y. The colors are from this image.
Capturing the colors with Photoshop I've got this list of colors:
"#5D385A", "#6D3B47", "#6F5C4B", "#50717A", "#547057", "#4C6180", "#717080", "#705574", "#726B59", "#5E4854", "#415A4B", "#425A64", "#3A4E6F"
However, when I try to get the color of a pixel from the image, this color doesn't match with the previous list. And, I've got 95 different colors when in the image there are only 13 different colors.
I open the image and get the color from a pixel with this class:
import PIL.Image
class Image:
def __init__(self, file):
self.image = PIL.Image.open(file).convert("RGB")
def get_color(self, x, y):
color = self.image.getpixel((x,y))
color = ("#%02x%02x%02x" % color).upper()
return color
Here is a short list of x and y of positions where I take the color:
144, 74
140, 46
150, 53
85, 87
160, 48
147, 60
137, 49
149, 53
148, 60
143, 52
161, 30
166, 23
134, 38
146, 29
155, 40
129, 37
154, 66
153, 38
151, 33
128, 36
How is that possible? How can I get 95 different colors from the image when there is only 13 different colors?
Edit I:
I have get all the colors from each pixel in the image and no one has the color what I get with Photoshop.
I have got 256 different colors, this is the list and number times found it.
{'#885F7D': 15, '#541B47': 15, '#68355B': 819, '#65355D': 17, '#78384A': 19, '#7E3942': 19, '#7B3846': 4588, '#7C3346': 39, '#7D3046': 50, '#773F4C': 21, '#785A49': 4, '#775F49': 35, '#765C49': 17540, '#7A4648': 21, '#756349': 62, '#785B49': 56, '#7C3546': 14, '#765D49': 12, '#7A4F48': 14, '#7C3746': 29, '#785549': 7, '#775D4A': 8, '#785749': 8, '#551743': 1, '#6A3158': 39, '#68325A': 6, '#86617E': 1, '#66385D': 31, '#6C2C56': 6, '#6C2A56': 7, '#6D2B54': 3, '#678D97': 88, '#2C5B6A': 60, '#416C79': 43, '#3F717A': 7, '#43686A': 64, '#5C5F71': 32, '#465771': 3, '#5E5666': 14, '#5D4C66': 7, '#644160': 2, '#683C5F': 2, '#659197': 2, '#1C606C': 88, '#32767E': 61, '#227B84': 59, '#3A757A': 60, '#803342': 16, '#7D3745': 6, '#3A727B': 7374, '#3B7479': 3, '#36747C': 11, '#6C4450': 104, '#82303F': 18, '#852B3B': 28, '#694A56': 3, '#3D7179': 15, '#694E59': 15, '#7D3545': 11, '#387283': 30, '#3B717B': 17, '#3A727D': 16, '#7B5A48': 37, '#832B43': 11, '#3B7184': 21, '#2A7C66': 1, '#5D5D4E': 2, '#3B7180': 23, '#41715A': 6, '#45714D': 44, '#297D59': 6, '#407256': 32, '#417160': 13, '#437155': 5275, '#467055': 16, '#327A58': 7, '#68514E': 4, '#407756': 2, '#3C7356': 22, '#56654F': 17, '#437154': 15, '#387457': 30, '#3F7169': 14, '#4B6D54': 9, '#805C49': 105, '#735E4A': 10, '#7F5747': 63, '#755C49': 9, '#457154': 16, '#337558': 45, '#536B52': 18, '#735944': 95, '#7B614F': 96, '#5D6750': 36, '#437156': 43, '#69624D': 21, '#457151': 29, '#3D7172': 10, '#70604B': 10, '#487458': 2, '#45744D': 96, '#447352': 2, '#23596C': 2, '#3C6A7E': 59, '#3F696B': 41, '#64819B': 37, '#204D73': 92, '#3C5E82': 60, '#3A5E8A': 93, '#385B92': 1, '#3C6182': 4212, '#5D7F9A': 1, '#0C4A72': 2, '#305E82': 118, '#5C6982': 118, '#8F8D9B': 26, '#646473': 3, '#7B7482': 118, '#5A7169': 14, '#39714D': 12, '#727182': 2691, '#797189': 13, '#3E724D': 1, '#3B7155': 51, '#885947': 1, '#7D5744': 1, '#866251': 1, '#4F7056': 51, '#675C48': 106, '#707289': 10, '#736E6C': 11, '#746B51': 12, '#756C58': 116, '#82705C': 27, '#135941': 27, '#235D44': 105, '#255B44': 24, '#1D5943': 45, '#2B5C46': 108, '#2B5C45': 8, '#746C58': 5469, '#2E5C46': 17561, '#7E705B': 32, '#4F634D': 10, '#7B6E5A': 32, '#45614B': 14, '#707584': 117, '#6E788C': 1, '#72716E': 1, '#75677E': 117, '#746684': 1, '#766D59': 26, '#3D5F49': 11, '#255943': 33, '#957890': 39, '#7A5174': 117, '#7C4B7C': 2, '#775E6A': 62, '#727152': 39, '#726C58': 32, '#365E47': 12, '#683F63': 37, '#7A5476': 4212, '#79507A': 37, '#766166': 38, '#7A6D57': 15, '#6E6B56': 13, '#2D5D46': 5, '#696A54': 4, '#2C5B45': 8, '#626852': 8, '#305C46': 24, '#2E5C44': 26, '#7E577B': 2, '#7C567A': 55, '#7A517A': 58, '#784F79': 1, '#5F3855': 1, '#724F68': 57, '#727053': 59, '#856C77': 89, '#51303E': 91, '#62444F': 56, '#60404E': 1, '#767558': 56, '#654654': 7521, '#623F53': 16, '#674B54': 7, '#747057': 25, '#746B58': 40, '#623E53': 15, '#654754': 40, '#757158': 11, '#6F6C56': 2, '#644554': 29, '#613D53': 16, '#6B5555': 15, '#6F5E56': 15, '#756D57': 11, '#634354': 7, '#634153': 13, '#716457': 7, '#644254': 7, '#654354': 4, '#305C48': 3, '#726C59': 2, '#7E7055': 6, '#817155': 7, '#48615F': 4, '#0A5649': 1, '#2E5C3E': 26, '#135669': 2, '#2C5B68': 34, '#2B5C53': 21, '#2E5C41': 58, '#415F60': 3, '#0F5667': 5, '#2C5B64': 4676, '#2C5B66': 19, '#2C5B5B': 17, '#2E5C4D': 8, '#175966': 7, '#375D61': 2, '#61675B': 1, '#2F5B64': 20, '#2C5B60': 16, '#2F5B4A': 3, '#55675E': 2, '#2E5C4A': 8, '#275C64': 23, '#674654': 10, '#385260': 1, '#684553': 26, '#1C5E66': 46, '#564D59': 5, '#3D5660': 8, '#4F4F5B': 10, '#5E4A57': 7, '#365961': 5, '#47525D': 8, '#5C4B57': 4, '#614756': 2, '#5A4759': 36, '#504A60': 10, '#404B67': 7, '#2C5667': 18, '#8B6B75': 1, '#2B4D71': 876, '#2D5D62': 18, '#7C6D7B': 1, '#58728D': 16, '#0A365F': 16, '#21553E': 4, '#335F4B': 1, '#35624D': 20, '#3D6752': 4}
I don't understand anything. How is it possible that no one pixel has the color that I've got in Photoshop?
Edit II:
With the same code, I have got the color map of another image. This is the image:
The predominant colors that you can see in this image are these:
"#F50A22", "#00EC83", "#00A200", "#0007A4", "#9D132B", "#734500", "#6230FF", "#F42AFF", "#BEFF00", "#EC7800", "#65DCD1", "#FF6D00" : "#004500"
Executing the test, how I said, the same code. I've got that all these colors are found it in the image among others! And no one of them how in the first image.
The results are:
Colors matched: {'#F50A22': 2245, '#00EC83': 9437, '#00A200': 21039, '#0007A4': 8772, '#9D132B': 99, '#734500': 2970, '#6230FF': 112, '#F42AFF': 5271, '#BEFF00': 2380, '#EC7800': 3076, '#65DCD1': 6503, '#FF6D00': 4709, '#004500': 6612}
colors matched: 13
And other colors found it in the image are:
Other colors: {'#FFFFFF': 1931, '#FCFFFD': 27, '#FAFFFB': 2, '#F7FEF9': 12, '#F4FEF7': 10, '#F6FEF8': 20, '#F6FDF8': 1, '#F9FEFA': 12, '#FBFEFC': 9, '#FEFFFE': 40, '#FAFEFB': 12, '#FBFFFC': 7, '#F3FEF6': 7, '#F4FDF6': 2, '#F5FDF7': 1, '#F2FDF5': 3, '#EEFDF2': 3, '#F2FDF6': 7, '#F4FEF8': 12, '#EFFDF4': 3, '#E5FCEC': 4, '#DAFAE5': 1, '#D3FAE0': 3, '#D4FAE0': 1, '#DAFAE4': 1, '#DFFBE8': 1, '#E9FCEF': 3, '#EDFDF2': 2, '#EFFDF3': 3, '#E2FBEA': 3, '#E2FCEA': 3, '#EFFEF3': 1, '#F2FEF5': 1, '#EDFCF1': 2, '#EBFDF0': 1, '#F1FDF4': 1, '#F3FEF7': 4, '#EDFDF1': 2, '#E7FCEE': 3, '#E3FCEB': 1, '#E0FCE9': 1, '#DCFBE6': 5, '#DAFBE5': 1, '#D9FAE4': 1, '#D9FAE3': 1, '#E3FCEC': 1, '#EEFDF3': 1, '#D7FAE2': 1, '#D1FADF': 1, '#D1FADE': 1, '#D6FAE2': 1, '#E1FBEA': 2, '#EBFDF1': 1, '#DFFBE9': 1, '#DEFBE7': 2, '#DBFBE5': 1, '#F6132A': 111, '#00EC84': 33, '#00EC85': 16, '#04EC86': 11, '#14EC87': 3, '#F40D23': 3, '#F20E24': 1, '#F50B22': 8, '#F11426': 2, '#F40C23': 1, '#EF1A28': 1, '#EE1B29': 1, '#F01827': 1, '#F21125': 1, '#F40D24': 1, '#F40E24': 1, '#774A03': 165, '#F40E23': 1, '#F50C22': 1, '#F6142A': 3, '#00EC82': 1, '#00EB82': 2, '#00EA7F': 1, '#00EB81': 1, '#6FE09C': 1, '#7E5416': 2, '#00A300': 78, '#00A500': 43, '#D9403B': 1, '#00AB16': 1, '#00A600': 40, '#00A700': 1123, '#5E2AFF': 2471, '#00B213': 2, '#00AA00': 6, '#7A4F0D': 3, '#6636FF': 2, '#00AE02': 2, '#00AC00': 3, '#00AB08': 2, '#00A800': 12, '#00A900': 8, '#00B317': 1, '#6C3CFF': 1, '#00AE00': 2, '#00AE14': 1, '#00A903': 1, '#7F55FE': 1, '#6CEE9F': 1, '#00AD00': 2, '#6CDCD2': 268, '#6A3CFE': 2, '#7549FF': 1, '#4ED688': 1, '#6B3DFF': 1, '#5E2BFF': 24, '#6839FD': 1, '#6231FE': 1, '#5E31FC': 2, '#00AF08': 1, '#00AC07': 1, '#6339FA': 1, '#5F33FB': 3, '#5F30FD': 3, '#00B10E': 1, '#656565': 1, '#00AB00': 2, '#00B02D': 2, '#6037F9': 1, '#5F2EFE': 2, '#5F3EF5': 1, '#5F32FC': 1, '#6040F4': 1, '#5F32FB': 2, '#6041F3': 1, '#6042F2': 1, '#7145FC': 1, '#5F2CFF': 10, '#6147EF': 1, '#6454EA': 1, '#6036F9': 1, '#685AEA': 1, '#00AF2F': 1, '#6B57EE': 1, '#00B110': 1, '#00AA02': 1, '#8ADBD3': 3, '#683CFB': 1, '#72DDD2': 3, '#6D47F8': 1, '#775EF3': 1, '#9CD7D1': 2, '#5E31FD': 1, '#00AB18': 1, '#82DCD3': 1, '#673EFB': 1, '#7450F9': 1, '#612EFF': 8, '#6236FB': 1, '#602CFF': 5, '#6B49F7': 1, '#602DFF': 7, '#5F2BFF': 6, '#6334FD': 1, '#2EEB8B': 1, '#704AFB': 1, '#6231FF': 1, '#6738FE': 1, '#612DFF': 3, '#3FEB8F': 1, '#66DBD1': 5, '#67D8D2': 1, '#00AE2B': 1, '#65DAD2': 1, '#F42DFF': 15, '#FC67FF': 6, '#F246FA': 1, '#F84CFF': 7, '#6233FF': 1, '#6ADCD2': 22, '#6132FE': 1, '#FBFEFE': 2, '#F434FF': 5, '#F8FDFC': 1, '#68DCD1': 33, '#6034FE': 1, '#FB5DFF': 2, '#FAFEFD': 2, '#F2FBFA': 1, '#6442FA': 1, '#6031FF': 1, '#F539FF': 7, '#F5FCFC': 1, '#E7F9F6': 1, '#F02AFF': 5, '#EFFBF9': 2, '#DDF6F3': 1, '#5F2EFF': 1, '#DD2BFF': 1, '#E82AFF': 1, '#F32AFF': 8, '#F744FF': 3, '#E7F9F7': 1, '#CFF2EF': 1, '#6136FD': 1, '#5F2AFF': 1, '#DD2AFF': 1, '#E42AFF': 1, '#EC2AFF': 2, '#E1F7F4': 1, '#C3EFEA': 1, '#6031FE': 1, '#EA2AFF': 2, '#ED2AFF': 1, '#DAF6F2': 1, '#BAEEE7': 1, '#6DDDD3': 2, '#6937FF': 1, '#ED37FE': 1, '#D7F5F1': 2, '#B6EDE6': 1, '#69DDD2': 2, '#74DFD4': 1, '#81DED9': 1, '#EF2BFF': 1, '#B3ECE7': 1, '#7ED7D4': 1, '#F22AFF': 2, '#D9F5F2': 1, '#B7EDE7': 1, '#DB39FC': 1, '#F12EFF': 1, '#E0F7F4': 1, '#C2EFEA': 1, '#87DBD3': 1, '#E737FE': 1, '#E6F8F6': 2, '#CCF2EE': 1, '#84DCD3': 1, '#ECFAF9': 1, '#D8F5F2': 1, '#65DCD0': 5, '#69DBCF': 6, '#6ADCD1': 1, '#98D3CD': 1, '#F440FC': 1, '#F42CFF': 7, '#F4FCFB': 1, '#6FD9CC': 2, '#6FD9CB': 3, '#6BDBCF': 1, '#7ED7C8': 1, '#80D3C1': 1, '#F531FF': 3, '#F42BFF': 35, '#FDFEFE': 2, '#F8FDFD': 1, '#83D8CB': 1, '#7ED3C2': 1, '#FF7100': 78, '#FEFFFF': 1, '#97D2CC': 1, '#FF7000': 40, '#FF6E00': 40, '#FF7925': 1, '#F33FF7': 1, '#6FDDD2': 2, '#FF6B00': 8, '#F62DF4': 1, '#F52BFB': 1, '#FF7409': 1, '#F62DF3': 1, '#F52BFC': 3, '#A2CFCA': 1, '#F73FE3': 1, '#F52DF9': 1, '#F42AFE': 1, '#FF7400': 4, '#FF730E': 1, '#FC36D5': 1, '#F62DF1': 1, '#F52BFD': 1, '#F52CFF': 6, '#F52DFF': 13, '#76DDD3': 2, '#FF6C00': 8, '#F831EA': 1, '#F52BFA': 3, '#F632FF': 1, '#8DDAD2': 1, '#F836E6': 1, '#F52BF9': 2, '#A4CCC8': 1, '#FF6A08': 1, '#7ADDD3': 1, '#FF690B': 1, '#F42BFE': 1, '#92D9D2': 2, '#FF6E0B': 1, '#F031FA': 1, '#A7C8C5': 1, '#FF6429': 1, '#FF7200': 62, '#FF671A': 1, '#7EDCD3': 1, '#EC35F6': 1, '#6CDACE': 1, '#6DDBD0': 1, '#FF671C': 1, '#FF7104': 1, '#FF6911': 1, '#FF642C': 1, '#FF6B23': 1, '#FF6E13': 1, '#FF7300': 5, '#F530FF': 1, '#F532FF': 3, '#6DDDD2': 1, '#F533FF': 1, '#F635FF': 1, '#F537FF': 8, '#F539FE': 2, '#F538FF': 9, '#00AC1A': 4, '#FF780E': 1, '#004B04': 29, '#FF873C': 1, '#FF7C1B': 1, '#FF7606': 4, '#FF780C': 1, '#FF7502': 1, '#FF7504': 1, '#FF770A': 2, '#004A03': 9, '#004A02': 5, '#F73FFF': 2, '#F435FF': 1, '#004700': 9, '#FF7A0F': 1, '#F52EFF': 1, '#F63BFF': 1, '#F638FF': 1, '#004600': 22, '#004B03': 3, '#004901': 8, '#FF7D1D': 1, '#F43EFB': 1, '#FF8533': 1, '#F62DF6': 1, '#FF7F24': 1, '#004902': 3, '#004900': 1, '#F441FC': 1, '#C1E057': 1, '#C2FD00': 5, '#C1F700': 1, '#C0FE00': 176, '#C4EE30': 2, '#C3E846': 1, '#C2FB00': 2, '#FEFEFE': 9, '#004C07': 11, '#B8FB00': 2, '#C3FB00': 1, '#FDFEFD': 5, '#BAFB00': 2, '#C5F11A': 2, '#B3F600': 2, '#BEFC00': 1, '#C1FD00': 7, '#FBFCFB': 3, '#BCDE52': 1, '#BBFE00': 9, '#FAFBFA': 2, '#B6F700': 1, '#BDFB00': 1, '#C3F800': 5, '#F331FF': 1, '#B2F500': 1, '#BDF900': 1, '#BDFD00': 1, '#BBFC00': 2, '#BDFE00': 10, '#C3EA40': 1, '#FCFDFC': 4, '#B5F600': 2, '#BCFD00': 7, '#C4E847': 1, '#CDFD09': 3, '#2337B3': 1, '#4251B6': 1, '#C5ED37': 1, '#D5FF3E': 17, '#0012A7': 625, '#004B06': 1, '#CFFE22': 5, '#B6F900': 2, '#C5FD00': 5, '#D3FF3C': 3, '#005010': 1, '#CBFD00': 5, '#C2FE00': 3, '#B8F900': 2, '#D2FE31': 8, '#C8FD00': 3, '#B9FA00': 2, '#C4FD00': 3, '#F8FBF9': 2, '#CCFE08': 3, '#F4F6F4': 2, '#C7FD00': 5, '#EBF1EC': 1, '#F8F9F7': 1, '#E1EAE4': 1, '#004701': 1, '#132AAF': 2, '#D5E2D8': 1, '#F1F5F2': 2, '#D1DFD5': 1, '#EC8417': 1, '#D4E1D7': 1, '#F3F5F3': 1, '#D9E4DC': 1, '#EB7800': 15, '#ED7B00': 133, '#F6F8F5': 1, '#DEE8E0': 1, '#E6EDE6': 1, '#FAFDFB': 1, '#EAF0EB': 1, '#EEF3EF': 1, '#EA7700': 2, '#F5F7F5': 1, '#C2E64E': 1, '#CAFD00': 2, '#F7FAF8': 1, '#E87700': 2, '#EA7800': 7, '#004C06': 2, '#CFFE20': 2, '#004A05': 1, '#E37600': 1, '#E67700': 1, '#00591D': 1, '#990A22': 2077, '#A6293C': 1, '#021EAA': 1, '#0007A3': 6, '#0009A1': 2, '#001697': 2, '#000B9F': 2, '#00119B': 1})
total other colors: 448
Both images are png.
How is it possible that I found all the colors among others in the second image and not found anyone of the color searched in the first image?
you can see 13 colors yes! but the code doesn't because it's more precise than your eyes.
try zooming into the picture more, you'll see that between the colors there is another lighter one, which can consist of more than one color to go from one to the other, also I noticed some black and white at the left side "maybe it's just from your snipping tool or something"
but what I'm saying is, the code is right :)
you can try and create a photo using paint and only two colors with the fill tool, and make sure it's only one color without any gradient.
I found the problem and the solution. The problem is that I'm using images which has been created from a previous export. I mean, I have resized and make an export from an original imagin and in this momento something happens in Photoshop or whatever other program which produce an image with many other colors and not the original colors.
So, you have to run the process over the original version of the image, the export from the vectorized image. If you make an export from this export and then run the process, you will have problems like me.
I have a numpy array of milliseconds in integers, which I want to convert to an array of Python datetimes via a timedelta operation.
The following MWE works, but I'm convinced there is a more elegant approach or with better performence than multiplication by 1 ms.
start = pd.Timestamp('2016-01-02 03:04:56.789101').to_pydatetime()
dt = np.array([ 19, 14980, 19620, 54964615, 54964655, 86433958])
time_arr = start + dt * timedelta(milliseconds=1)
So your approach produces:
In [56]: start = pd.Timestamp('2016-01-02 03:04:56.789101').to_pydatetime()
In [57]: start
Out[57]: datetime.datetime(2016, 1, 2, 3, 4, 56, 789101)
In [58]: dt = np.array([ 19, 14980, 19620, 54964615, 54964655, 86433958])
In [59]: time_arr = start + dt * timedelta(milliseconds=1)
In [60]: time_arr
Out[60]:
array([datetime.datetime(2016, 1, 2, 3, 4, 56, 808101),
datetime.datetime(2016, 1, 2, 3, 5, 11, 769101),
datetime.datetime(2016, 1, 2, 3, 5, 16, 409101),
datetime.datetime(2016, 1, 2, 18, 21, 1, 404101),
datetime.datetime(2016, 1, 2, 18, 21, 1, 444101),
datetime.datetime(2016, 1, 3, 3, 5, 30, 747101)], dtype=object)
The equivalent using np.datetime64 types:
In [61]: dt.astype('timedelta64[ms]')
Out[61]: array([ 19, 14980, 19620, 54964615, 54964655, 86433958], dtype='timedelta64[ms]')
In [62]: np.datetime64(start)
Out[62]: numpy.datetime64('2016-01-02T03:04:56.789101')
In [63]: np.datetime64(start) + dt.astype('timedelta64[ms]')
Out[63]:
array(['2016-01-02T03:04:56.808101', '2016-01-02T03:05:11.769101',
'2016-01-02T03:05:16.409101', '2016-01-02T18:21:01.404101',
'2016-01-02T18:21:01.444101', '2016-01-03T03:05:30.747101'], dtype='datetime64[us]')
I can produce the same array from your time_arr with np.array(time_arr, dtype='datetime64[us]').
tolist converts these datetime64 items to datetime objects:
In [97]: t1=np.datetime64(start) + dt.astype('timedelta64[ms]')
In [98]: t1.tolist()
Out[98]:
[datetime.datetime(2016, 1, 2, 3, 4, 56, 808101),
datetime.datetime(2016, 1, 2, 3, 5, 11, 769101),
datetime.datetime(2016, 1, 2, 3, 5, 16, 409101),
datetime.datetime(2016, 1, 2, 18, 21, 1, 404101),
datetime.datetime(2016, 1, 2, 18, 21, 1, 444101),
datetime.datetime(2016, 1, 3, 3, 5, 30, 747101)]
or wrap it back in an array to get your time_arr:
In [99]: np.array(t1.tolist())
Out[99]:
array([datetime.datetime(2016, 1, 2, 3, 4, 56, 808101),
...
datetime.datetime(2016, 1, 3, 3, 5, 30, 747101)], dtype=object)
Just for the calculation datatime64 is faster, but with the conversions, it may not be the fastest overall.
https://docs.scipy.org/doc/numpy/reference/arrays.datetime.html