Advertisement
earlution

1D and 2D list wrangling

Oct 9th, 2023
523
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.33 KB | None | 0 0
  1. """ 1. Read values from a 1D list, output to console: """
  2.  
  3. # Sample 1D list
  4. my_list = [1, 2, 3, 4, 5]
  5.  
  6. # Output values from the list to the console
  7. for value in my_list:
  8.     print(value)
  9.  
  10.  
  11. """ 2. Read values from the console, write to a 1D list: """
  12.  
  13. # Initialize an empty list
  14. my_list = []
  15.  
  16. # Prompt the user for input and add values to the list
  17. while True:
  18.     user_input = input("Enter a value (or 'exit' to quit): ")
  19.     if user_input.lower() == 'exit':
  20.         break
  21.     my_list.append(user_input)
  22.  
  23. # Display the contents of the list
  24. print("Contents of the list:")
  25. for value in my_list:
  26.     print(value)
  27.  
  28.  
  29. """ 3. Read values from a 1D list, write to a text file: """
  30.  
  31. # Sample 1D list
  32. my_list = [1, 2, 3, 4, 5]
  33.  
  34. # Specify the name of the output text file
  35. output_file_name = "output.txt"
  36.  
  37. # Open the text file for writing
  38. with open(output_file_name, "w") as file:
  39.     for value in my_list:
  40.         file.write(str(value) + "\n")
  41.  
  42.  
  43. """ 4. Read values from the console, write to a text file: """
  44.  
  45. # Specify the name of the output text file
  46. output_file_name = "output.txt"
  47.  
  48. # Open the text file for writing
  49. with open(output_file_name, "w") as file:
  50.     while True:
  51.         user_input = input("Enter a value (or 'exit' to quit): ")
  52.         if user_input.lower() == 'exit':
  53.             break
  54.         file.write(user_input + "\n")
  55.  
  56.  
  57. """ 5. Read values from a text file (each value on a separate line), write to a 1D list: """
  58.  
  59. # Specify the name of the input text file
  60. input_file_name = "input.txt"
  61.  
  62. # Initialize an empty list
  63. my_list = []
  64.  
  65. # Open the text file for reading
  66. with open(input_file_name, "r") as file:
  67.     for line in file:
  68.         # Remove leading/trailing whitespace and add to the list
  69.         my_list.append(line.strip())
  70.  
  71. # Display the contents of the list
  72. print("Contents of the list:")
  73. for value in my_list:
  74.     print(value)
  75.  
  76.  
  77. """ 6. Read a paragraph from a text file (terminated by a single EOL character), write to a 1D list:"""
  78.  
  79. # Specify the name of the input text file
  80. input_file_name = "input.txt"
  81.  
  82. # Initialize an empty list
  83. my_list = []
  84.  
  85. # Open the text file for reading
  86. with open(input_file_name, "r") as file:
  87.     paragraph = file.read()
  88.  
  89. # Split the paragraph into lines and add to the list
  90. my_list = paragraph.splitlines()
  91.  
  92. # Display the contents of the list
  93. print("Contents of the list:")
  94. for value in my_list:
  95.     print(value)
  96.  
  97.  
  98. """ 7. Read values from a 2D list, output to console: """
  99.  
  100. python
  101. Copy code
  102. # Sample 2D list
  103. two_d_list = [
  104.     [1, 2, 3],
  105.     [4, 5, 6],
  106.     [7, 8, 9]
  107. ]
  108.  
  109. # Output values from the 2D list to the console
  110. for row in two_d_list:
  111.     for value in row:
  112.         print(value, end=" ")  # Print values on the same line
  113.     print()  # Move to the next line after each row
  114.  
  115.  
  116. """ 8. Read CSV data from the console, write to a 2D list: """
  117.  
  118. import csv
  119.  
  120. # Initialize an empty 2D list
  121. two_d_list = []
  122.  
  123. # Prompt the user for CSV input
  124. print("Enter CSV data (e.g., 1,2,3):")
  125. while True:
  126.     user_input = input("Enter data (or 'exit' to quit): ")
  127.     if user_input.lower() == 'exit':
  128.         break
  129.     # Split the input into values and append to the 2D list
  130.     row = user_input.split(',')
  131.     two_d_list.append(row)
  132.  
  133. # Display the contents of the 2D list
  134. print("Contents of the 2D list:")
  135. for row in two_d_list:
  136.     print(row)
  137.  
Tags: python list
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement