附加列表Twi

2024-04-24 17:08:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道如何创建一个程序,可以根据用户的共同朋友推荐他们的朋友。特别是如果两个人是共同的朋友,我想把他们的用户ID放入一个列表中。例如,如果用户A是B和C的共同朋友,我希望A的列表如下所示:

A: [B, C]

如果B只是A的朋友,我希望B的列表如下:

B: [A]

如何附加列表以便将特定用户的朋友ID放入用户列表中?你知道吗

这是我目前掌握的密码。你知道吗

def open_file():
      '''Remember the docstring'''
      Done = False
      while not Done: 
        try:
           file_name = input("Enter a filename: ") 
           file_pointer = open(file_name)
           Done = True
           return file_pointer

       except FileNotFoundError:
           print("File not found. Please try a different filename: ")

 def read_file(file_pointer):  
      ''' Remember the docstring'''
     # Read n and initizlize the network to have n empty lists -- 
     #    one empty list for each member of the network
     n = file_pointer.readline()
     n = int(n)
     network = []
     user_chosen = int(input("Please pick a number within the correct range: "))
     for user_chosen in range(n):
        network.append([])
     else:
       print("Error: input must be an int within the correct range.")
     return network 

Tags: the用户id列表inputdef朋友range