Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.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 Can';t转换为';int';串_Python_String_Int_Typeerror_Bubble Sort - Fatal编程技术网

Python Can';t转换为';int';串

Python Can';t转换为';int';串,python,string,int,typeerror,bubble-sort,Python,String,Int,Typeerror,Bubble Sort,我试图对冒泡排序算法进行编码,但遇到了类型错误:无法将'int'对象隐式转换为str,我没有任何线索,因为我使用isinstance()检查了x和length,它们都是整数 以下是我目前的代码: x = 1 list1 = list(input("What numbers need sorting? Enter them as all one - ")) length = len(list1) print(list1) while True: for i in range(0,lengt

我试图对冒泡排序算法进行编码,但遇到了类型错误:无法将'int'对象隐式转换为str,我没有任何线索,因为我使用
isinstance()
检查了
x
length
,它们都是整数

以下是我目前的代码:

x = 1
list1 = list(input("What numbers need sorting? Enter them as all one - "))
length = len(list1)
print(list1)
while True:
    for i in range(0,length):
        try:
            if list1[i] > list1[i+1]:
                x = list1[i]
                list1.remove(x)
                list1.insert(i+1,x)
                print(list1)
            if list1[i] < list1[i+1]:
                x += 1
                print(list1)
        except IndexError:
            break
    if x == length:
        print("The sorted list is - ",''.join(list1))
        break
x=1
list1=列表(输入(“需要排序的数字是什么?全部输入为1-”)
长度=长度(列表1)
打印(列表1)
尽管如此:
对于范围内的i(0,长度):
尝试:
如果列表1[i]>列表1[i+1]:
x=列表1[i]
列表1.删除(x)
列表1.插入(i+1,x)
打印(列表1)
如果列表1[i]<列表1[i+1]:
x+=1
打印(列表1)
除索引器外:
打破
如果x==长度:
打印(“排序后的列表为-”,“”.join(列表1))
打破

列表1
由整数组成(可能取决于用户键入的内容,但代码的编写大部分都像需要一个整数列表一样),但您使用的是
”。在其上加入
,就像它包含字符串一样:

>>> ''.join([0])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: sequence item 0: expected string, int found
>>> ''.join(['0'])
'0'
>>> 
加入([0]) 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 TypeError:序列项0:应为字符串,找到int >>>''.join(['0']) '0' >>>
错误出现在
连接(列表1)
调用中
str.join
需要一个iterable字符串。但是,您的
列表1
是一个整数列表。所以结果它出了问题

您可以通过将列表的元素映射到
str
等效项来修复错误本身,方法是写入:

print("The sorted list is - ",''.join(map(str, list1))

请不要在迭代列表时更改列表。此外,您在此处使用
x
有两个目的:计数已排序的实例和交换元素。您正在尝试在此处分配
x
字符串值:
x=list1[i]
。列表
list1
是一个字符串列表,而不是整数。此代码在python 3上正确运行,您是否在python 2上使用此cod?哦,是的,我使用了x两次Willem。所以,如果我使用
int(input())
然后创建一个列表,它应该可以工作吗?我正在使用python 3是的,谢谢大家!因此,正如其他人所说,将
列表(input()
更改为
int(input()
),然后转换为列表。然后使用您的方法映射元素并交换元素。谢谢!
unsorted = True
while unsorted:
    unsorted = False  # declare the list sorted
                      # unless we see a bubble that proves otherwise
    for i in range(len(l)-1):  # avoid indexerrors
        if l[i] > l[i+1]:
            unsorted = True  # the list appears to be unsorted
            l[i+1], l[i] = l[i], l[i+1]  # swap elements

print(l)  # print the list using repr