小编Ian*_*Ian的帖子

为什么这段代码在某些情况下会引发IndexError?

我的功能是这样的:

输入:

8
Run Code Online (Sandbox Code Playgroud)

输出:

['000', '001', '010', '011', '100', '101', '110', '111']
Run Code Online (Sandbox Code Playgroud)

所以这是创建一个列表来存储input_number从0开始的二进制数.这是我的代码(正确的版本):

import math

input_num = 8
max_bit = math.ceil(math.log(input_num, 2))

list_Bin = [None] * input_num

for i in range(input_num):
    extra_d = max_bit - len(bin(i)[2:])
    list_Bin[i] = '0'*extra_d + bin(i)[2:]

print(list_Bin)
Run Code Online (Sandbox Code Playgroud)

这段代码效果很好.但是,如果我改变一行代码:

list_Bin = [None] * input_num
Run Code Online (Sandbox Code Playgroud)

list_Bin = [] * input_num
Run Code Online (Sandbox Code Playgroud)

它会引发IndexError.

我真的很想知道为什么,因为我多次遇到过这个问题.

python indexing list python-3.x

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

为什么使用getch()会显示错误?

struct node
{
    int data;
    struct node *next;
}   *start=NULL;

void create()
{
    char ch;
    do
    {
        struct node *new_node,*current;

        new_node = (struct node *)malloc(sizeof(struct node));

        printf("\nEnter the data : ");
        scanf("%d",&new_node->data);
        new_node->next = NULL;

        if(start == NULL)
        {
            start = new_node;
            current = new_node;
        }
        else
        {
            current->next = new_node;
            current = new_node;
        }

        printf("nDo you want to creat another : ");
        ch = getch();
    } while(ch!='n');
}
Run Code Online (Sandbox Code Playgroud)

这是包含以下内容的代码部分 getch()

当我尝试在联机编译器中运行此代码时,出现以下错误:未定义对getch collect2的引用:错误:1d返回1退出状态

如何解决这个问题?...请帮助

c

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

将字符串拆分为单词数组

我想在string不使用的情况下将a 分成一个单词数组string.Split.我已经尝试过这段代码了,但它无法将结果分配给数组

string str = "Hello, how are you?";
string tmp = "";
int word_counter = 0;
for (int i = 0; i < str.Length; i++)
{
     if (str[i] == ' ')
     {
         word_counter++;
     }
}
string[] words = new string[word_counter+1];

for (int i = 0; i < str.Length; i++)
{
    if (str[i] != ' ')
    {
        tmp = tmp + str[i];
        continue;
    }
    // here is the problem, i cant assign every tmp in the …
Run Code Online (Sandbox Code Playgroud)

c# arrays string

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

自定义按钮:点击标签

我需要创建 custom Button- a里面Panel有 2 Labels 以获得漂亮的视觉格式。

但是我需要通过Labels单击面板以触​​发event,例如,MouseDown,或者我需要将其分配event给两个标签。

我尝试创建UserControl但问题是相同的 - 如果我将MouseDown事件设置为UserControl,其中的标签会阻止单击控件本身。

如果我设置为Label.Enable = false它可以解决该问题,但会生成另一个 - 文本Label变为gray和该属性我无法覆盖(我需要黑色文本,当events出现某些情况时需要红色文本)。

c# events event-handling winforms

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

我可以不间断地使用switch语句吗?

我的老师不允许我们使用诸如break,goto,continue ......之类的东西我决定在我的代码中添加一个switch语句而且我被卡住了,因为我能让它工作的唯一方法就是这样:

switch (exitValidation)
{
    case 'y':
    case 'Y': exit = false; break;
    case 'n':
    case 'N': Console.WriteLine("\nPlease Enter Valid values");
              exit = false; break;
    default:  exit = true; break;
}
Run Code Online (Sandbox Code Playgroud)

有没有办法在没有"休息"的情况下使用开关?另外,正在使用"休息"; 真的那么糟糕?

c# break switch-statement

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

如何在c#中以反向模式进行子串

我的字符串是"csm15 + abc-indiaurban @ v2".我只想从我的字符串中"indiaurban".我现在正在做的是:

var lastindexofplusminus = input.LastIndexOfAny(new char[]{'+','-'});

var lastindexofattherate = input.LastIndexOf('@');

string sub = input.Substring(lastindexofplusminus,lastindexofattherate);
Run Code Online (Sandbox Code Playgroud)

但得到错误"索引和长度必须引用字符串中的位置."

提前致谢.

c# string reverse substring

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

使用LinQ合并2个不同类型的列表

我有2个清单.如果它们都是类型的string.我可以使用以下内容合并它们:

List<string> myCars = new List<String> { "Yugo", "Aztec", "BMW" };
List<string> modeList = new List<String> { "BMW", "Saab", "Aztec" };
var carConcat = (from c in myCars select c)
.Concat(from c2 in modeList select c2);
Run Code Online (Sandbox Code Playgroud)

但是,如果我有一个,我怎么能(快速)获得相似的结果List<string>List<int>

List<string> myCars = new List<String> { "Yugo", "Aztec", "BMW" };
List<int> modeList = new List<int> { 1, 2, 3 };
Run Code Online (Sandbox Code Playgroud)

c# linq string int list

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

除特定URI以外的所有位置的Apache配置

我需要为“位置指令”应用一些配置。

这些配置必须应用于所有位置,除了某些位置URIs(例如,我不想更改的配置/products,因此,波纹管配置必须应用于除/ products之外的所有位置)

<Location />
 # desired configurations
</Location>
Run Code Online (Sandbox Code Playgroud)

regex location config directive apache2

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

有人可以解释这个bitCount代码是如何工作的吗?

这是我的TA帮助我获得的代码,但后来我完全忘记了它是如何工作的,因为我似乎无法得到正确的答案,而面试评分是明天.如果有人可以提供帮助请.谢谢

* bitCount - returns count of number of 1's in word
*   Examples: bitCount(5) = 2, bitCount(7) = 3
*   Legal ops: ! ~ & ^ | + << >>
*   Max ops: 40
*   Rating: 4
*/
int bitCount(int x) {
    int m4 = 0x1 | (0x1<<8) | (0x1<<16) | (0x1<<24);
    int m1 = 0xFF; 
    int s4 = (x&m4) + ((x>>1)&m4) + ((x>>2)&m4) + ((x>>3)&m4) + ((x>>4)&m4) + ((x>>5)&m4) + ((x>>6)&m4) + ((x>>7)&m4);
    int s1 = (s4&m1) + …
Run Code Online (Sandbox Code Playgroud)

c bit-shift bit bitwise-operators bitcount

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

单击 esc 按钮时,不应关闭甜蜜警报

当用户单击 esc 警报时不应关闭,他应该单击立即付款按钮以进一步操作。

 if($unpaid==true){
     echo $print = '<script>
      function alertOn(){
swal({ 
        title: "No Membership Fees Received",
        text: "Please pay membership fees as per your selected package",
        type: "warning",
        confirmButtonText: "PAY NOW"

      },
      function(){
        window.location.href = "unpaid.php";
    });
};
window.onload = alertOn
</script>';
Run Code Online (Sandbox Code Playgroud)

javascript php

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