Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Wednesday, May 27, 2020

How to filter a list of dictionaries in python

# This is a list of dictionaries in python
list_of_dictionaries = [{'id':2, "name":"python"}, {'id':1, "name":"import"}, {'id':3, "name":"bye!"}]

# This is the list, filtered by the 'name' key
# returns a list that contains one element -- the dictionary with id == 1
filteredlist = list(filter(lambda d: d['name'] in ['import'], list_of_dictionaries))


# This is the list, filtered by the 'name' key
# returns a list that contains two dictionaries
filteredlist = list(filter(lambda d: d['name'] in ['import','python'], list_of_dictionaries))


# understanding the filter function
# https://docs.python.org/3/library/functions.html#filter

# understanding what lambda means
# https://docs.python.org/3/tutorial/controlflow.html#lambda-expressions

Tuesday, May 19, 2020

How to sort a list of dictionaries in python


# This is a list of dictionaries in python
list_of_dictionaries = [{'id':2, "name":"python"}, {'id':1, "name":"import"}]

# This is the list, sorted by the key "name"
sortedlist = sorted(list_of_dictionaries, key=lambda k: k['name'])

# This is the list, reverse sorted by the key "name"
sortedlist = sorted(list_of_dictionaries, key=lambda k: k['name'], reverse=True)

#shortcut to learn more about the sorted function
# https://docs.python.org/3/howto/sorting.html

#understanding what lambda means
# https://docs.python.org/3/tutorial/controlflow.html#lambda-expressions



Thursday, August 11, 2016

How to get VueJS, Rubaxa Sortable, and flask to work together

One of the problems I recently had integrating flask, sortable, and vuejs was that everytime I removed a object from the vuejs store, the sortable list would reset itself, sorting based on the data-id rather than the order of sortable. In the end, I added a function to Vue's watch and compiled events to sort sortable.
Vue.directive('sortable', {
    bind: function(){
        var vm = this.vm;
        var el = this.el;
        window.sortable = Sortable.create(el, {
            store: {
                get: function(sortable) {
                    const order = "{{ sort_order }}";
                    vm.sortOrder = order
                    return order ? order.split('|') : [];
                },

                set: function(sortable) {
                    const order = sortable.toArray();
                    vm.sortOrder = order.join('|')
                    $.ajax({
                        type: "POST",
                        url: '/sortorder',
                        data: {
                            'ordertype': 'todo_order',
                            'order': order.join('|')
                        }
                    })
                }
            },

            group: "todo_sort_order",
        });

    },
});

var vm =  new Vue({
    el:'#app',
    data:{
        todos: {{json_todo_list |safe}},
        sortOrder: [],
        orderedTodos:[]
    },
    methods: {

        deleteTodo: function(todo){
            _id = todo.id
            $("#" + _id)[0].reset();
            this.todos.$remove(todo);

            $.ajax({
                type: "POST",
                url: '/present/delete',
                data: {'id':_id},
                success: function(data) {
                }
            });
        },
    },
    watch: {
        'todos':function(val, oldVal){
            sortable.sort(this.sortOrder.split('|'));
        },
    },
    compiled: function(){
        sortable.sort(this.sortOrder.split('|'));
    }
})

Wednesday, December 30, 2015

How to flatten a list of lists

flatten
In [1]:
#An example of how to flatten a list of lists.

#create lista and listb 
lista = ['a','b','c']
listb = ['d','e','f']

#create a combined list that needs to be flattened
combined_lists = []
combined_lists.append(lista)
combined_lists.append(listb)

#note that the items in lista and listb are in separate lists within a list.
print combined_lists
[['a', 'b', 'c'], ['d', 'e', 'f']]
In [2]:
#flatten the list of lists
flattened_list = [item for sublist in combined_lists for item in sublist]
flattened_list
Out[2]:
['a', 'b', 'c', 'd', 'e', 'f']