我想以数字分隔空格或分号的形式从用户那里获取输入。当用户输入类似于Lists: 1 3 4 2 1 2 1 3; 4 4 2 4 3 2 4 4 3 1 3
的列表时,我想要将分号前的数字放在一个列表中,并将分号后的数字放入不同的列表中。所以对于上面的输入,我的两个列表应该是[1, 3, 4, 2, 1, 2, 1, 3] and [4, 4, 2, 4, 3, 2, 4, 4, 3, 1, 3]
。我应该尝试拆分功能吗?我的目标是返回一个列表,其中包含按升序多次出现的所有元素。
while True:
original_string = input("Lists: ")
if not original_string:
exit()
first_split = original_string.split(';')
first_list, second_list = [elem.split(' ') for elem in first_split]
print(first_list)
print(second_list)
#how can returns the list of all the elements that occur multiple times in both lists and the return list should be in ascending order
我的输出应该是这样的
Lists: 1 3 4 2 1 2 1 3; 4 4 2 4 3 2 4 4 3 1 3
[2, 3]
Lists : 1 1 2 3 4 5; 2 3 4 5 6
[]
Lists : ;
[]
Lists:
您可以进行第一次拆分(由;
定义)。
然后作为第二个(由空格char定义),对于第一个分割中定义的每个元素。
first_split = original_string.split(';')
first_list, second_list = [elem.split(' ') for elem in first_split]
您可能希望对数据进行一些处理,例如:transform to integer并删除多余的空格或空元素。
请尝试以下代码。我已添加评论以便更好地理解。
original_string = input("Lists: ")
splitted_input = original_string.split(';') # split the input string into two lists of String
list1 = [int(i) for i in splitted_input[0].split()] # convert first String (before semi-colon) into list of int
list2 = [int(i) for i in splitted_input[1].split()] # convert second String (after semi-colon) into list of int
l1 = list(set([x for x in list1 if list1.count(x) > 1])) # List of only repeated numbers in list1
l2 = list(set([x for x in list2 if list2.count(x) > 1])) # List of only repeated numbers in list2
l = l1 + l2
final_list = list(set([x for x in l if l.count(x) > 1])) # List of only repeated numbers in l
final_list_sorted = sorted(final_list) # sort the list in ascending order
print('Two lists before and after semi-colin are: ', list1, list2)
print('Two lists with only repeated numbers are: ', l1, l2)
print('Final list with repeated numbers in both the lists in an ascending order is: ', final_list_sorted)
输出:
Lists: 1 3 4 2 1 2 1 3; 4 4 2 4 3 2 4 4 3 1 3
Two lists before and after semi-colin are: [1, 3, 4, 2, 1, 2, 1, 3] [4, 4, 2, 4, 3, 2, 4, 4, 3, 1, 3]
Two lists with only repeated numbers are: [1, 2, 3] [2, 3, 4]
Final list with repeated numbers in both the lists in an ascending order is: [2, 3]
Lists: 1 1 2 3 4 5; 2 3 4 5 6
Two lists before and after semi-colin are: [1, 1, 2, 3, 4, 5] [2, 3, 4, 5, 6]
Two lists with only repeated numbers are: [1] []
Final list with repeated numbers in both the lists in an ascending order is: []
Lists: ;
Two lists before and after semi-colin are: [] []
Two lists with only repeated numbers are: [] []
Final list with repeated numbers in both the lists in an ascending order is: []
您可以使用qazxsw poi,qazxsw poi和qazxsw poi等列表表达式来获得所需的输出,如下所示:
sorted()
输出:
split()
试试这个:
set()
要查找多次出现的元素,可以使用此方法
while True:
original_string = input("Lists: ")
if not original_string:
exit()
# get lists
l1, l2 = original_string.split(';')[0].split(), original_string.split(';')[1].split()
# print lists
print("l1: ", l1)
print("l2: ", l2)
# get occurences
occurences_in_l1 = sorted(set([e for e in l1 if l1.count(e) > 1]))
occurences_in_l2 = sorted(set([e for e in l1 if l2.count(e) > 1]))
# print occurences
print("sorted occurences l1: ", occurences_in_l1)
print("sorted occurences l2: ", occurences_in_l2)