小编Joh*_*ica的帖子

排序后的字符串返回"无"

我正在尝试使用list.sort()方法.出于某种原因,None即使列表确实有值,结果也会返回.在使用该方法的其他用法中,它运行良好,我在该方法的使用中找不到任何差异.

我写的代码是一个简单练习的解决方案:

给定一个字符串列表,返回一个包含排序顺序的字符串的列表,除了将所有以'x'开头的字符串分组.

例如['mix','xyz','apple','xanadu','aardvark']产生['xanadu','xyz','aardvark','apple','mix']

提示:这可以通过制作2个列表并在组合之前对每个列表进行排序来完成.

我的代码:

def front_x(words):
    """ +++your code here+++ """
    x_list = []
    rest_list = []
    for word in words:
        if word[0] == 'x':
            x_list.append(word)
        else:
            rest_list.append(word)
    x_list = x_list[:].sort()
    rest_list = rest_list[:].sort()
    return [x_list, rest_list]
Run Code Online (Sandbox Code Playgroud)

x_listrest_list有正确的价值观,当for循环完成,并在分拣动作后,他们返回None.有任何想法吗?

python sorting

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

如何在java 8中将列表转换为具有设定值的地图

List<Hosting> list = new ArrayList<>();
list.add(new Hosting(1, "liquidweb.com"));
list.add(new Hosting(2, "aws.amazon.com"));
list.add(new Hosting(3, "digitalocean.com"));
list.add(new Hosting(2, "aws.amazon.com"));
Run Code Online (Sandbox Code Playgroud)

我想将上面的列表转换为Map<Integer, Set<String>>

1 -> {"liquidweb.com"}
2 -> {"aws.amazon.com"}
3 -> {"digitalocean.com"}
Run Code Online (Sandbox Code Playgroud)

如何在 Java 8 中进行转换?

java java-8 java-stream

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

克隆git存储库后不是git存储库

使用后克隆git存储库

git clone https://github.com/********/****.git 
Run Code Online (Sandbox Code Playgroud)

我只是尝试将某些内容推送到同一个git仓库中,但它显示:

致命的:不是git存储库(或任何父目录):.git

我怎样才能解决这个问题?

git

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

为什么C ++中的最小值显示出奇怪的结果?

在这个小程序中,我希望用户提示5个整数,并打印最小值和最大值。逻辑看起来还算不错,尽管如此,该min值仍打印出一个奇怪的负数。

我试图错误地更改变量的名称,以为'min'可能指的是C ++中允许的最小整数值,但结果没有改变。

#include <iostream>
using namespace std;
int main() {
    int numbers[5], min = 0, max = 0;
    cout << "Enter five numbers: ";        // Type a number and press enter

    for (int i = 0; i < 5; i++) {
        cin >> numbers[i];                 // Get user input from the keyboard

        for ( int i = 0; i < 5; i ++ ) {
            if (numbers[i] < min) {
                min = numbers[i];
            } else if (numbers[i] > …
Run Code Online (Sandbox Code Playgroud)

c++

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

与print和for循环相乘的区别

练习是:

列出前10个多维数据集(即从1到10的每个整数的多维数据集)的列表,然后使用for循环打印出每个多维数据集的值。

我这样写有什么区别?我这样写:

numbers = list (range(1,11))
for number in numbers :

    print(number**3) 
Run Code Online (Sandbox Code Playgroud)

一个解决方案是:

cubes = []
for number in range(1, 11):
    cube = number**3
    cubes.append(cube)

for cube in cubes:
    print(cube)
Run Code Online (Sandbox Code Playgroud)

python printing

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

Collections.unmodifiableCollection 返回值

List<Integer> integers = Arrays.asList(1, 2);
List<Integer> integers2 = Arrays.asList(1, 2);
Collection<Integer> integers3 = Collections.unmodifiableCollection(integers);
Collections.unmodifiableCollection(integers2);
integers3.add(3);
integers2.add(3); 
Run Code Online (Sandbox Code Playgroud)

我知道执行integers3.add()会抛出 UnsupportedOperationException,但 integers2 没有改变实例。我查看了 Collections.unmodifiableCollection(Collection ..) 的源代码,但是 integers2 的实现也从 AbstractList 抛出了 UnsupportedOperationException。为什么是这样?

java

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

为什么类槽用关键字指定但用符号访问?

我最近遇到了关于 Lisp 结构的令人困惑的二分法。

使用 来创建结构时(defstruct),我们通过关键字 ( :slotname)指定插槽。但是在访问它时,我们使用局部符号 ( 'slotname)。

为什么?这对我来说毫无意义。

另外,keyword每次声明结构时,这不会污染包吗?

如果我尝试通过关键字访问插槽,则会出现令人困惑的错误,例如:

尝试读取槽的值(槽值)时,对象 #S(ACCOUNT :BALANCE 1000 :CUSTOMER-NAME "John Doe") 中缺少槽 :BALANCE。

我不明白这个消息。它似乎在告诉我,我眼皮底下的东西并不存在。

我曾尝试使用本地符号声明结构;并且还有未绑定的关键字 ( #:balance) 并且这些都不起作用。

oop syntax common-lisp

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

作业出错了。这是gcc的bug吗?

//filename:mt.c\n//filename is useful to understand the gcc command\n#include <stdio.h>\nint isTmax(int x);\n\nint main()\n{\n    printf("wtf %d\\n", isTmax(0x7fffffff));\n    return 1;\n}\n\nint isTmax(int x)\n{\n    int y = ((x + x + 2) ^ 1);\n    int z = (!(~(x + x + 2) + 1) ^ 0);\n    printf("y = %d\\n", y);\n    printf("z = %d\\n", z);\n    return y & z;\n} \n
Run Code Online (Sandbox Code Playgroud)\n

代码很奇怪,因为它是一个 csapp 讲义解决方案(显然是错误的)。(x+x+2)当 x 等于 0x7fffffff 时,\n 等于 0。(~(x+x+2)+1)由于溢出所以等于0。所以(!(~(x+x+2)+1)^0)等于1。调试时观察验证了这一点。\n我认为,正常情况下,赋值后z应该为1。

\n

环境\xef\xbc\x9a{系统\xef\xbc\x9awindows 10; 虚拟系统ubuntu 20.04 LTS;虚拟机软件:VirtualBox 6.1;GCC:gcc(Ubuntu 9.3.0-17ubuntu1~20.04)9.3.0}

\n

\n …

c gcc integer-overflow

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

字符串 std::logic_error: basic_string::_S_construct null 无效

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string S = 0; 
    int T,R;
    
    cin >> S >> R;
    
    for(int i = 0; i < T; i++)
    {
        for(int k = 0; k < S.length(); k++)
        {
            for(int j = 0; j < R; j++)
            {
                cout << S[k];
            }
        }
        cout << endl;
    }
    
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

错误池语句为:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string S = 0; 
    int T,R;
    
    cin >> S …
Run Code Online (Sandbox Code Playgroud)

c++ stl

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

堆栈缓冲区溢出行为

在函数中的缓冲区之后声明变量是否会使缓冲区无法访问其内存区域?因为我尝试这样做,每次编译程序时缓冲区仍然可以访问它。缓冲区的首地址始终是堆栈帧的最低可能地址。

和编译器有关系吗?我正在使用海湾合作委员会。

int check_authentication(char *password){
    int demovar;
    char password_buffer[16];
    int auth_flag;

    strcpy(password_buffer,password);
    if(strcmp(password_buffer,"brilling")==0)auth_flag=1;
    if(strcmp(password_buffer,"outgrabe")==0)auth_flag=1;

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

c c++ buffer-overflow

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