小编Tib*_*bit的帖子

如何更改 Black 的换行设置(Python 格式化程序)?

我使用 black 作为我的 python 格式化程序,当我的代码中有一个很长的列表时,black 将格式化这个列表,因为需要很多行来放置这个列表,如下所示:

city_order_list = [
    1,
    22,
    8,
    26,
    31,
    3,
    36,
    35,
    20,
    2,
    29,
    21,
    ...
]
Run Code Online (Sandbox Code Playgroud)

是否有任何设置可以禁用此功能,或其他高级设置(例如在一行中指定固定数量的元素)?像这样:

city_order_list = [
    1, 22, 8, 26, 31,
    28, 3, 36, 35, 20,
    2, 29, 21, 16, 50,
    34, 30, 9, 49, 10,
    39, 33, 45, 15, 44,
    42, 40, 19, 41, 13,
    25, 14, 24, 43, 7,
]
Run Code Online (Sandbox Code Playgroud)

python formatter

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

列表和整数的Python类型错误?

我想阅读数字列表并按功能对它们进行排序,这是代码:

user_input = [int(i) for i in input().split(' ')]
for i in user_input:
    if i == user_input[0]: list2 = [[i]]

    for j in list2:
        if user_input[i] - j[-1] == 1:
            j.append(user_input[i])
        else:
            list2.append(user_input[i])
print(list2)

#############################################
>>> 8 7 1 9 2 6 3 5 4

Traceback (most recent call last):
  File "test.py", line 6, in <module>
    if user_input[i] - j[-1] == 1:
TypeError: 'int' object is not subscriptable
Run Code Online (Sandbox Code Playgroud)

我用谷歌搜索了这个问题,知道这个问题是在调用带有下标的 int 对象时发生的,但是当我运行时print(type(user_input), type(j)),结果都是list. 我想知道为什么会发生错误。有人可以解释我吗:)

python

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

标签 统计

python ×2

formatter ×1