【问题标题】:Array between integer and character in pythonpython中整数和字符之间的数组
【发布时间】:2021-10-17 21:40:31
【问题描述】:
 pat = "TATAG"
 DSIGMA = 4 //i used 4 character 'ACGT'
 m = len(pat)
 string = "ACGT"
 shift = [[0] * DSIGMA]*m
 for l in range(m):
   U[l] = 1
    for s in range(DSIGMA):
       shift[l][string[s]] = 1'''
Output

TypeError: 列表索引必须是整数或切片,而不是 str


I build this code in c++ and run perfectly, but when i convert into python it doesn't work. How to fix my code? Thanks!

【问题讨论】:

  • 你好 Luqman,欢迎来到 stackoverflow :)

标签: python


【解决方案1】:

对不起,这是我第一次使用堆栈溢出。

我使用 string[s] 作为索引,因为该字符串在我的下一个代码中会很有用。有我的c++程序。

 #include <iostream>
 #include <stdlib.h>
 #include <string.h>
 #define DSIGMA 4
 #define SIGMA 256
 #include <fstream>
 using namespace std;
 void MAS_Search(char *P,char *T){
    int shift[100][SIGMA],U[100],l,s,k,safe[100],i;
    int mas,avr_shift,scan[100],max_pos,mas_shift[100][100];
    int w;
    int count=0;
    char dna[DSIGMA]={ 'A','C','G','T' };
    int freq[100];
    freq['A'] = 293;    freq['C'] = 207;
    freq['G'] = 207;    freq['T'] = 293;
    int n=strlen(T);
    int m=strlen(P);
    /*Preproses*/
    for (l = 0; l<m; l++) {
        U[l] = 1;   // Deklarasi U = {0,1,...,m-1}.
        for (s = 0; s<DSIGMA; s++) {
        shift[l][dna[s]] = 1;
        }
    }
    for (k = 1; k <= m; k++) {
        safe[k] = 0;
    }
    /* Preproses */
    for (i = 0; i <m; i++) {
        for (l = 0; l <m; l++) {
            if (U[l] == 1) {
                for (s = 0; s <DSIGMA; s++) {
                    for (k = shift[l][dna[s]]; k <= m; k++) {
                        if (safe[k] == 0 &&  dna[s] == P[l - k]) {
                            shift[l][dna[s]] = k;
                            break;
                        }
                    }
                }
            }
        }
        //Max
        mas=0;
        for (l = 0; l <m; l++) {
            if (U[l] == 1) {
                avr_shift = 0;
                for (s = 0; s <DSIGMA; s++) {
                    avr_shift = avr_shift + shift[l][dna[s]] * freq[dna[s]];
                }
                if ((mas < avr_shift) || (mas == avr_shift && freq[P[max_pos]] > freq[P[l]])) {
                    mas = avr_shift;
                    max_pos = l;
                    cout<<max_pos;
                }
            }
        }
        scan[i]=max_pos;
        U[max_pos]=0;
        for (k=1;k<=max_pos-1;k++){
            if (P[max_pos]!=P[max_pos-k]){
            safe[k]=1;
            }
        }
    }
    
    //Found pattern and text
    w = 0;
    while (w<=n-m){
    i=0;
    while (i<=m && T[w+scan[i]] == P[scan[i]]){
        i=i+1;
    }
    if (i>m){
        cout<<"Pattern found at: "<<w+1<<endl;
        w=w+shift[scan[m]][T[w+scan[m]]];
    }
    else{
        w=w+shift[scan[i]][T[w+scan[i]]];
    }
    }
}
//Input
main(){
    ifstream teks;
    string Data;
    int i=0;
    char T[40000];
    teks.open("Data.txt");
    while (!teks.eof()){
        teks.get(T[i]);
        i++;
    }
    char P[] = "TATA";
    MAS_Search(P, T);
    return 0;
    teks.close();
    cin.get();
}

【讨论】:

    【解决方案2】:

    你现在正在写 python :) 所以不要评论像 ///**/ 并使用# or ''' ''' 发表评论。

    小心Spaces/Tabs :)

    你在哪里定义了U参数?你应该在某个地方定义它;你可以找到并阅读类似的错误here

    为什么你传递 string[s] 作为索引?如果您需要通过字符串/键访问,您可以使用 DICT 之类的东西。你可以阅读这个here

    删除 ``` 以及最后一行中的 output

    不知道应该是什么结果,但是这样改代码不会报错:

    pat = "TATAG"
    DSIGMA = 4 # i used 4 character 'ACGT' ****
    m = len(pat)
    string = "ACGT"
    shift = [[0] * DSIGMA]*m
    U=[0,0,0,0,0] # DEFINE SOMETHING ****
    for l in range(m):
        U[l] = 1
        for s in range(DSIGMA):
            # Change this to something else ****
            # string[s] is not ok as an index ****
            # shift[l][string[s]] = 1
            shift[l]=1
    

    【讨论】:

      猜你喜欢
      • 2020-05-28
      • 2013-03-27
      • 2017-12-03
      • 2021-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-21
      • 1970-01-01
      相关资源
      最近更新 更多