在python中过滤日期列表

时间:2018-11-15 11:40:38

标签: python python-3.x datetime timestamp filtering

我有以下列表列表:

[[Timestamp('2018-04-07 00:00:00'), Timestamp('2018-07-07 00:00:00'),Timestamp('2020-04-07 00:00:00')], [Timestamp('2018-04-07 00:00:00'),Timestamp('2018-10-07 00:00:00'),Timestamp('2018-12-07 00:00:00'),Timestamp('2021-04-07 00:00:00'),Timestamp('2022-04-07 00:00:00'),Timestamp('2023-04-07 00:00:00')]

我的问题是,是否可以按其他日期过滤列表列表。例如: 如果我选择此日期:

datetime.datetime(2019, 1, 26, 0, 0)

是否有一种方法可以过滤列表列表,使其仅获取比我选择的日期高,也比我选择的日期前的最后一个日期高的日期? 例如,对于列表的第一个列表,我要保留值:

[Timestamp('2018-07-07 00:00:00'),Timestamp('2020-04-07 00:00:00')]

对于列表列表的第二个列表:

[Timestamp('2018-12-07 00:00:00'),Timestamp('2021-04-07 00:00:00'),Timestamp('2022-04-07 00:00:00'),Timestamp('2023-04-07 00:00:00')]   

1 个答案:

答案 0 :(得分:1)

像这样吗?

using Prism.Commands;
using Prism.Navigation;
using Dialog.Interfaces;
using Prism.Services;

namespace Dialog.ViewModels
{
    public class RegisterPageViewModel : ViewModelBase
    {
        private INavigationService _navigationservice;
        private IRegistrationService _registartionService;
        private IPageDialogService _pageDialogService;
        private string _email;
        private string _password;
        private string _confirmpassword;

        public DelegateCommand NavigateToLoginCommand { get; private set; }
        public DelegateCommand RegisterCommand { get; private set; }


        public string Email
        {
            get
            {
                return _email;
            }
            set
            {
                SetProperty(ref _email, value);
            }
        }
        public string Password
        {
            get
            {
                return _password;
            }
            set
            {
                SetProperty(ref _password, value);
            }
        }
        public string ConfirmPassword
        {
            get
            {
                return _confirmpassword;
            }
            set
            {
                SetProperty(ref _confirmpassword, value);
            }
        }


        public RegisterPageViewModel(INavigationService navigationService, IRegistrationService registrationService, IPageDialogService pageDialogService) : base(navigationService)
        {
            _navigationservice = navigationService;
            _registartionService = registrationService;
            _pageDialogService = pageDialogService;

            NavigateToLoginCommand = new DelegateCommand(NavigateToLoginPage);
            RegisterCommand = new DelegateCommand(OnRegisterCommandExecuted, RegisterCommandCanExecute)
                .ObservesProperty(() => Email)
                .ObservesProperty(() => Password)
                .ObservesProperty(() => ConfirmPassword);
        }

        private bool RegisterCommandCanExecute()
        {
            return !string.IsNullOrWhiteSpace(Name) && !string.IsNullOrWhiteSpace(Email) && !string.IsNullOrWhiteSpace(Password) && !string.IsNullOrWhiteSpace(ConfirmPassword) && IsNotBusy;
        }

        private void OnRegisterCommandExecuted()
        {
            IsBusy = true;
            _registartionService.CreateUser("xxx", "xxxx", "xxx");
            IsBusy = false;
        }

        private void NavigateToLoginPage()
        {
            _navigationservice.GoBackAsync();
        }
    }
}

这产生

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Dialog.Views.RegisterPage" NavigationPage.HasNavigationBar="False">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="1*" />
            <RowDefinition Height="1*" />
            <RowDefinition Height="1*" />
        </Grid.RowDefinitions>
        <Grid.Resources>
            <ResourceDictionary>
                <Style TargetType="Entry">
                    <Setter Property="Margin" Value="40,10" />
                </Style>
            </ResourceDictionary>
        </Grid.Resources>
        <StackLayout Grid.Row="1" VerticalOptions="FillAndExpand">
            <Entry Text="{Binding Email}" Placeholder="Email Address" VerticalOptions="EndAndExpand" />
            <Entry Text="{Binding Password}" IsPassword="true" Placeholder="Password" VerticalOptions="StartAndExpand" />
            <Entry Text="{Binding ConfirmPassword}" IsPassword="true" Placeholder="Confirm Password" VerticalOptions="StartAndExpand" />
            <Button Text="Register" Command="{Binding RegisterCommand}" />
            <Button Text="Login" Command="{Binding NavigateToLoginCommand}" />
        </StackLayout>
    </Grid>
</ContentPage>