How do I match values through a list? In my case I have a list of colors -
["Black", "Lavender", "Brazen", "Army", "Birch"]
I have this function that creates a collection of the color name and a related URL:
(combine-color-imgurl get-product-images)
=>
[("Black"
"example.com/WWbNhro1n3YfmQTx1q5Piv1j8lZzse6M-25_2a353315-9965-457f-83f8-32af89f3b067.jpeg")
("Lavender"
"example.com/trWCTNYNOfiA8EBX9HZcxVB1Hg8pVtr9-25_403cbe3e-9894-4ad6-9f69-dedcfd7ef77b.jpeg")
("Brazen"
"example.com/COYrBHEsiOgdeOfxsQGcw8XXynsVdpxy-25_af0a9ff9-b988-467e-9b39-c4ae572420e8.jpeg")
("Army"
"example.com/YWKhJXFM0dKhJuRXphCXD4TWstnZRRJm-25_b3a2e43c-3c8c-4abc-83d2-d00d9b3ed594.jpeg")
("Birch"
"example.com/IRsHVjavdFsybvmpT6xjnpKqMtjyjeoZ-25_6321e4ef-5bed-463f-990a-1857151b8a11.jpeg")]
I'm not exactly sure how to get only the URL related to the color.
So the function would be something like
(defn get-url [color]
(some-operation (combine-color-imgurl get-product-images)))
=> URL
As mentioned by Juan, the solution was to have combine-color-imgurl return a map then use
(get (into {} (combine-color-imgurl get-product-images)) color)
to get the URL.
Related
firstly apologies: I am a beginner in Earth Engine, but googling my question hasn't yielded any results. I have a reasonable amount of experience with other languages/platforms.
I have a table of data with headers 'name' and 'value' with N entries, I also have a multiband images in which the bands are named the same as the 'name' column in my table.
I want to apply a functions to each band in the image, based on its corresponding value in the table.
I'm struggling to find a way to do this without using loops and getInfo(), both of which I understand are not efficient and generally frowned upon.
I think perhaps I'm missing something fundamental here regarding the interaction between local variables and things occuring serverside - help would be greatly appreciated!
You could perhaps iterate over the band names in the image:
var updatedImage = ee.Image(
image.bandNames().iterate(
function (bandName, acc) {
bandName = ee.String(bandName) // Must cast from ee.Element to actual type
var feature = features
.filter(ee.Filter.eq('name', bandName))
.first()
var value = feature.getNumber('value')
var bandWithFunctionApplied = image.select(bandName)
.add(value) // Apply some function
return ee.Image(acc)
.addBands(bandWithFunctionApplied)
},
ee.Image([])
)
)
https://code.earthengine.google.com/082411908a7525a4c8a87916b5ea88fc
I have a dictionary of phone numbers where number is Key and country is value. I want to update the key and add country code based on value country. I tried to use the map function for this:
print('**Exmaple: Update phone book to add Country code using map function** ')
user=[{'952-201-3787':'US'},{'952-201-5984':'US'},{'9871299':'BD'},{'01632 960513':'UK'}]
#A function that takes a dictionary as arg, not list. List is the outer part
def add_Country_Code(aDict):
for k,v in aDict.items():
if(v == 'US'):
aDict[( '1+'+k)]=aDict.pop(k)
if(v == 'UK'):
aDict[( '044+'+k)]=aDict.pop(k)
if (v == 'BD'):
aDict[('001+'+k)] =aDict.pop(k)
return aDict
new_user=list(map(add_Country_Code,user))
print(new_user)
This works partially when I run, output below :
[{'1+952-201-3787': 'US'}, {'1+1+1+952-201-5984': 'US'}, {'001+9871299': 'BD'}, {'044+01632 960513': 'UK'}]
Notice the 2nd US number has 2 additional 1s'. What is causing that?How to fix? Thanks a lot.
Issue
You are mutating a dict while iterating it. Don't do this. The Pythonic convention would be:
Make a new_dict = {}
While iterating the input a_dict, assign new items to new_dict.
Return the new_dict
IOW, create new things, rather than change old things - likely the source of your woes.
Some notes
Use lowercase with underscores when defining variable names (see PEP 8).
Lookup values rather than change the input dict, e.g. a_dict[k] vs. a_dict.pop(k)
Indent the correct number of spaces (see PEP 8)
I'm have a list with string types and i want to get each one that have maximum of occurence element grouped by another column. I'm trying to do this by linqu expression but it doesn't work. Is it possible to run my code that i show below ?
#test=(from a in #data
group a by new {a.PostCode}
into obj
select obj).ToDictionary(x => x.Key,x=>x.ToList()
.Select(y=>y.Statistic).GroupBy(s => s)
.OrderByDescending(s => s.Count())
.First().Key);
I have some relations between persons in my graph.
my data (generate script below)
create (s:Person {name: "SUE"})
create(d:Person {name: "DAVID"})
create(j:Person {name: "JACK"})
create(m:Person {name: "MARY"})
create(js:Person {name: "JASON"})
create(b:Person {name: "BOB"})
create(a1:Adress {id:1})
create(a2:Adress {id:2})
create(a3:Adress {id:3})
create(a4:Adress {id:4})
create(a5:Adress {id:5})
merge (d)-[:MOTHER]->(s)
merge(j)-[:MOTHER]->(s)
merge(js)-[:MOTHER]->(m)
merge(b)-[:MOTHER]->(m)
merge(b)-[:CURRENT_ADRESS]->(a1)
merge(js)-[:CURRENT_ADRESS]->(a2)
merge(j)-[:CURRENT_ADRESS]->(a3)
merge(s)-[:CURRENT_ADRESS]->(a4)
merge(d)-[:CURRENT_ADRESS]->(a5)
;
I can get mothers who live with her child:
MATCH (p:Person)-[:CURRENT_ADRESS]->(a:Adress)<-[:CURRENT_ADRESS]-(t), (t)-[:MOTHER]->(p)
return p.name,t.name
p.name t.name
MARY JASON
but i want to get mothers who is not living with any child of her.
How can i do that in Cyper?
Actually in your graph, everybody is living at a different address due to different identifiers.
Let's build a graph example introducing the sister which lives at the same address :
CREATE
(p:Person)-[:MOTHER]->(m:Person),
(p)-[:FATHER]->(f:Person),
(p)-[:SISTER]->(s:Person),
(p)-[:CURRENT_ADDRESS]->(a:Adress),
(m)-[:CURRENT_ADDRESS]->(b:Adress),
(f)-[:CURRENT_ADDRESS]->(c:Adress),
(s)-[:CURRENT_ADDRESS]->(a)
Now this is very simple, match family members that don't have a CURRENT_ADDRESS relationship in depth2 to the family member :
MATCH (p:Person)-[:MOTHER|:FATHER|:SISTER]->(familyMember)
WHERE NOT EXISTS((p)-[:CURRENT_ADDRESS*2]-(familyMember))
RETURN familyMember
Try this
MATCH (p:Person)-[:CURRENT_ADRESS]-(a:Adress), (p)-[:MOTHER|:FATHER]->(t)
WITH p,a,t
MATCH (p), (t) where not (t)-[:CURRENT_ADRESS]-(a)
return p.NAME,t.NAME
This should work:
MATCH (p:Person)-[:CURRENT_ADRESS]-(a:Adress), (p)-[:MOTHER|:FATHER]->(t)-[:CURRENT_ADRESS]-(b:Adress)
WHERE a <> b
return p.NAME, t.NAME;
By the way, I'd also put the appropriate direction arrow on the CURRENT_ADRESS relationships.
Finally i found it.
match path=(p:Person)-[:MOTHER]->(m:Person)-[:CURRENT_ADRESS]->(a:Adress)
where all(x in nodes(path) where not exists((p)-[:CURRENT_ADRESS]->(a)))
return path
I have the following Test collection where each document looks like:
firstName: "Jeff",
lastname: "Harper",
scores:[ {'period':'week one', 'score':90},
{'period':'week two', 'score':85},
{'period':'week three','score':92},
{'period':'week four', 'score':87}
I would like to iterate through the scores array and console.log the score. As a trial, I have tried:
Test.find()forEach(function(doc){ console.log( doc.firstName ) } );
This works fine to print out the first name. If I would want to print the first score in the array object, i.e., I try the statement:
Test.find()forEach(function(doc){ console.log( doc.scores[0].score ) } );
which doesn't work. How do I gain access to the elements in the array of objects?
Thanks everyone for your input. Christian Fritz identified my problem. Now, I limit my search to only documents that have the object array. Both the forEach method and the fetch() method work now. However, Ethaan, I had to include an inner for-loop inside the primary for-loop to gain access to each internal score. Thanks for your help and your editing and the picture of the beautiful asian princess.