How can I split confirmed value, death value and recovered value. I want to add them to different lists. I tried to isdigit method to find value in line. Also I tried split('":'). I thought I can define value after '":'. But these are not working.
https://api.covid19api.com/total/dayone/country/us
I added all line to textlist from this page.
I just edited question for other users. My problem solved thank you.
The list actually contains a string. You need to parse it and then iterate over it to access the required values from it.
import json
main_list = ['.....']
data_points = json.parse(main_list[0])
confirmed = []
for single_data_point in data_points:
confirmed.append(single_data_point.Confirmed)
print(confirmed)
A similar approach can be taken for any other values needed.
Edit:
On a better look at your source, it looks like the initial data is not in the right JSON format to begin with. Some issues I noticed:
Each object which has a Country value does not have its closing }. This is a bigger issue and needs to be resolved first.
The country object starting from the 2nd object has a ' before the object starting. This should not be the case as well.
I suggest you to look at how you are initially parsing/creating the list.
Since you gave the valid source of your data it becomes pretty simple:
import urllib.request
import json
data = json.load(urllib.request.urlopen("https://api.covid19api.com/total/dayone/country/turkey"))
confirmed=[]
deaths=[]
recovered=[]
for dataline in data:
confirmed.append(dataline["Confirmed"])
deaths.append(dataline["Deaths"])
recovered.append(dataline["Recovered"])
print ("Confirmed:",confirmed)
print ("Deaths:", deaths)
print ("Recovered:",recovered)
I have a list that should contain all my other lists. Currently I append every list separately but that just looks pretty ugly..
looplist = [] # initiate an empty list that contains all date from the other lists
[...]
looplist.append(internallist1)
[...]
looplist.append(internallist10)
the internallists are all getting initialized and filled in a for loop
You can simply use + to merge them.
You may check for more info.
If you want to have list of lists, check this topic.
listOne.extend(anotherList)
this could help you: https://docs.python.org/3/tutorial/datastructures.html
you can also do listOne+=anotherList and this is less expensive, as it doesn`t involve a function call like extend
To answer what you are asking, Just initialize looplist with your 10 lists.
looplist = [internallist1,
internallist2,
internallist3] #Note: internallist3,] is also valid, python allows trailing comma. Nifty!
However, your 10 lists really shouldn't be separately named lists in the first place if this is your real use case. Just use looplist[0] through looplist[9] instead from the get go.
The zip method could work for you as you stated your output should:
look something like [ [list1], [list2], ... , [list n] ]
in your case the code would be similar to
looplist = list(zip(internallist1,[...], internallist10))
I start by downloading some tweets from Twitter.
tweet_text = DonaldTrump["Tweets"]
tweet_text = tweet_text.str.lower()
Then in next step, we move with TweetTokenizer.
Tweet_tkn = TweetTokenizer()
tokens = [Tweet_tkn.tokenize(t) for t in tweet_text]
tokens[0:3]
Can someone explain to me and help me solve it.
I have been through similar questions that face similar errors but they provide different solutions.
Lists are mutable and can therefore not be used as dict keys. Otherwise, the program could add a list to a dictionary, change its value, and it is now unclear whether the value in the dictionary should be available under the new or the old list value, or neither.
If you want to use structured data as keys, you need to convert them to immutable types first, such as tuple or frozenset. For non-nested objects, you can simply use tuple(obj). For a simple list of lits, you can use this:
tuple(tuple(elem) for elem in obj)
But for an arbitrary structure, you will have to use recursion.
Here is the block to analyse:
('images\\Principales\\Screenshot_1.png', '{"categories":[{"name":"abstract_","score":0.00390625},{"name":"outdoor_","score":0.01171875},{"name":"outdoor_road","score":0.41796875}],"description":{"tags":["road","building","outdoor","scene","street","city","sitting","empty","light","view","driving","red","sign","intersection","green","large","riding","traffic","white","tall","blue","fire"],"captions":[{"text":"a view of a city street","confidence":0.83864323826716347}]},"requestId":"73fc14d5-653f-4a0a-a45a-e7a425580361","metadata":{"width":150,"height":153,"format":"Png"},"color":{"dominantColorForeground":"Grey","dominantColorBackground":"Grey","dominantColors":["Grey"],"accentColor":"274A68","isBWImg":false}}')
I need to extract all elements after "description", but i don't know how to do that... (in fact, i need this elements:
"road", "building","outdoor","scene","street","city","sitting","empty","light","view","driving","red","sign","intersection","green","large","riding","traffic","white","tall","blue","fire"
I've been looking for several minutes already, but I do not understand how to do it! I'm a little beginner in learning "lists" element, and I still have a hard time understanding.
The "For" loop returns only 'images\\Principales\\Screenshot_1.png', then the big blocks left ...
Did you have a solution?
Thanks in advence!
EDIT:
Indeed, it is actually JSON! Thanks to the people who helped me :)
To extract the desired elements contained in the second block, I simply proceeded thus:
import json
ElementSeparate= '{"categories":[{"name":"abstract_","score":0.00390625},{"name":"outdoor_","score":0.01171875},{"name":"outdoor_road","score":0.41796875}],"description":{"tags":["road","building","outdoor","scene","street","city","sitting","empty","light","view","driving","red","sign","intersection","green","large","riding","traffic","white","tall","blue","fire"],"captions":[{"text":"a view of a city street","confidence":0.83864323826716347}]},"requestId":"73fc14d5-653f-4a0a-a45a-e7a425580361","metadata":{"width":150,"height":153,"format":"Png"},"color":{"dominantColorForeground":"Grey","dominantColorBackground":"Grey","dominantColors":["Grey"],"accentColor":"274A68","isBWImg":false}'
ElementSeparate = json.loads(ElementSeparate)
for a in ElementSeparate['description']['tags']:
print a
To me it looks like you're trying to parse JSON. You should use the JSON parser for the second element of the array. You'll get back either list or dictionary. Then you'll be able to extract data from "description" key has.
https://docs.python.org/3/library/json.html
I've got an object which contains an element named "companies".
This element can either be a list of objects or just a single object (not contained within a list).
I would like to run through all companies, but this example fails if the element "companies" is just a single item (not contained within a list):
for company in companies:
I've tried to test before the for-loop, such as:
if type(companies['company']) is list:
# do your thing
but that fails as well.
Can anyone help?
Firstly, that's a really horrible way to structure data, and you should complain to whoever creates it. If an item can be a list, it should always be a list, even if that list just contains one element.
However, the code you have shown should work - although a better way to do it is if isinstance(companies['company'], list). If that's still not working, you will need to show the data, and the exact code that's using it.
You can make a list from a non-list for non-conditional use of "for ... in ...".
companies = list(companies)
for company in companies:
# use "company" in some way