How to remove duplicate items in python list - python-2.7

How to remove duplicate items in a list. For example spider gives me below output:
[
{"category": "Movies", "id": 4},
{"category": "Movies", "id": 5}
{"category": "Movies", "id": 4}
]
I want to remove 1st and last item because they are same. please help.

x will be your list
uniquex = []
deleted = []
for i in x:
if i in uniquex:
uniquex.remove(i)
deleted.append(i)
elif i not in deleted:
uniquex.append(i)

Related

What's the best way to sort MAP<String, Object> by value in Dart programming?

I'm new to dart programming, I came from a Javascript environment
everything is fresh to me and I really have no Idea how this sorting works
in dart.
Here's an example from my API
List example = [
{"id": "1", "age": "20"},
{"id": "2", "age": "21"},
{"id": "3", "age": "22"},
]
Before I will assign "example" to other variables eg.
var example2 = example;
I want to sort it by "age", I found libs and other "LONG" solutions out there but feels like there's another way.. Thanks in advance!
There is sort function for list. Try reading the documentation a bit:
void main() {
List example = [
{"id": "1", "age": "20"},
{"id": "2", "age": "19"},
{"id": "3", "age": "22"},
{"id": "4", "age": "9"},
];
print(example);
example.sort((x,y) => x["age"].compareTo(y["age"]));
print(example);
}
EDIT: Your definition should contain integers logically:
void main() {
List example = [
{"id": 1, "age": 20},
{"id": 2, "age": 19},
{"id": 3, "age": 22},
{"id": 4, "age": 9},
];
print(example);
example.sort((x,y) => x["age"].compareTo(y["age"]));
print(example);
}

Data is not validated in Django Rest Framework

I am trying to pass the data to a serializer like the following:
myDict = {
"invoice_date": "2021-02-24T11:44:13+05:30",
"invoice_number": "12",
"vendor": "4",
"amount": "12",
"gst": "12",
"total_amount": "14",
"transaction_type": "Allot",
"status": "Hold",
"transactions": [
{
"t_no": 47,
"f_mile": "45",
"long_haul": "45",
"l_mile": "45",
"labour": "45",
"others": "54",
"a_t_no": 47,
},
{
"t_no": 102,
"f_mile": "12",
"long_haul": "12",
"l_mile": "21",
"labour": "21",
"others": "21",
"a_t_no": 102,
},
],
"owner": 2,
}
But when I check the validated data in the serialzer it shows it without the transactions data:
{'invoice_date': datetime.datetime(2021, 2, 24, 6, 14, 13, tzinfo=<UTC>), 'invoice_number': '12', 'amount': 12, 'gst': 12, 'total_amount': 14, 'status': 'Hold', 'transaction_type': 'Allot', 'vendor': <Vendor: Vendor object (4)>, 'owner': <User: yantraksh>}
so I tried to check the initial data that is being passed to the serializer :
<QueryDict: {
"invoice_date": ["2021-02-24T11:44:13+05:30"],
"invoice_number": ["12"],
"vendor": ["4"],
"amount": ["12"],
"gst": ["12"],
"total_amount": ["14"],
"transaction_type": ["Allot"],
"status": ["Hold"],
"transactions": [
'[{"t_no":47,"f_mile":"45","long_haul":"45","l_mile":"45","labour":"45","others":"54","a_t_no":47},{"t_no":102,"f_mile":"12","long_haul":"12","l_mile":"21","labour":"21","others": "21","a_t_no":102}]'
],
"owner": [2],
}>
It shows that the transaction data is being passed as a string, what should I change it to in order to get it as validated data ?
Based on the "evidence" in that QueryDict (namely strings wrapped in lists) it sounds like you're not submitting the data as JSON.
You can't use regular HTML form data to submit structured data, you'll need to post JSON data.

How to add a list in a list to another list

I have a list called test3 and I want to add this list to another list test0. The problem is that I only want the "noequipment" "sublist" to be added to the test0.
I tried it with the following code:
var test0 = test0 + test3["noequipment"];
This is list test3:
var test3 = [
{
"noequipment": [
{"name": "test0.1", "time": 10},
{"name": "test0.2", "time": 10},
{"name": "test0.3", "time": 10},
{"name": "test0.4", "time": 10},
]
},
{
"equipment1": [
{"name": "test1.1", "time": 10},
{"name": "test1.2", "time": 10},
{"name": "test1.3", "time": 10},
{"name": "test1.4", "time": 10},
]
},
];
Maybe you are looking for this answer
for (var i in test3[0]['noequipment']) {
test0.add(i);
print("===========>>${test0}");
}
Use code below assuming test0 is already an array
test0.addAll(test3[0]['noequipment'])
Please test this code.
test0.addAll(test3[0]["noequipment"]);
Make sure parent list is defined as var test0 = [] ;
Here I test this code https://pastebin.pl/view/ff02c9cc

