{
id: "1698627066",
screen_name: "RomanceInfinity",
text: [
"Going NYP to have lunch with bro because I got too much time in between!!!",
"nyp"
],
stance: "",
source: "Twitter for iPhone",
fromid: "411377814521147392",
favourite: "false",
date: "2013-12-13 14:11:28",
replyto: "",
replytoid: "",
retweetfrom: "",
domaintype: "",
keywords: [
"nyp"
],
ratio: "",
latitude: 1000,
longitude: 1000,
retweet: 0,
mood_joy: "0.0",
mood_sadness: "0.0",
mood_surprised: "0.0",
mood_disgusted: "0.0",
mood_anger: "1.0",
_version_: 1454285708574326800
},
{
Is there a way that I count each word in the sentence using facet " Going NYP to have lunch with bro because I got too much time in between!!"?
For example having a results of Going =1 Nyp =1 lunch =1 and also not counting the punctuation?
Extract the text from the dict using:
name_of_dict['text'][0]
This will extract the sentence:
Going NYP to have lunch with bro because I got too much time in between!!!
To ignore any punctuation you can use a function such as .replace() via:
str = "Going NYP to have lunch with bro because I got too much time in between!!!";
print str.replace("!", "");
>>> Going NYP to have lunch with bro because I got too much time in between
Last, I refer you to this post for counting occurance in a string:
Efficiently calculate word frequency in a string
To which a viable solution is:
from collections import Counter
test = 'abc def abc def zzz zzz'
Counter(test.split()).most_common()
[('abc', 2), ('zzz', 2), ('def', 2)]
Related
I am trying to remove the : from the specific field value from the json output.
Please find the json output:
{
"suite_id": 111,
"object_type": 2,
"jobs": [
{
"jobId": 222,
"creationTime": "2022-04-12T13:38:02Z",
"browser": "chrome",
"browserVersion": "",
"platform": "win",
"platform_version": "",
"appiumVersion": "",
"deviceName": "",
"deviceOrientation": "",
"isSerial": false,
"totalExecutions": 1,
"isRetryExecAvailable": false,
"jobExecutionStatus": 1,
"etrs": {
"tasks": [
{
"executionId": 333,
"executionName": "Testing",
"executionStatus": "SUCCESS",
"restartExecutionStatus": "",
"restartExecutionId": 0,
"restartExecutionReportUrl": "",
"restartExecutionVideoUrl": "",
"initiatedOn": "2022-04-12T13:38:05Z",
"projectName": "Testproject",
"projectId": 1,
"executionVideoUrl": "",
"reportUrl": "avsfershd",
"testcaseId": 666,
"testCaseName": "testcase",
"errorsCount": 0,
"warningsCount": 0,
"statusMessage": "Success"
}
],
"total_execs": 0
},
"environmentType": "local"
}
]
}
I am taking the initiatedOn value from the json output by
$value = $Output.jobs.etrs.tasks.initiatedOn
which giving me value 2022-04-12T13:38:05Z but I need to remove : from the value.
Thank you,
Shivam
Continuing from my comment,
I would cast this universal sortable date formatted string into a [datetime] object, which you can format as you like instead of cutting off part of the string..
$value = '{0:yyyy-MM-dd}' -f [datetime]$Output.jobs.etrs.tasks.initiatedOn
Your example ends with the Z for Zulu Time (UTC + 00:00), but another value may look like 2022-04-12T13:38:05+08:00, so converting it to datetime would IMO be best.
I'm trying to insert data to a collection I created in Atlas MongoDB. The data is following:
[
{ id: 1, performer: 'John Doe', genre: 'Rock', price: 25, day: 1, image: '/img/uploads/1fsd324fsdg.jpg' },
{ id: 2, performer: 'Rebekah Parker', genre: 'R&B', price: 25, day: 1, image: '/img/uploads/2f342s4fsdg.jpg' },
{ id: 3, performer: 'Maybell Haley', genre: 'Pop', price: 40, day: 1, image: '/img/uploads/hdfh42sd213.jpg' }
]
`I get the error : "Insert not permitted while document contains errors."
What am I doing wrong? Please advise.
may be quote problem ...
use double quote to key and property
"id" : 1, "performer : "John Doe" .. ~
This is reslved now, formatting was the problem.
I do the following:
Modify title to "title" and mongo compass works.
Example:
[
{ "id": "1", "performer": "John Doe", "genre": "Rock", "price": "25", "day": "1", "image": "/img/uploads/1fsd324fsdg.jpg" },
]
Reference document: https://docs.mongodb.com/compass/current/documents/insert
It's better that you use MongoDB Compass and connect to it with a connection string:
click on the connection
click on the connection using mongodb compass
then get the compass downloaded from MongoDB according to required OS
but use connection string of connection to MongoDB application
Once you connect to your Compass, you can use import data and then browse your file to use.
Let's say we have objects like this in a mongodb collection:
{
_id: 00000001
colors: ["green", "yellow"],
houses: [
{
number: 1,
owner: "John"
},
{
number: 2,
owner: "John"
},
{
number:3,
owner: "Dave"
}
]
},
{
_id: 00000002
colors: ["green", "red"],
houses: [
{
number: 15,
owner: "Dave"
},
]
}
So, to get every object where the color array contains the color green the query I would need to write would look smth like this: collection.find({colors: "green"});
Now if I would like to get all the objects in which John owns a house, how would I formulate such a query?
Basically what I am asking is, if my query would be collection.find({houses: {owner: "John", number: ?}}) what would I need to replace the "?" with to tell mongo that I don't care what value number has.
Or maybe there is another approach that I haven't thought of?
Thank you for any help!
(Btw this is a made up example hence why the IDs look weird and the object in itself doesn't seem very useful.)
To query an array of objects you can use the dot notation, try:
db.collection.find({ "houses.owner": "John"}})
I have the following structure:
{
id: "1",
invoices: [{ id: "1", balance: 1},{ id: "2", balance: 1}]
},
{
id: "2",
invoices: [{ id: "3", balance: 1},{ id: "4", balance: 1}]
}
I'm getting a list of invoices IDs that i shouldn't update, the rest i need to update the balance to 0.
I'm pretty new to MongoDB and managing to find a way to do it.
Let say you want to update all invoices of id 1 except invoice.id 2 try this one:
db.collection.update(
{ id: "1", "invoices.id": {$ne: 2} },
{
$set: {
"invoices.$[]": { balance: 0 }
}
}
)
First of all, you forgot the quotes around the field names. Your documents should be like this:
{
"id": "1",
"invoices": [{
"id": "1",
"balance": 1
}, {
"id": "2",
"balance": 1
}]
}
I have limited experience with MongoDB, as I learnt it this semester at University. However, here is my solution:
db.collection.update(
{ id: "1" },
{
$set: {
"invoices.0": { id: "1", balance: 0 }
}
}
)
What does this solution do?
It takes the document with id 1. That is your first document.
The $set operator replaces the value of a field with the specified value. (straight out from the MongoDB manual - MongoDB Manual - $set operator).
"invoices.0" takes the first invoice from the invoices array and then it updates the balance to 100.
Replace the word collection from db.collection with your collection name.
Try and see if it works. If not, I'd like someone with more experience to correct me.
LE: Now it works, try and see.
This question already has answers here:
Sort array of dictionary
(3 answers)
Closed 5 years ago.
I have to sort an array of dictinaries for my first project (online course). Sort has not been covered and I was left to do some research myself. Did go to Apple and read about closures, watched youtube to understand the concept of sorting but I am still a little confused on how to setup the logic for that (func I am assuming)
This is the collection I have so far
var players: [[String: Any]] = [
["Name": "Joe Smith",
"Height": 42,
"Experience": true,
"Guardians": "Jime and Jan Smith",
"Team": ""],
["Name": "Jill Tanner",
"Height": 36,
"Experience": true,
"Guardians": "Clara Tanner",
"Team": ""],
["Name": "Bill Bon",
"Height": 43,
"Experience": true,
"Guardians": "Sara and Jenny Bon",
"Team": ""],
]
Any tips or source would be great thanks!
I strongly suggest you convert your data into an Array of structs (or perhaps objects), rather than an Array of dictionaries. The weak typing necessary to use Dictionary (i.e. having Any as the type of the values) makes dictionaries really annoying to work with. Plus there's also the massive hit you take to performance and safety when working with Dictionaries.
struct Player {
let name: String
let height: Int
let experience: Bool
let guardians: String
let team: String?
}
let players = [
Player(
name: "Joe Smith",
height: 42,
experience: true,
guardians: "Jime and Jan Smith",
team: nil
),
Player(
name: "Jill Tanner",
height: 36,
experience: true,
guardians: "Clara Tanner",
team: nil
),
Player(
name: "Bill Bon",
height: 43,
experience: true,
guardians: "Sara and Jenny Bon",
team: nil
),
]
// Example, sort by height
let sortedPlayers = players.sorted{ $0.height < $1.height }
for player in sortedPlayers {
print(player)
}
I've provided the sorted method of Array a closure which tells it how to perform a comparison between any two given elements. In this example, I've written a closure that puts shorter players before taller players.