Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
data = [{'name':'Albert','rel':'Head','unique_number': 101},
{'name':'Sheen','rel':'Head','unique_number': 201},
{'name':'Peter','rel':'Son','unique_number': 101},
{'name':'Chloe','rel':'Daughter','unique_number': 101}]
can you help me out in getting data like this? filtered on unique_number
updated_data = [
{'house_head':'Albert','members':['Peter','Chloe']},
{'house_head':'Sheen','members':[]}
]
The following should work:
numbers=set([i['unique_number'] for i in data])
dict={i:{'Head':'', 'members':[]} for i in numbers}
for i in data:
if i['rel']=='Head':
dict[i['unique_number']]['Head']=i['name']
else:
dict[i['unique_number']]['members'].append(i['name'])
new_data=[{'house_head':dict[i]['Head'], 'members':dict[i]['members']} for i in dict]
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed yesterday.
Improve this question
i have soup response text with multiple group and sub groups.
i want to get automatic all groups and their values .
how can i do it ?
In the end, I want to get the title and the value for each group. The best thing for me is for each group to have its values separately.
OrderedDict([('#id',
'boic'),
('mc:id',
'boic'),
('mc:ocb-conditions',
OrderedDict([('mc:rule-deactivated',
'true'),
('mc:international',
'true')])),
('mc:cb-actions',
OrderedDict([('mc:allow',
'false')]))])
My goal is to get to a state where I get the following output:
'#id','boic'
'mc:id','boic'
'mc:ocb-conditions'
'mc:rule-deactivated','true'
'mc:international', 'true'
'mc:cb-actions'
'mc:allow','false'
i try to use
' '.join(BeautifulSoup(soup_response, "html.parser").findAll(text=True))
and got all values But I'm missing the titles of the values.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I used pandas to read a lot of datasets from bloomberg.
When I tested the reading program I noticed that pandas wasn't reading all rows, but it skipped some ones.
The code is the following:
def data_read(data_files):
data = {}
#Read all data and add it to a dictionary filename -> content
for file in data_files:
file_key=file.split('/')[-1][:-5]
data[file_key] = {}
#Foreach sheet add data sheet -> data
for sheet_key in data_to_take:
#path+"/"
data[file_key][sheet_key] = pnd.read_excel(file, sheet_name=sheet_key)
return data
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Hi I have big text file and I want to read the file in python and save the data on list.
the structure of the file is like this
[{"address":"office1","id":"3311"},{"address":"office2","id":"3322"}]
[{"address":"office3","id":"3312"},{"address":"office4","id":"3323"}]
I want to save the first line in one list and the second line in different list. Can you please explain how to do it.
file.txt
[{"address":"office1","id":"3311"},{"address":"office2","id":"3322"}]
[{"address":"office3","id":"3312"},{"address":"office4","id":"3323"}]
code:
import ast
lists = []
for line in open('file.txt'):
lists.append(ast.literal_eval(line.strip()))
>>> lists
[[{'id': '3311', 'address': 'office1'}, {'id': '3322', 'address': 'office2'}], [{'id': '3312', 'address': 'office3'}, {'id': '3323', 'address': 'office4'}]]
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a problem with django pagination. In my table I have 13,618 records but do pagination, I do not return results.
>>> from api.models import Post
>>> posts = Post.objects.all()
>>> posts.count()
13618
>>> posts = Post.objects.all()[10:10]
>>> posts.count()
0
The problem is in your slicing:
posts = Post.objects.all()[10:10]
You're asking for the 10th item to the 9th (10-1) item, which is an empty list. The same would happen if you did this:
ls = [1,2,3]
ls[1:1] # => []
It looks like you want 10 items starting from the 10th, in which case you should do:
posts = Post.objects.all()[10:20]
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Mutliple fields in the where clause of a QuerySet
qs_new = model_obj.objects.all()
You can use filter()to define a WHERE clause for your query:
qs_new = model_obj.objects.filter(...)
The QuerySet documentation describes various ways in which you can combine conditions and filters.
qs_new = model_obj.objects.filter(col_name='value', col_name2='value2')
filters_con = { 'a': 1, 'b': 2, 'c': 3 }
model_name.objects.filter(**filters_con)
it is working fine for me.