这个列表重叠程序给出了奇怪的输出



我正在做一个练习,在这个练习中,我们制作了两个列表,然后我们必须创建一个程序,该程序将返回一个列表,该列表仅包含列表之间共有的元素。

我把代码写成——

print('Please type in your list number 1')
list_1=input() 
print('Great! Please type in your list number 2 ')
list_2=input()
commonlist=[] # this will be the list containing common elements.

for i in range(len(list_1)):
if list_1[i] in list_2:
commonlist.append((list_1[i])) # this will put the common elements between the two lists in commonlist
print(commonlist)

现在,如果我的list_1[1,2,3,4,5]并且我的list _2[3,4,5,6,7,8,9],那么预期的输出应该是

[3,4,5]

但输出是

[[["、"3"、"、">

4"、"、"5"、"]]

为什么我会收到此输出?

你得到这个奇怪的输出的原因是input()返回一个字符串。所以你正在迭代字符串而不是列表。

可以通过使用str.split()从输入创建值列表来解决此问题:

print('Please type in your list number 1')
list_1=input().split()
print('Great! Please type in your list number 2 ')
list_2=input().split()

请注意,您的输入必须用空格分隔。

另外作为旁注,如果顺序无关紧要,您可以在此处使用set()s:

>>> a = [1, 2, 3, 4]
>>> b = [2, 6, 1, 8]
>>> 
>>> set(a) & set(b)
{1, 2}
>>>

好的,我会尽量简单地解释它。

你的input()得到任何东西作为string。您需要根据自己的需要进行类型转换。

在你的 python 解释器中试试这个。

>>> a=input()
5
>>> a+10
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
a+10
TypeError: Can't convert 'int' object to str implicitly

为什么抛出错误。这称为强类型语言。这意味着python不允许这样做,您需要将其更改为所需的类型才能获得所需的内容。试试这个?

>>> a=input()
5
>>> int(a)+10
15
>>> 

现在它可以工作,因为我们添加了一个int()这是类型转换。现在在你的问题中,你只是把它们当作字符串并使用它们。

话虽如此,您必须使用list(input())将它们更改为列表 即便如此,字符串中那些不需要的空格也会出现。

>>> a
'1 2 3 4'
>>> list(a)
['1', ' ', '2', ' ', '3', ' ', '4']

在这种情况下,请使用 split()。默认情况下,your_string.split()返回由空格分隔的列表。您甚至可以指定要拆分的分隔符。所以这里不需要使用list()

>>> a
'1 2 3 4'
>>> a.split()
['1', '2', '3', '4']
print('Please type in your list number 1')
list_1=input().split() 
print(list_1)
print('Great! Please type in your list number 2 ')
list_2=input().split()
print(list_2)
commonlist=[] # this will be the list containing common elements.

for i in range(len(list_1)):
if list_1[i] in list_2:
commonlist.append((list_1[i])) # this will put the common elements between the two lists in commonlist
print(commonlist)

输出:

Please type in your list number 1
1 2 3 4
['1', '2', '3', '4']
Great! Please type in your list number 2 
2 3 5 6
['2', '3', '5', '6']
['2', '3']

看到你得到了你想要的东西,尽管我建议用这种方式在列表中找到共同元素。简单得多。

print('Please type in your list number 1')
list_1=input().split() 
print('Great! Please type in your list number 2 ')
list_2=input().split() 
commonlist = set(list_1)&set(list_2)

print(list(commonlist))

commonlist = set(list_1)&set(list_2)以一行结尾。以蟒蛇的方式做。简单的方法。

注意:这不会以有序的方式给出常见项目。但是你会得到所有常见的。

发生这种情况是因为input()返回一个str对象。因此,当您迭代字符串时,它被视为字符列表。

如果要以发布的格式输入列表,则需要将其作为字符串,然后对其进行解析以获取整数列表。str类的split()方法会有所帮助。如果需要,您需要指定分隔符。

input() 将输入的数据保存为字符串。 您需要将该输入转换为列表。 您需要对程序进行一些更改。它看起来像这样:

print('Please type in your list number 1(separated by ",") ')
list_1=input().split(",") 
print('Great! Please type in your list number 2(separated by",")')
list_2=input().split(",") 
commonlist=[] # this will be the list containing common elements.

for i in range(len(list_1)):
if list_1[i] in list_2:
commonlist.append((list_1[i])) # this will put the common elements between the two lists in commonlist
print(commonlist)

请注意,列表输入的两个数字之间应包含","。 例如: 23,45,67,54,67

最新更新