小编Rei*_*ica的帖子

如何在Python中将多个项目从一个列表移动到另一个列表

我想将一个以上的项目从一个列表移动到另一个列表.

list1 = ['2D','  ','  ','  ','  ','  ','  ','  ','  ']
list2 = ['XX','XX','5D','4S','3D','  ','  ','  ','  ']
list3 = ['XX','XX','XX','8C','7H','6C','  ','  ','  ']
Run Code Online (Sandbox Code Playgroud)

在上面的代码中' '是一个双重空间

我想能够移动'5D','4S','3D'list2'8C','7H','6C'list3.

我已经尝试了下面的代码,但它不起作用.

list1 = ['2D','  ','  ','  ','  ','  ','  ','  ','  ']
list2 = ['XX','XX','5D','4S','3D','  ','  ','  ','  ']
list3 = ['XX','XX','XX','8C','7H','6C','  ','  ','  ']


items_to_be_moved = list2[list2.index('XX')+2 : list2.index('  ')]

list3[list3.index('  ')] = items_to_be_moved
del list2[list2.index('XX')+2 : list2.index('  ')]

print('list2',list2)
print('list3',list3) …
Run Code Online (Sandbox Code Playgroud)

python indexing list

1
推荐指数
1
解决办法
4074
查看次数

为什么在声明向量迭代器时使用 typename 关键字?

当我想为向量声明迭代器时,为什么需要使用类型名?

例如:

typename vector<T>::iterator i;
Run Code Online (Sandbox Code Playgroud)

如果我删除关键字,typename那么该程序根本无法运行。我写的内容是:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

template <class T>

class MyClass
{
  private:
    vector<T>  array;

  public:
    MyClass ( T * begin,int n ) : array(n)
    {
      copy( begin, begin + n, array.begin());
    }

    void List()
    {
      typename vector<T>::iterator i;
      for( i = array.begin(); i != array.end(); ++i )
        cout << * i << "," ;
    }   
};


int main()
{
  string array[4] = { "Tom","Jack","Mary","John"};
  MyClass<string>obj(array,4);
  obj.List();
  return 0; …
Run Code Online (Sandbox Code Playgroud)

c++ typename

0
推荐指数
1
解决办法
2260
查看次数

标签 统计

c++ ×1

indexing ×1

list ×1

python ×1

typename ×1