Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中用逗号合并列表中的两个元素?_Python - Fatal编程技术网

如何在python中用逗号合并列表中的两个元素?

如何在python中用逗号合并列表中的两个元素?,python,Python,让我们假设我有一个列表,如下所示: ['a', '256','c','d'] # 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

让我们假设我有一个列表,如下所示:

['a', '256','c','d']
# 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
我的输出应该是

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

我更希望它直接修改现有列表,而不是创建新列表。

试试这个

列表=['a'、'256'、'c'、'd']

列表[1]=“,”。加入(列表[1:3]中项目的项目)
希望这就是你想要的

如果你想合并第二和第三个元素,试试这个

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

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

基本上,我想提取泰坦尼克号数据集,而不使用熊猫,这是我们学院的一项任务。泰坦尼克号数据集的链接如下所示:

我已经编写了完整的解决方案,如下所示:

['a', '256','c','d']
# 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
isReal函数将检查给定字符串txt是否为浮点数。如果是,则返回true,否则返回false。 def isReal(txt): 尝试: 浮动(txt) 返回真值 除值错误外: 返回错误 #myload是一个函数,它将加载具有给定文件名的数据集,并去除所有不需要的字符,如“'”、“\r'、“\n”,并使用“,”作为分隔符进行拆分,这是在拆分函数的帮助下完成的,然后它将数据集中一行中的每个单词放入一个列表中,并将此列表追加到另一个列表中(基本上是创建列表列表)在此之前,我们在列表中插入“,”作为一个元素,然后使用join函数将乘客姓名合并为列表中的单个元素,最后我们将每个字符串类型的整数元素转换为整数,并以相同的方式浮动,并在完成任务的列表中替换它们。最后,我们弹出第一个列表从列表列表中。 def myload(文件名): L=[] 将open('train.csv')作为f: 对于f中的行: line=line.rstrip('\r\n') l=[line.split(',')中元素的元素.strip('“')] l、 插入(4,,) L.附加(L) 对于l中的l: l[3:6]=[''.join(l[3:6])] 对于l中的l: 对于索引,枚举中的元素(l): 如果l[index].isdigit()==True: l[index]=int(l[index]) 其他: 如果isReal(l[index])==True: l[索引]=浮动(l[索引]) L.pop(0) 返回L Li=[] nb=输入('输入要显示的行数:') nb=int(nb) i=0 Li=myload('train.csv') 而我sure:L=['a'、'b'、'c']对于索引,枚举中的项(L):L[0]=L[0]+L[1]+L[2]请打印您的问题并将代码放入其中,并确保使用格式化工具对其进行格式化。此外,请解释为什么您的代码没有按您认为的方式工作。您在寻找什么算法?您做了哪些尝试?事实上,这可以通过
mylist[:]=['a'、'256、c'、'd']来回答
。您的问题非常模糊……您期望的确切行为是什么?如果i是数字,或者如果i是数字,i+1是字符串,请将元素i与i+1合并?