2016-11-09 6 views
-1

私たちは下記のように、私はリストを持っていると仮定してみましょう:リストの2つの要素をPythonのコンマでマージするには?

['a', '256','c','d'] 

私の出力は

['a','256,c','d'] 

する必要があります私はずっとそれが新しいリストを作成していない、直接、既存のリストを変更することを好むだろう。

+0

確認:列挙(L)におけるL = [ 'A'、 'B'、 'C​​']インデックスの 、項目:

Iは以下の通りである完全なソリューションを書かれています\t L [0] = L [0] + L [1] + L [2] \t print L –

+0

質問を編集してそこにコードを入れ、フォーマットツールを使用してフォーマットしてください。また、あなたのコードがあなたが思うように働いていない理由を説明してください。 – idjaw

+0

あなたはどんなアルゴリズムを探していますか?どのような試みをしましたか?それは 'mylist [:] = ['a'、 '256、c'、 'd']'で答えることができます。 – TigerhawkT3

答えて

0

[1] = "" 内のアイテムの(項目への参加= [ 'A'、 '256'、 'C​​'、 'D']

リストこの

リストを試しますリスト[1:3]) 希望マージしたい場合、これはあなたが

0

欲しいものである第二と第三の要素は、この

lst = ['a', '256', 'c', 'd'] 

lst[1] += ',' + lst[2] 
del(lst[2]) 
0

は基本的に私は、タイタニック号のデータを抽出したいしようとはワットに設定しました私たちの大学で与えられた任務だったパンダを使用しないで。タイタニックデータセットへのリンクは以下の通りです: https://vincentarelbundock.github.io/Rdatasets/datasets.html

# isReal function will check whether the given string txt is a floating point number or not. and returns true if it is, and false otherwise. 
def isReal(txt): 
    try: 
     float(txt) 
     return True 
    except ValueError: 
     return False 
# myload is a function that will load the dataset with the given file_name and strips all the unwanted characters like '"','\r','\n',and splits with ',' being the delimiter and this is done with the help of split function then it puts each word in a line in the data set into a list and this list is appended into another list (basically creating list of lists) before this we insert ',' as an element in the list and then use the join function to merge the names of the passengers as a single element in the list and then finally we convert each of the integer element which are string type into integer and same way floats and replace them in the list which finishes the job. Finally we pop the first list from the list of lists. 
def myload(file_name): 
    L=[] 
    with open('train.csv') as f: 
     for line in f: 
      line=line.rstrip('\r\n') 
      l=[element.strip('"') for element in line.split(',')] 
      l.insert(4,',') 
      L.append(l) 

    for l in L: 
     l[3:6]=[''.join(l[3:6])] 
    for l in L: 
     for index, elem in enumerate(l): 
      if l[index].isdigit() == True: 
       l[index]=int(l[index]) 
      else: 
       if isReal(l[index]) == True: 
        l[index]=float(l[index]) 
    L.pop(0)     
    return L 
Li=[] 
nb=input('enter the number of lines u want to display:') 
nb=int(nb) 
i=0 
Li = myload('train.csv') 
while i < nb: 
    print(Li[i]) 
    i+=1 
関連する問題