Extracting value from dictionary in a list

I have this list with dictionaries:
[{"Name": "Tore", "Score": 7}, {"Name": "Arne", "Score": 10}, {"Name": "Terje", "Score": 4}]
and want to put the information in variables so i can get this:
first = "Tore
first_number = 7
second = "Arne
second-number = 10
How do I access these values in these variables?
Tried allot of different ways, but neither works.

django get elements with biggest qty of content_set

I'm trying to do a reverse key query. The query is this: the foo elements that has the biggest quantity of bar.
Foo:
name = models.CharField('name', max_length=255)
Bar:
name = models.CharField('name', max_length=255)
foo = models.ForeignKey(Foo)
foo_1.name = foo1
foo_2.name = foo2
foo_3.name = foo3
bar_1.name = bar1
bar_1.foo = foo1
bar_2.name = bar2
bar_2.foo = foo2
bar_3.name = bar3
bar_3.foo = foo2
bar_4.name = bar4
bar_4.foo = foo3
bar_4.name = bar4
bar_4.foo = foo3
'Foo2' has 2 'bars' and 'foo3' has 2 'bars' while 'foo1' has only one 'bar'
the result i want is a list with the foos with the biggest quantity of bars and the qty of bars like this:
[{'foo': foo2, 'qty_of_bars': 2}, {'foo': foo3, 'qty_of_bars': 3}]
I've tried a lot of things, for example:
foos = Foo.objects.all()
foos_with_biggest_qty_of_bar = foo.annotate(bar_count=Count('bar')).order_by(
'-bar_count').first()
This gives only one foo, the one with the biggest qty of bars, for example 'foo3', but there may be more than one. In the case above there are 2, foo2 and foo3.
I've tried this:
foos_with_biggest_qty_of_bar = foo.annotate(bar_count=Count('bar')).order_by(
'-bar_count').first()
This gives me the biggest qty of bars related to one single foo
biggest_qty_of_bars = bars.objects.filter(foo=foos_with_biggest_qty_of_bar ).count()
foos_with_biggest_qty_of_bars = Foo.objects.all().annotate(total_bars=biggest_qty_of_bars )
This last line didnt work, i got:
'int' object has no attribute 'lookup'
I tried other thing with a for loop(it's terrible, very ugly, sorry guys for this, by it was an attempt on despair, it's driving me crazy):
qyt_of_bars_of_each_foo = Foo.object.values('name').annotate(
count_bar=Count('bar'))
biggest_number = 0
foo_with_biggest_number_of_bar = []
for qty_of_bar_of_foo in qyt_of_bars_of_each_foo :
if qyt_of_bars_of_each_foo ['count_bar'] >= biggest_number :
foo_with_biggest_number_of_bar .append(qty_of_bar_of_foo)
biggest_number = qyt_of_bars_of_each_foo ['count_bar']
Nothing gave me the result I wanted, which is a list with the foos with the biggest quantity of bars and the qty, like I said before. I'm still trying but as I said it's driving me Crazy, i'm stuck on it since yesterday. Any help I would appreciate a lot!!
Have you tried doing an annotation over the entire queryset using Count and then returning the values as a list?
foo_objects = Foo.objects.all().annotate(
qty_of_bars=Count('bar')).order_by('-qty_of_bars').values(
'name', 'qty_of_bars')
This would return a list, e.g.:
[{'name': u'Foo1', 'qty_of_bars': 3}, {'name': u'Foo2', 'qty_of_bars': 2}]
given an app module name of 'foo' and the following fixture data:
[{
"fields": {
"name": "Foo1"
},
"model": "foo.foo",
"pk": 1
}, {
"fields": {
"name": "Foo2"
},
"model": "foo.foo",
"pk": 2
}, {
"fields": {
"foo": 1,
"name": "Bar1-1"
},
"model": "foo.bar",
"pk": 1
}, {
"fields": {
"foo": 1,
"name": "Bar2-1"
},
"model": "foo.bar",
"pk": 2
}, {
"fields": {
"foo": 1,
"name": "Bar3-1"
},
"model": "foo.bar",
"pk": 3
}, {
"fields": {
"foo": 2,
"name": "Bar1-2"
},
"model": "foo.bar",
"pk": 4
}, {
"fields": {
"foo": 2,
"name": "Bar2-2"
},
"model": "foo.bar",
"pk": 5
}]
update
If you're needing to get to values where qt_of_bars is greater than 1, you can easily get to those via a list comprehension, since you can't filter on an annotated field via the ORM:
foo_objects = [{'foo': f, 'qty_of_bars': f.qty_of_bars}
for f in Foo.objects.all().annotate(
qty_of_bars=Count('bar')).order_by(
'-qty_of_bars') if f.qty_of_bars > 1]
[{'foo': <Foo: Foo1>, 'qty_of_bars': 3}, {'foo': <Foo: Foo2>, 'qty_of_bars': 2}]