小编ipl*_*uto的帖子

为什么x [i] [:] = x [:] [i]其中x是列表列表?

我正在处理列表列表和访问列非常混乱.

我们假设x定义如下:

  x = [[int(np.random.rand()*100) for i in xrange(5)] for x in xrange(10)]
  pprint.pprint(x)
Run Code Online (Sandbox Code Playgroud)
[[86, 92, 95, 78, 68],
 [76, 80, 44, 30, 73],
 [48, 85, 99, 35, 14],
 [3, 84, 50, 39, 47],
 [3, 7, 67, 28, 65],
 [19, 13, 98, 53, 33],
 [9, 97, 35, 25, 89],
 [48, 3, 48, 5, 1],
 [21, 40, 72, 61, 62],
 [58, 43, 84, 69, 26]]
Run Code Online (Sandbox Code Playgroud)

现在,双方x[1][:]x[:][1]产生相同的结果:

[76, 80, 44, 30, 73]
Run Code Online (Sandbox Code Playgroud)

有人可以解释原因吗?谢谢

python list

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

什么情况ostream :: write或ostream :: operator <<会失败?

在我的C++代码中,我经常将不同的值写入文件中.我的问题是,如果有任何写入或<<会失败的情况,考虑到文件已成功打开的事实.我是否需要检查每一次写入或<<确认它是否正确执行?

c++ file ofstream ostream

4
推荐指数
2
解决办法
1474
查看次数

运营商>>不喜欢vector <bool>?

我有以下代码,它基本上采用向量并将其写入文件,然后打开文件并将内容写入不同的向量.

#include <fstream>
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<bool> q, p;
//         ^^^^
    q.resize(5, 0);
    q[0] = 1;
    q[2] = 1;
    q[4] = 1;

    ofstream ofile("file.log");

    for (int i = 0; i<5; i++)
        ofile <<q[i]<<" ";

    ofile.close();

    ifstream ifile("file.log");

    p.resize(5);

    int i = 0;
//        vvvvvvvvvvvv
    while(ifile>> p[i])
    {
        cout <<i<<"\t"<<p[i]<<endl;
        i++;
    }

    ifile.close();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我注意到的是,当向量包含double,int和long数据类型时,此代码编译并运行没有问题,但如果将其更改为bool则会产生错误.这是我得到的错误消息:

../src/timeexample.cpp:31: error: no match for ‘operator>>’ in ‘ifile >> p.std::vector<bool, _Alloc>::operator[] [with _Alloc = std::allocator<bool>](((long unsigned int)i))’ …
Run Code Online (Sandbox Code Playgroud)

c++ boolean file vector ifstream

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

为什么 python 使用标识而不是大括号和关键字?

我一直无法理解的一件事是为什么 python 使用缩进,与其他脚本语言不同。依赖缩进使得编辑 Python 代码有时非常令人沮丧。

附:我有一种强烈的感觉,这个问题因为没有建设性而被关闭。我认为知道这个答案会很有趣。

python

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

标签 统计

c++ ×2

file ×2

python ×2

boolean ×1

ifstream ×1

list ×1

ofstream ×1

ostream ×1

vector ×1