简体   繁体   English

如何从单独列表中的列表列表中清空所有项目

[英]How to empty all items from a list of lists in a separate list

I'm new to Python and programming in general so maybe there is a simple way to do this without much hassle.我是 Python 和编程的新手,所以也许有一种简单的方法可以轻松做到这一点。 The code I've written so far is this (I'm giving you all the code for context):到目前为止我写的代码是这样的(我给你所有的上下文代码):

def prime(n):
    if n > 1:
        for i in range(2, n):
            if n % i == 0:
                return False
    return True


def length(n):
    if len(n) > 5:
        return True
    return False


def list_filter(lista):
    return [x for x in lista if x]


toggle = False
enter_numbers = True
zero_one_neg = True
prime_list = []
lcm_list = []
while enter_numbers:
    nums = list(map(int, input('\nEnter up to five natural numbers greater than 1 separated by commas. Enter at least two if you want to calculate their LCM: ').split(',')))
    for i in range(len(nums)):
        if nums[i] <= 1:
            zero_one_neg = True
            print('\nNo zeroes, ones or negatives. Start over. ')
            if length(nums) and zero_one_neg:
                print('\nNo more than five numbers. Start over. ')
                break
            break
        else:
            zero_one_neg = False
    if length(nums) and not zero_one_neg:
        print('\nNo more than five numbers. Start over. ')
    elif not length(nums) and not zero_one_neg:
        enter_numbers = False
for i in range(len(nums)):
    lcm_list.append([])
    prime_list.append([])
    for j in range(2, nums[i]):
        if nums[i] % j == 0:
            lcm_list[i].append(j)
            if prime(j):
                prime_list[i].append(j)
            toggle = True
    if not toggle:
        print('\n', nums[i], 'is a prime number.')
    if toggle:
        print('\n', nums[i], 'is not a prime number. It\'s prime factors are: ', prime_list[i])
        toggle = False
filtered_lcm_list = list_filter(lcm_list)
filtered_prime_list = list_filter(prime_list)
for i in range(len(filtered_lcm_list)):
    l = list(map(int, filtered_lcm_list[i]))
print(l)
print(filtered_lcm_list)

The problem is in this section:问题出在这一节:

for i in range(len(filtered_lcm_list)):
    full_list = list(map(int, filtered_lcm_list[i]))
print(full_list)
print(filtered_lcm_list)

If i give an input of say 88,77 i get this output:如果我输入 88,77,我会得到这个输出:

Enter up to five natural numbers greater than 1 separated by commas. Enter at least two if you want to calculate their LCM: 88,77

 88 is not a prime number. It's prime factors are:  [2, 11]

 77 is not a prime number. It's prime factors are:  [7, 11]
[7, 11]
[[2, 4, 8, 11, 22, 44], [7, 11]]

Process finished with exit code 0

I want full_list to contain all the elements of filtered_lcm_list but not as a list of lists.我想full_list包含的所有元素filtered_lcm_list但不是作为一个列表的列表。

How about this?这个怎么样?

full_list = [int(i) for sub_list in filtered_lcm_list for i in sub_list]

This makes use of a list comprehension, first iterating over the lists contained in filtered_lcm_list , then over the elements in each of those nested lists.这使得使用列表理解的,在列表第一迭代包含在filtered_lcm_list ,然后在每个那些嵌套列表的元素。

If you don't want duplicate numbers returned, you could use a set comprehension:如果您不想返回重复的数字,则可以使用集合理解:

full_list = {int(i) for sub_list in filtered_lcm_list for i in sub_list]}

and convert that back to a list, using list(full_list) if you wish.并将其转换回列表,如果您愿意,可以使用list(full_list)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM