在列表的第n个位置插入元素

时间:2019-12-03 22:12:43

标签: python list append

我正在使用Python开发项目,遇到一个问题。 我有一个长数据集,它是两个层列表的列表,看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <Rhino7DefaultInstallDir>$([MSBuild]::GetRegistryValueFromView('HKEY_LOCAL_MACHINE\SOFTWARE\McNeel\Rhinoceros\7.0\Install', 'Path', null, RegistryView.Registry64))</Rhino7DefaultInstallDir>
    <Rhino7DebugInstallDir>$([MSBuild]::GetRegistryValueFromView('HKEY_CURRENT_USER\Software\McNeel\Rhinoceros\7.0-WIP-Developer-Debug-trunk\Install', 'Path', null, RegistryView.Registry64))</Rhino7DebugInstallDir>
    <Rhino7InstallDir>$([MSBuild]::ValueOrDefault('$(Rhino7DebugInstallDir)', '$(Rhino7DefaultInstallDir)'))</Rhino7InstallDir>
    <Rhino7DefaultPluginsDir>$([MSBuild]::GetRegistryValueFromView('HKEY_LOCAL_MACHINE\SOFTWARE\McNeel\Rhinoceros\7.0\Install', 'Default Plug-ins Folder', null, RegistryView.Registry64))</Rhino7DefaultPluginsDir>
    <Rhino7DebugPluginsDir>$([MSBuild]::GetRegistryValueFromView('HKEY_CURRENT_USER\Software\McNeel\Rhinoceros\7.0-WIP-Developer-Debug-trunk\Install', 'Default Plug-ins Folder', null, RegistryView.Registry64))</Rhino7DebugPluginsDir>
    <Rhino7PluginsDir>$([MSBuild]::ValueOrDefault('$(Rhino7DebugPluginsDir)', '$(Rhino7DefaultPluginsDir)'))</Rhino7PluginsDir>
  </PropertyGroup>
  <Import Project="..\..\packages\Costura.Fody.3.3.3\build\Costura.Fody.props" Condition="Exists('..\..\packages\Costura.Fody.3.3.3\build\Costura.Fody.props')" />
  <PropertyGroup Condition="$(Configuration.Contains('2018'))">
    <TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
    <RevitVersion>2018</RevitVersion>
  </PropertyGroup>
  <PropertyGroup Condition="$(Configuration.Contains('2019'))">
    <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
    <RevitVersion>2019</RevitVersion>
  </PropertyGroup>
  <PropertyGroup Condition="$(Configuration.Contains('2020'))">
    <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
    <RevitVersion>2020</RevitVersion>
  </PropertyGroup>
  <PropertyGroup>
    <!-- Common ruleset shared by all projects -->
    <CodeAnalysisRuleset>$(SolutionDir)solution.ruleset</CodeAnalysisRuleset>
  </PropertyGroup>
  <ItemGroup>
    <AdditionalFiles Include="$(SolutionDir)stylecop.json" />
  </ItemGroup>
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProductVersion>9.0.30729</ProductVersion>
    <SchemaVersion>2.0</SchemaVersion>
    <ProjectGuid>{D6C256D4-B518-464F-9E68-CB282202E846}</ProjectGuid>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>Namespace</RootNamespace>
    <AssemblyName>AssemblyName</AssemblyName>
    <TargetFrameworkProfile />
    <NuGetPackageImportStamp>
    </NuGetPackageImportStamp>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug 2018|AnyCPU'">
    <DebugSymbols>true</DebugSymbols>
    <OutputPath>bin\Debug 2018\</OutputPath>
    <DefineConstants>TRACE;DEBUG;REVIT_2018</DefineConstants>
    <DebugType>full</DebugType>
    <PlatformTarget>AnyCPU</PlatformTarget>
    <ErrorReport>prompt</ErrorReport>
    <LangVersion>7.3</LangVersion>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release 2018|AnyCPU'">
    <DebugSymbols>true</DebugSymbols>
    <OutputPath>bin\Release 2018\</OutputPath>
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DefineConstants>REVIT_2018</DefineConstants>
    <Optimize>true</Optimize>
    <LangVersion>7.3</LangVersion>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug 2019|AnyCPU'">
    <DebugSymbols>true</DebugSymbols>
    <OutputPath>bin\Debug 2019\</OutputPath>
    <DefineConstants>TRACE;DEBUG;REVIT_2018; REVIT_2019</DefineConstants>
    <DebugType>portable</DebugType>
    <PlatformTarget>AnyCPU</PlatformTarget>
    <ErrorReport>prompt</ErrorReport>
    <LangVersion>7.3</LangVersion>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release 2019|AnyCPU'">
    <DebugSymbols>true</DebugSymbols>
    <OutputPath>bin\Release 2019\</OutputPath>
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DefineConstants>REVIT_2018; REVIT_2019</DefineConstants>
    <Optimize>true</Optimize>
    <LangVersion>7.3</LangVersion>
    <DebugType>portable</DebugType>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug 2020|AnyCPU'">
    <DebugSymbols>true</DebugSymbols>
    <OutputPath>bin\Debug 2020\</OutputPath>
    <DefineConstants>TRACE;DEBUG;REVIT_2018; REVIT_2019; REVIT_2020</DefineConstants>
    <DebugType>full</DebugType>
    <PlatformTarget>AnyCPU</PlatformTarget>
    <ErrorReport>prompt</ErrorReport>
    <LangVersion>7.3</LangVersion>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release 2020|AnyCPU'">
    <DebugSymbols>true</DebugSymbols>
    <OutputPath>bin\Release 2020\</OutputPath>
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DefineConstants>REVIT_2018; REVIT_2019; REVIT_2020</DefineConstants>
    <Optimize>true</Optimize>
    <LangVersion>7.3</LangVersion>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="Microsoft.VisualBasic" />
    <Reference Include="PresentationCore" />
    <Reference Include="PresentationFramework" />
    <Reference Include="System" />
    <Reference Include="System.Data" />
    <Reference Include="System.Drawing" />
    <Reference Include="System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\3.0\System.Management.Automation.dll</HintPath>
    </Reference>
    <Reference Include="System.Windows.Forms" />
    <Reference Include="System.Xml" />
    <Reference Include="WindowsBase" />
  </ItemGroup>
  <ItemGroup>
    ... Files ...
  </ItemGroup>
  <ItemGroup>
    <EmbeddedResource Include="Properties\Resources.resx">
      <SubType>Designer</SubType>
      <Generator>ResXFileCodeGenerator</Generator>
      <LastGenOutput>Resources.Designer.cs</LastGenOutput>
    </EmbeddedResource>
  </ItemGroup>
 ... More Files in ItemGroups ...
  <ItemGroup>
    ... Project References ...
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="AppCenter.Analytics.Metrics">
      <Version>1.1.0</Version>
    </PackageReference>
    <PackageReference Include="Microsoft.AppCenter.Analytics">
      <Version>2.6.2</Version>
    </PackageReference>
    <PackageReference Include="Microsoft.AppCenter.Crashes">
      <Version>2.6.2</Version>
    </PackageReference>
    <PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers">
      <Version>2.9.8</Version>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="StyleCop.Analyzers">
      <Version>1.1.118</Version>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
  </ItemGroup>
  <Choose>
    <When Condition="$(Configuration.Contains('Debug'))">
      <ItemGroup>
        <Reference Include="Eto">
          <HintPath>$(Rhino7InstallDir)Eto.dll</HintPath>
          <Private>False</Private>
        </Reference>
        <Reference Include="RhinoCommon">
          <HintPath>$(Rhino7InstallDir)RhinoCommon.dll</HintPath>
          <Private>False</Private>
        </Reference>
        <Reference Include="Rhino.UI">
          <HintPath>$(Rhino7InstallDir)Rhino.UI.dll</HintPath>
          <Private>False</Private>
        </Reference>
        <Reference Include="GH_IO">
          <HintPath>$(Rhino7PluginsDir)Grasshopper\GH_IO.dll</HintPath>
          <Private>False</Private>
        </Reference>
        <Reference Include="Grasshopper">
          <HintPath>$(Rhino7PluginsDir)Grasshopper\Grasshopper.dll</HintPath>
          <Private>False</Private>
        </Reference>
        <Reference Include="RevitAPI">
          <HintPath>C:\Program Files\Autodesk\Revit $(RevitVersion)\RevitAPI.dll</HintPath>
          <Private>False</Private>
        </Reference>
        <Reference Include="RevitAPIUI">
          <HintPath>C:\Program Files\Autodesk\Revit $(RevitVersion)\RevitAPIUI.dll</HintPath>
          <Private>False</Private>
        </Reference>
        <Reference Include="AdWindows">
          <HintPath>C:\Program Files\Autodesk\Revit $(RevitVersion)\AdWindows.dll</HintPath>
          <Private>False</Private>
        </Reference>
      </ItemGroup>
    </When>
    <Otherwise>
      <ItemGroup>
        <PackageReference Include="RhinoCommon" Version="7.0.19274.12465-wip" IncludeAssets="compile; build" />
        <PackageReference Include="Grasshopper" Version="7.0.19274.12465-wip" IncludeAssets="compile; build" />
        <PackageReference Include="Revit_All_Main_Versions_API_x64" Version="$(RevitVersion).0.*" IncludeAssets="build; compile" />
      </ItemGroup>
    </Otherwise>
  </Choose>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  <PropertyGroup>
    <ResolveAssemblyWarnOrErrorOnTargetArchitectureMismatch>None</ResolveAssemblyWarnOrErrorOnTargetArchitectureMismatch>
  </PropertyGroup>
</Project>

我想做的是插入另一个具有相同第一个元素但第二个不同的数据集,像这样:

[('A', 15), ('C', 125), ('L', 37), ('J', 215), ('M', 829), etc.]

这样,第一组数据的每个列表都包含三个元素,如下所示:

[('A', 2), ('C', 4), ('L', 9), ('J', 7), ('M', 15), etc.]

我需要设置哪种代码来获取第三组数据,以便将其第二个元素添加到我的第一组数据中?

1 个答案:

答案 0 :(得分:2)

如果按位置排列并且列表的长度相等,则可以尝试以下方法:

p

您基本上要做的是将两个列表组合成一个更大的元组,然后在其中按位置访问每个元素。