小编ane*_*ayf的帖子

从头 Python 构建包含数据的表

我需要用这样的数据构建一个表:

     ???????????????????????????????
     ? ID     ? Name      ? Age    ?
     ???????????????????????????????
     ? 1      ? Jonh      ? 35     ?
     ???????????????????????????????
     ? 2      ? Joseph    ? 40     ?
     ???????????????????????????????
Run Code Online (Sandbox Code Playgroud)

我不允许为此使用任何 Python 库。它必须从头开始。我发现有一些框画 unicode 字符可用于绘制表格(https://en.wikipedia.org/wiki/Box-drawing_character)。前任:

print(u'\u250C') -> will print ?
Run Code Online (Sandbox Code Playgroud)

我不知道我应该如何解决这个问题。我应该打印数据然后绘制表格还是应该逐行打印完整的盒装。任何帮助表示赞赏。

我到目前为止的工作:

length_list = [len(element) for row in data for element in row]
column_width = max(length_list)
for row in data:
   print(u'\u250C' + (u'\u2500'*column_width*len(data[0])) + u'\u2510')
   row = "".join(element.ljust(column_width + 2) for element in row)
   print(row)
   print(u'\u2514' + …
Run Code Online (Sandbox Code Playgroud)

python

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

IOError: [Errno 25] 设备的 ioctl 不合适

我有以下代码来查找 Linux 中控制台的宽度,它适用于 Python 2.7 和 Python 3.X:

def get_default_console_width():
   try:
      from shutil import get_terminal_size
      console_width, rows = shutil.get_terminal_size()
   except Exception:
      import termios, fcntl, struct, sys
      s = struct.pack('hh', 0, 0)
      x = fcntl.ioctl(sys.stdout.fileno(), termios.TIOCGWINSZ, s)
      rows, console_width = struct.unpack("hh", x)
   return console_width
Run Code Online (Sandbox Code Playgroud)

在我的 test_something.py 文件中,我测试了一些调用get_default_console_width()的函数,它给了我这个错误:

IOError: [Errno 25] Inappropriate ioctl for device
Run Code Online (Sandbox Code Playgroud)

我知道有一些类似的帖子也有同样的问题,但我没有找到任何对这种情况有帮助的内容。

任何帮助表示赞赏!谢谢!

python python-2.7 python-3.x

6
推荐指数
1
解决办法
3万
查看次数

std::vector.push_back() C++

我想使用 push_back 函数用字母初始化一个向量。这是正确的方法吗?

vector<char> v;
char letter = 'A';
for(int i=0; i<26; i++)
{
    v.push_back(letter+i);
}
Run Code Online (Sandbox Code Playgroud)

有用。我只是想知道我是否应该在将 i 添加到 int 之前使用类型转换字母?

或者有没有更有效的方法?

c++ std

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

如何将多个字典添加到Python集合中

我有以下词典:

x = {"Name": "A"}
y = {"Name": "B"}
z = {"Name": "C"}

all_dictionaries = {}
all_dictionaries.update(x)
all_dictionaries.update(y)
all_dictionaries.update(z)
print(all_dictionaries)
Run Code Online (Sandbox Code Playgroud)

输出是

{'Name': 'C'}.
Run Code Online (Sandbox Code Playgroud)

但我希望它打印以下内容(包含所有声明的字典的一大组):

{{"Name": "A"}, {"Name": "B"}, {"Name": "C"}}
Run Code Online (Sandbox Code Playgroud)

不起作用update()。任何帮助表示赞赏!

python dictionary

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

标签 统计

python ×3

c++ ×1

dictionary ×1

python-2.7 ×1

python-3.x ×1

std ×1