Question

python 3.7

write a function called read_file() which accepts a filename and returns a list of tuple objects. For example, if we have thewrite a function called get_unique_ip_data_list() which accepts a list of tuple objects and returns a unique dictionary. This

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#trace05.txt:

1   0.000000000   192.168.0.24   10.0.0.5   98   84
1
2   1.001451000   192.168.0.24   10.0.0.5   98   84
1
3   2.002970000   192.168.0.24   10.0.0.5   98   84
1
4   3.003552000   192.168.0.24   10.0.0.5   98   84
1
5   4.005007000   192.168.0.24   10.0.0.5   98   84
1

#trace05.txt in image format:

0.000000000 192.168.0.24 1.001451000 192.168.0.24 98 con WN WN 10.0.0.5 10.0.0.5 10.0.0.5 10.0.0.5 2.002970000 192.168.0.24 3

#source code trace.py

def read_file(filename):
   f=open(filename,"r")
   i=1
   data=[]
   for line in f.readlines():
       if(i%2==1):
           sub=line.split("   ")
           sub_touple=(sub[2],round(float(sub[1])),int(sub[5].replace("\n","")))
           data.append(sub_touple)
       i+=1
   f.close()
   return data
def get_unique_ip_data_list(data):
   dict={}
   for i in range(len(data)):
           if data[i][0] in dict.keys():
               dict[data[i][0]].append((data[i][1],data[i][2]))
           else:
               dict[data[i][0]]=[(data[i][1],data[i][2])]
              
   return dict

ip_data_list=read_file("trace05.txt")
print(ip_data_list)
print("\n\n")
dict1=get_unique_ip_data_list(ip_data_list)
print(dict1)
       

#in image format:

14 def read_file(filename) : f=open (filename, r) i=1 data=[] for line in f.readlines(): if(i82==1): sub=line.split( ) su

#output:

C:\Users\vishnu\Desktop>python trace.py [(192.168.0.24, 0, 84), (192.168.0.24, 1, 84), (192.168.0.24, 2, 84), (192.168

#if you have any doubts comment below.. if you like give thumbs up....

Add a comment
Know the answer?
Add Answer to:
python 3.7 write a function called read_file() which accepts a filename and returns a list of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python Design a class named IP_address to represent IP address objects. The IP_addressclass contains the following...

    Python Design a class named IP_address to represent IP address objects. The IP_addressclass contains the following A number of instance variables/fields to store a table of data. You can design them on your own. A constructor that creates a table with the following: a list of data. IP address an integer to indicate the number of elements in the sum_list/freq_list/average_list A get_ip_address() method that returns the IP address For example, consider the following code fragment: ip_key = '192.168.0.24' data_list =[(0,...

  • Continue from the previous question, modify the following in the IP_address class. : add a method...

    Continue from the previous question, modify the following in the IP_address class. : add a method named get_statistics(). The get_statistics() method return a list of all statistics data (i.e. the ip address, total sum of all packet-size, total frequency, average of all packet-size) For example, consider the following code fragment ip_key = '192.168.0.24' data_list =[(0, 84), (1, 84), (2, 84), (3, 84), (4, 84), (5, 84), (6, 84), (7, 84), (8, 84), (9, 84), (10, 84), (11, 84), (12, 84),...

  • Python code that: Accepts a list of integers and an integer, and returns the index of...

    Python code that: Accepts a list of integers and an integer, and returns the index of the SECOND  occurrence of the integer in the list. It returns None if the number does not occur two or more times in the list. Example 1: second_index([2,34,3,45,34,45,3,3], 3) returns 6 Example 2: second_index([2,34,3,45,34,45,3,3], 45) returns 5 Example 3: second_index([2,34,3,45,134,45,3,3], 134) returns None Example 4: second_index([2,34,3,45,134,45,3,3], 100) returns None

  • 1. Write a function called ordinal_sum that accepts a string argument and returns the sum of...

    1. Write a function called ordinal_sum that accepts a string argument and returns the sum of the ordinal values of each character in the string. The ordinal value of a character is its numeric Unicode code point in decimal. 2. Write code that creates a Python set containing each unique character in a string named my_string. For example, if the string is 'hello', the set would be {'h', 'e', 'l', 'o'} (in any order). Assign the set to a variable...

  • Define a function called collapse() which takes a list as input. Each element of the list...

    Define a function called collapse() which takes a list as input. Each element of the list will either be an integer, or a list of integers. The function should modify the input list by replacing all of the elements which themselves are lists with the sum of their elements. For example: Test Result vals = [1, 2, 3, 4, 5] collapse(vals) print(vals) [1, 2, 3, 4, 5] vals = [1, [2, 3], 4, 5] collapse(vals) print(vals) [1, 5, 4, 5]...

  • write the following code in python 3.2+. Recursive function prefered. Thank you! Define a function named...

    write the following code in python 3.2+. Recursive function prefered. Thank you! Define a function named q3() that accepts a List of characters as a parameter and returns a Dictionary. The Dictionary values will be determined by counting the unique 3 letter combinations created by combining the values at index 0, 1, 2 in the List, then the values at index 1, 2, 3 and so on. The q3() function should continue analyzing each sequential 3 letter combination and using...

  •    PYTHON --create a function that accepts a list of numbers and an int that returns...

       PYTHON --create a function that accepts a list of numbers and an int that returns index of 2nd occurrence of the int in list, otherwise returns None if # does not repeat more 2 or more times EX: [10,24,3,45,10,49,4,5], 10) returns 4 --create a function that accepts a string that returns true if every letter of the alphabet can be found at least one time in the string, (has to be the lowercase alphabet), and false otherwise.    for...

  • 5. Write a Python function called readlinesmton that accepts the name of the file (i.e. filename),...

    5. Write a Python function called readlinesmton that accepts the name of the file (i.e. filename), starting line number i.e. m) and ending line number (i.e. n) as a parameter. The function then returns the contents from line number m to n (m <n). If m < 1 then start from line 1. Similarly, if n > number of lines the file, then stop at the last line in the file. Sample Input: readlinesmton("data.txt",5,7) Sample Input: readlinesmton("data.txt", 0,10)

  • in python A. Define a function called contains_only_integers() that takes a tuple, returns True if all...

    in python A. Define a function called contains_only_integers() that takes a tuple, returns True if all the items in the tuple are integers(2), and returns False otherwise. For example, contains_only_integers (3, 5, 17, 257, 65537) ), contains_only_integers( (-1,) ), and contains_only_integers( ) should all return True, but contains_only_integers (2.0,4.0)) and contains_only_integers (8, 4, "2", 1)) should both return false. Your function should use a while loop to do this calculation. 121 Hint: the is instance() built-in function provides the most...

  • Python 3.7.3 ''' Problem 3 Write a function called names, which takes a list of strings...

    Python 3.7.3 ''' Problem 3 Write a function called names, which takes a list of strings as a parameter. The strings are intended to represent a person's first and last name (with a blank in between). Assume the last names are unique. The function should return a dictionary (dict) whose keys are the people's last names, and whose values are their first names. For example: >>> dictionary = names([ 'Ljubomir Perkovic', \ 'Amber Settle', 'Steve Jost']) >>> dictionary['Settle'] 'Amber' >>>...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT