2017-06-22 65 views
1

我在做一個練習,在其中我們做了兩個列表,然後我們必須創建一個程序,它將返回一個只包含列表之間通用元素的列表。此列表重疊程序給出奇怪的輸出

我寫的代碼 - 現在

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', ']']

爲何我獲得這個輸出?

回答

1

好吧我會盡量簡單地解釋它。

你的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 =集(LIST_1)&集(list_2)在一行結束。做它的蟒蛇方式。簡單的方法。

注意:這不會以有序方式提供常見項目。但你會得到所有常見的。

2

你得到那個怪異輸出的原因是因爲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()!而非:

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

此外,原始代碼適用於python2.7,無需更改 – VMRuiz

+0

@VMRuiz這是一個有效的點。但是我決定省略,因爲OP很清楚使用Python3。另外,在Python2中使用'input()'是邪惡的;-) –

+0

當然,這只是對你的答案的沉迷,而不是抱怨 – VMRuiz

1

這是因爲input()返回str對象。所以當你遍歷一個字符串時,它被當作一個字符列表。

如果你想以你發佈的格式輸入列表,你需要把它作爲一個字符串,然後解析它得到一個整數列表。 str類的split()方法將有所幫助。如有需要,您需要指定一個分隔符。

1

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