I'm working in goal/prolog (prolog swi 6.0.0) and have a situation where I would like to do the following: I have the following things in my belief base:
elevator([agent0,0,[]])
elevator([agent1,0,[]])
elevator([agent2,0,[]])
then execute the following query:
if bel(bagof(Elevator,elevator(Elevator),Elevators)) then insert (elevatorList(Elevators)).
However rather then the expected result of Elevators being
[[agent1,0,[]],[agent2,0,[]],[agent3,0,[]]]
the resulting list is
[Elevator/['.'(Agent0, '.'(0, '.'([], [])))]] [Elevator/['.'(Agent0, '.'(0, '.'([], []))),'.'(bidAgent, '.'(0, '.'([], [])))]] [Elevator/['.'(Agent0, '.'(0, '.'([], []))),'.'(bidAgent, '.'(0, '.'([], []))),'.'(agent1, '.'(0, '.'([], [])))]]
Any idea what I'm doing wrong here?
Related
I have written this code
import scrapy
class YellowPages(scrapy.Spider):
name = 'yp'
start_urls = [
"https://www.yellowpages.com/search?search_terms=agent&geo_location_terms=Los%20Angeles%2C%20CA&page=1",
]
def parse(self, response):
agent_name = response.xpath("//a[#class='business-name']/span/text()").extract()
phone_number = response.xpath("//div[#class='phones phone primary']/text()").extract()
address = response.xpath("//div[#class='street-address']/text()").extract()
locality = response.xpath("//div[#class='locality']/text()").extract()
data = zip(agent_name, phone_number, address, locality)
for item in data:
info = {
#'page' : response.url,
'Agent name': item[0],
'Phone number': item[1],
'Address': item[2],
'Locality':item[3],
}
yield info
next_page_href = response.xpath('//a[#class= "next ajax-page"]/#href').extract()[0]
next_page = "https://www.yellowpages.com"+next_page_href
if next_page is not None:
yield scrapy.Request(response.urljoin(next_page), callback=self.parse)
But now I want to have ratings added to my CSV file. bt the rating number is written in word.
like this.
<div class="result-rating three ">
On the webpage this rating is shown by stars and the number of the total stars is written in word in the code.
I want to get that rating in number. Anyone know how will I able extract the words into numbers??
Assuming the rating is from one to five, you can maintain an array of these words (one to five) and detect them in the string.
Something like this:
word_number_mapping = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}
rating_value = None
rating_text = response.css('.result-rating::attr(class)').extract()
if rating_text:
for k, v in word_number_mapping.items():
if k in rating_text:
rating_value = v + 0.5 if 'half' in rating_text else v
Hope it helps.
I'm trying to dynamically create nested map like below in code.
def people = [
[name: 'Ash', age: '21', gender: 'm'],
[name: 'Jo', age: '22', gender: 'f'],
[name: 'etc.', age: '42', gender: 'f']
]
So I can search it like below
person = people.findAll {item ->
item.gender == 'm' &&
item.age == '21'}
My problem is that whilst I can dynamically create one dimensional maps in code, I don't know how to dynamically combine maps in code to create nested map e.g. let's assume in code I have created two maps name1 and name2. How do I add them to people map so they are nested like above example?
def people = [:]
def name1 = [name:'ash', age:'21', gender:'m']
def name2 = [name:'Jo', age:'22', gender:'f']
I've searched / tried so many posts without success. Below is close, but does not work :(
people.put((),(name1))
people.put((),(name2))
In your example, people is a list of maps, not a nested map
So you can simply do:
def people = []
def name1 = [name:'ash', age:'21', gender:'m']
def name2 = [name:'Jo', age:'22', gender:'f']
Then:
people += name1
people += name2
Or define it in one line:
def people = [name1, name2]
I have a list of state names:
stateNames = ['Alabama', 'Georgia', 'Florida']
And I have a dictionary that has a list of codes for each state name. *Not all states have codes. And I don't need the codes for all states, just the ones from the list:
masterdict = {'Alaska': [1,2,3], 'Alabama': [4, 5, 6], 'Arkansas': [7,8,9], 'Arizona': [], 'California': [], 'Colorado': [], 'Connecticut': [], 'DistrictOfColumbia': [], 'Delaware': [], 'Florida': [21, 48], 'Georgia': ['1,3,2,4,5'], 'Wyoming': []}
I want to look through my list and get the codes individually for each state in that list. I'm still a little off on the logic. One is a list(item in list) and one is a dictionary with keys ('state name') and values (list of codes). What am I doing incorrectly here:
for item in stateNames:
for k in masterdict.item():
if item == masterdict[k]:
print(masterdict[v])
In your first loop, you are getting all of the keys. So you don't need to do another loop. Just grab the value by the key.
for item in stateNames:
print(masterdict[item])
declare variable $fb := doc("factbook.xml")/mondial;
for $c in $fb//country
where ($c/encompassed/#continent = 'f0_119') and ($c/#population < 100000)
return concat('Country: ',$c/name, ', Population: ',$c/#population);
it returns:
Type Error: Type of value '
()
' does not match sequence type: xs:anyAtomicType?
At characters 11681-11698
At File "q2_3.xq", line 4, characters 13-67
At File "q2_3.xq", line 4, characters 13-67
At File "q2_3.xq", line 4, characters 13-67
however, if i do not do a concat return, just name or population it will work, and most strange thing is i have another program :
declare variable $fb := doc("factbook.xml")/mondial;
for $c in $fb//country
where $c/religions = 'Seventh-Day Adventist'
order by $c/name
return concat('Country: ',$c/name, ', Population: ',$c/#population);
The return syntax is exactly same, however, it works.
Why this happens?
Without seeing an example of your data it's impossible to say for sure, but if $c/name returns more than one value, then your error would make sense. Do you have any results where there are more than one name element?
I think it is pretty straightforward. All I am trying to do is update the original dictionary's 'code' with that of another dictionary which has the value. I get a feeling 2 for loops and an IF loop can be further shortened to get the answer. In my actual problem, I have few 1000's of dicts that I have to update. Thanks guys!
Python:
referencedict = {'A': 'abc', 'B': 'xyz'}
mylistofdict = [{'name': 'John', 'code': 'A', 'age': 28}, {'name': 'Mary', 'code': 'B', 'age': 32}, {'name': 'Joe', 'code': 'A', 'age': 43}]
for eachdict in mylistofdict:
for key, value in eachdict.items():
if key == 'code':
eachdict[key] = referencedict[value]
print mylistofdict
Output:
[{'age': 28, 'code': 'abc', 'name': 'John'}, {'age': 32, 'code': 'xyz', 'name': 'Mary'}, {'age': 43, 'code': 'abc', 'name': 'Joe'}]
There is no need to loop over all values of eachdict, just look up code directly:
for eachdict in mylistofdict:
if 'code' not in eachdict:
continue
eachdict['code'] = referencedict[eachdict['code']]
You can probably omit the test for code being present, your example list always contains a code entry, but I thought it better to be safe. Looking up the code in the referencedict structure assumes that all possible codes are available.
I used if 'code' not in eachdict: continue here; the opposite is just as valid (if 'code' in eachdict), but this way you can more easily remove the line if you do not need it, and you save yourself an indent level.
referencedict = {'A': 'abc', 'B': 'xyz'}
mylistofdict = [{'name': 'John', 'code': 'A', 'age': 28}, {'name': 'Mary', 'code': 'B', 'age': 32}, {'name': 'Joe', 'code': 'A', 'age': 43}]
for x in mylistofdict:
try:
x['code']=referencedict.get(x['code'])
except KeyError:
pass
print(mylistofdict)