python 列表,字符串,元组和集合

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 列表,字符串,元组和集合相关的知识,希望对你有一定的参考价值。

#################################
### LISTS ###
################################

# A list is a value that contains multiple values in an ordered sequence. A List value refers to the list itself, not the
# values inside the list. A list value begins and ends with brackets and looks like ['cat', 'bat', 'rat', 'dog'].
# Values inside the list are called items, and items are separated with commas. 

# Lists are a good alternate to having a number of variables that you have to manage, since if the number of values changes,
# your program will not be able to store more values than you have variables. Instead of using repetitive variables, it's 
# much easer to instead use a single variable that contains a list value.

################################
### RETREIVING LIST ITEMS WITH INDEXES AND SLICES ###
################################

# To get to individual items within a list, use an index. The first item starts with 0. Indexes can only be integers and 
# not floats. You can use a negative index to go backwards (negative indexes start at -1)

myList = ['cat', 'bat', 'rat', 'dog']

myList[0] # result is 'cat'
myList[1] # result is 'bat'
myList[-1] # result is 'dog'

'My favorite animal is the ' + myList[0] + '.' # result is 'My favorite animal is the cat.'

# Lists can also contain other list values. You can access these using multiple indexes, like so:

myList = [['cat', 'bat', 'rat', 'dog'], [10, 20, 40, 80]]

myList[0] # result is 'cat'
myList[0][1] # result is bat
myList[1][3] # result is 80

# You can get multiple values from a list using a slice. A slice will go up to BUT NOT INCLUDE the value at the second index.
# You can leave out the first or second index to start from the beginning or the end (as a shortcut)

myList = ['cat', 'bat', 'rat', 'dog']

myList[0:2] # result is ['cat', 'bat']
myList[:2] # result is ['cat', 'bat']
myList[2:] # result is ['rat', 'dog']
myList[0:-1] # result is ['cat', 'bat', 'rat']

################################
### DETERMINING A LIST LENGTH USING len() ###
################################

# you can determine the number of values in a list using len()

myList = ['cat', 'bat', 'rat', 'dog']
len(myList) # result is 4

################################
### CHANGING LIST VALUES ###
################################

# you can use an index to change a value at the position of that index

myList = ['cat', 'bat', 'rat', 'dog']

myList[2] = 'lemur' # change value at 2nd position (3rd value) in the list
myList # result is ['cat', 'bat', 'lemur', 'dog']
myList[3] = myList[2]
myList # result is ['cat', 'bat', 'lemur', 'lemur']

################################
### LIST CONCATENATION AND REPLICATION ###
################################

# you can concatenate lists 
[1, 2, 3] + ['A', 'B', 'C'] # result is [1, 2, 3, 'A', 'B', 'C']

[1, 2, 3] + myList # result is [1, 2, 3, 'cat', 'bat', 'lemur', 'lemur']

# you can also replicate a list
myList = ['cat', 'bat', 'rat', 'dog']
myList * 3 # result is ['cat', 'bat', 'rat', 'dog', 'cat', 'bat', 'rat', 'dog', 'cat', 'bat', 'rat', 'dog']

################################
### COMBINING LISTS ###
################################

# You can combine lists separated by a comma

list1 = ['cat', 'bat', 'rat', 'dog']
list2 = ['squirrel', 'penguin', 'llama']

combinedList = [list1, list2] # result is ['cat', 'bat', 'rat', 'dog', 'squirrel', 'penguin', 'llama']

################################
### DELETING VALUES FROM A LIST ###
################################

# you can delete using the del statement and an index
myList = ['cat', 'bat', 'rat', 'dog']
del myList[2] # delete value at index 2
myList # result is ['cat', 'bat', 'dog']

# del can also be used on a simple variable to delete it, as if it were an 'unassignment' statement
# however, you almost never need to delete simple vairiables. del is mainly used to delete values

################################
### USING LOOPS WITH LISTS ###
################################

# A loop repeats the code block once for each value in a list or list-like value. In the previous example
# using i in range(4): print (i), this is the same as writing for i in [0, 1, 2, 3]. i is set to a successive
# value in the list-like value of [0, 1, 2, 3].

# A common way to iterate over the indexes of a list is to use range(len(someList)):
myList = ['cat', 'bat', 'rat', 'dog']
for i in range(len(myList)): # len returns a lengthy of 4 for myList, which is then fed into the range() funciton so it'll loop four times
    print('Index ' + str(i) + ' in myList is: ' + myList[i])

# The result will be:
    # Index 0 in myList is: cat
    # Index 1 in myList is: bat
    # Index 2 in myList is: rat
    # Index 3 in myList is: dog

################################
### USING THE IN AND NOT IN OPERATORS WITH LISTS ###
################################

# You can determine whether a vlue is or isn't in a list with the in or not in operators. Like other operators, in and 
# not in are used in expressions and connect two values: a value to look for in a list and the list where it may be found.
# These expressions will evaluate to a Boolean value.
'cat' in ['cat', 'bat', 'rat', 'dog'] # result is True
'snake' in ['cat', 'bat', 'rat', 'dog'] # result is False

# Example of a program that lets the user type a pet name and then checks ot see if the name is in the list of pets.
myPets = ['cat', 'bat', 'rat', 'dog']
name = input('Enter a pet type: ') # create name variable and allow user input
if name not in myPets: # if the pet type isn't in the list myPets, print 'Not on the list'
    print('This ' + name + ' you speak of is not in the list!')
else: # else if the pet type is on the list, print the pet type and the affirmative message 
    print(name + ' is in the pet list! Hooray!')

# Example of using a loop with a for loop:

catNames = [] # create catNames variable with an empty list value
while True: # start infinite loop
    # the len() below queries the length of the catNames variable already, and prints that length plus 1, so you get 'Enter the name of cat 4' if you've already entered 3 cats
    print('Enter the name of cat ' + str(len(catNames) + 1) + ' (or enter nothing to stop.):') #
    name = input() # name is variable that allows the user to input a name
    if name == '': # if name entered is blank, break the loop
        break
    catNames = catNames + [name] # list concatenation
print('The can names are:') # one loop has been exited
for name in catNames: # start a new loop to go through the name values in catNames
    print (name) # print each name

# if you enter 'Susumu', 'Thumbs', 'Fluffy' into catNames, the for loop will print:
    # Susumu
    # Thumbs
    # Fluffy

################################
### MULTIPLE VARIABLE ASSIGNMENT SHORTCUT ###
################################

# Say you have a list and you want to place the values into a number of variables. You could do this:

cat = ['black', 'large', 'green']

color = cat[0]
size = cat[1]
eyes = cat[2]

# However you can also assign them all at once, as long as the number of variables equals the length of the list.
cat = ['black', 'large', 'green']

color, size, eyes = cat # the three variables will be assigned the three values from cat in order

################################
### METHODS FOR THE LIST DATA TYPE ###
################################

# A method is the same thing as a function, except it is 'called on' a value. For example, if a list were stored in myList,
# you would call the index() list method on that list like myList.index('cat'). The method comes after the value, separated by a period.

# Each data type has its own set of methods. The list data type has several useful methods for finding, adding, removing, or
# otherwise manipulating values within a list. These methods only apply to the list data type - they can't be applied
# to integers or strings.

# The index() method can be passed a value, and if the value exists in the list, the index position is returned. If the 
# value isn't in the list, then Python produces a ValueError error.
myList = ['cat', 'bat', 'rat', 'dog']

myList.index('bat') # index is the method, result is 1 since 'bat' is index 1 in myList

# Example of the same code but excepting for the ValueError so that you can return a custom error message
myList = ['cat', 'bat', 'rat', 'dog']

try: 
    myList.index('rabbit') 
except ValueError:
    print('Hey! That animal does not exist!')

# The append() and insert () methods allow you to add new values to a list. append() adds the value passed to the end of the list.
myList = ['cat', 'bat', 'rat', 'dog']

myList.append('rabbit')

myList # result is ['cat', 'bat', 'rat', 'dog', 'rabbit']

# insert() allows you to insert a value at any index of the list.
myList = ['cat', 'bat', 'rat', 'dog']

myList.insert(1, 'rabbit') # insert 'rabbit' into position 1 of the index

myList # result is ['cat', 'rabbit', 'bat', 'rat', 'dog']

# remove() allows you to remove a value. If the value doesn't exist in the list, you'll get a ValueError error. remove() will
# # only remove the first instance of the value found in the list, so it won't remove everything if a value shows up more
# # than once in the list. While # remove() is good if you know the value you want to remove, the del statement is good if 
# you want to delete something at # a specific position in the list (such as del myList[2]).
myList = ['cat', 'bat', 'rat', 'dog']

myList.remove('rat') # insert 'rabbit' into position 1 of the index

myList # result is ['cat', 'bat', 'dog']

# The sort() method allows you to sort the values. You can optionally use the reverse keyword argument of source = True to 
# sort backwards. The method sorts the actual list values in the variable, and you can't sort lists that have number values
# and string values. 
myList = [-0.7, 5, 2.3, 18, 1, -34]

myList.sort() # sorts in order

myList # result is [-34, -0.7, 1, 2.3, 5, 18]

myList.sort(reverse=True) # reverse keyword argument sorts in descending order

myList # result is [18, 5, 2.3, 1, -0.7, -34]

# Note that sort() sorts in ASCIIbetical order, meaning uppercase letters come before lowercase letters. If you need to sort
# in regular alphabetical order, use str.lower for the key argument in the sort() method call. This treats all items as
# if they were lower case.
myList = ['z', 'b', 'A', 'Z', 'a', 'C']
myList.sort() # normal ASCIIbetical sort
myList # result is ['A', 'C', 'Z', 'a', 'b', 'z']
myList.sort(key=str.lower) # alphabetical sort
myList # result is ['A', 'a', 'b', 'C', 'Z', 'z']

# you can also use sort keys like length. A sort key is a function that produces a value that can be used for the sorting
myList = ['cat', 'rabbit', 'a big snake', 'mouse']
myList # result is ['cat', 'rabbit', 'a big snake', 'mouse']
myList.sort(key=len) # sort by length
myList # result is ['cat', 'mouse', 'rabbit', 'a big snake']

################################
### LISTS AND INDENTATION ###
################################

# Indentation usually tells what block a line of code is in. However, with lists, you can actually span several lines and
# the indentation does not matter; Python knows that until it sees the ending square bracket, the list is not finished. This
# allows you to make a list more readable. An example:

import random

messages = ['It is certain',
      'It is definitely so',
      'Yes definitely',
      'For sure',
      'It may happen',
      'Probably not',
      'No way!']

print(messages[random.randint(0, len(messages) - 1)])

# You can also split up a single instruction across multiple lines using the \ continuation character.
print('This is on \
two lines!')

################################
### LIST-LIKE TYPES: STRINGS ###
################################

# Besides the list data type, there are other types that represent order sequences of values. For example, strings can be
# considered a 'list' of single text characters.
name = 'Steven'

name[0] # result is 'S'
name[1:4] # result is 'tev'
'Kr' in name # result is False
'St' in name # result is True

for i in name:
    print('****' + i + '****' )

# result is:
    # ****S****
    # ****t****
    # ****e****
    # ****v****
    # ****e****
    # ****n****

# Note that list are a mutable data type, meaning values can be added, removed or changed. strings are immutable, in that
# they cannot be changed. If you try you'll get a TypeError error. However, you can still 'mutate' a string by using slicing
# and concatenation to build a *new* string by copying parts from the old string.
name = 'Zophie the cat'

newName = name[0:7] + 'is a ' + name[11:]

newName # result is 'Zophie is a cat'

# There are escape characters that allow you to print quotes. Otherwise, you can use double quotes on the outside to print single quotes
# on the inside, and vice versa. These all work:
print('This is Alice\'s cat.')
print("This is Alice's cat.")
print('He then said "I am a cat", before running away.')

# You can break strings onto separate lines when printing using \n.
print('This is line one.\nThis is line two.')

# Placing an r before the beginning quotation mark of a string makes it a raw string. It completely ignores all espace characters and
# prints exactly what's in the string.
print(r'This is Alice\'s cat') # result is 'This is Alice\'s cat'

# Strings are indexed just like a list and can be indexed and sliced in the exact same way.
myString = 'This is my string'

myString[:4] # result is 'This'

# You can also search the contents of a string using the in and not in operators just like you can with a list
myString = 'This is my string'

'cat' in myString # result is False
'my' in myString # result is True

# There are also methods that can be applied to strings. upper() and lower() will return the string value as uppercase or lowercase (though
# it will not actually change the string - if you want to change the actual string you need to set the value isupper equal to the variable
# so it reassigns the variable.
myString = 'This is my string'

myString.upper() # result is 'THIS IS MY STRING', though the variable is not actually updated.
myString = myString.lower() # result is 'this is my string', since you're assigning the variable, this WILL change the string in the variable

# upper() and lower() are good if you need to make a case insensitive comparison.
myString = 'Great'

myString == 'great' # result is false since myString has a uppercase 'G' at the beginning
myString.lower() == 'great' # converts myString to 'great' with no uppercase 'G', therefore allowing you to compare them

# isupper() and islower() will tell you if there is at least one letter and all values are uppercase or lowercase
myString = 'Great'
myString.isupper() # result is False

myString = 'GREAT'
myString.isupper() # result is True

# Other string methods include (these are good for validating user input and making sure it's the correct format):
myString.isalpha() # returns True if the string consists only of letters and is not blank
myString.isalnum() # returns True if the string consists only of letters and numbers and is not blank
myString.isdecimal() # returns True if the string consists only of numeric characters and is not blank
myString.isspace() # returns True if the string consists only of spaces, tabs, and new lines and is not blank
myString.istitle() # returns True if the string consists only of of words that begin witn an uppercase followed only by lowercase

# startswith() and endswith() returns True or False if the string starts or ends with those characters
myString = 'This is a string that I type.'

myString.startswith('I type.') # result is False
myString.endswith('I type.') # result is True

# join() is useful when you have a list of strings that need to be joined together into a single string value. The method is called on a
# string, you pass it a list of strings, and it returns a single string
', '.join(['cats', 'bats', 'rats']) # result is 'cats, bats, rats'

'---'.join(['pizza', 'pretzels', 'donuts'])  # result is 'pizza---pretzels---donuts'

# split() does the opposite: it's called on a string value and returns a list of strings. By itself, it'll split wherever white space is found
myString = 'This is a string that I type.'
myString.split() # result is a list of ['This', 'is', 'a', 'string', 'that', 'I', 'type.']

# You can also specify where the split is.
myString = 'This is a string that I type.'
myString.split('string') # result is a list of ['This is a ', ' that I type.']

# This is often used when splitting a multiline string along newline characters.
myVariable = '''Dear Cat,
How have you been?
There is cat food in your dish.

Sincerely,
Bob'''

myVariable.split('\n') # result is ['Dear Cat,', 'How have you been?', 'There is cat food in your dish.', '', 'Sincerely,', 'Bob']

# You can justify and center using rjust(), ljust() and center(). You place in the length of the resulting string, and you can specify
# a character that fills in the spaces. 
myVariable = 'Hello world!'

myVariable.rjust(20) # result is '        Hello world!'
myVariable.ljust(20, '*') # result is 'Hello world!********'
myVariable.center(20, '=') # result is '====Hello world!===='

# This can be helpful when printing tablar data where you want the lines to line up
def printPicnic(itemsDict, leftWidth, rightWidth):
    print('PICNIC ITEMS'.center(leftWidth + rightWidth, '-'))
    for k, v in itemsDict.items():
        print(k.ljust(leftWidth, '.') + str(v).rjust(rightWidth))
picnicItems = {'sandwiches': 4, 'apples': 12, 'cups': 4, 'cookies': 8000}
printPicnic(picnicItems, 12, 5)
printPicnic(picnicItems, 20, 6)

''' Result is:
---PICNIC ITEMS--
sandwiches..    4
apples......   12
cups........    4
cookies..... 8000

-------PICNIC ITEMS-------
sandwiches..........     4
apples..............    12
cups................     4
cookies.............  8000
 '''

# You can remove whitespace (space, tab, and newline) with strip(), rstrip() and lstrip(). 
myVariable = '       Hello world       '

myVariable.rstrip() # result is '       Hello world'
myVariable.lstrip() # result is 'Hello world       '
myVariable.strip() # result is 'Hello world'

# You can optionally pass a strip() argument to tell it to strip a certain set of characters from the beginning and end of the string. 
# It will strip all of the # characters in the argument, and the order does not matter. Note that it won't strip anything on the inside
# of the string.
myVariable = 'CatDogSlothCat'

myVariable.strip('Cat') # result is 'DogSloth'
myVariable.strip('aCt') # result is 'DogSloth', since the order of characters doesn't matter
myVariable.strip('Sloth')

# You can create a string template, which allows you to enter various arguments and format them in different ways.
# In the example below, the first argument [0] is being formated as a float with two decimal places
# The second argument [1] is formated as a string
# The third argument [2] is formated as an exact integer
stringTemplate = '{0:.2f} {1:s} are worth US${2:d}'
stringTemplate.format(4.5560, 'Argentine Pesos', 1) # result is '4.56 Argentine Pesos are worth US$1'

# You can copy and paste using copy() and paste() from the pyperclip module
import pyperclip
pyperclip.copy('Hello World!')
pyperclip.paste()

################################
### LIST-LIKE TYPES: TUPLES ###
################################

# The tuple data type is almost identical to the list data type, except 1) tuples are typed with parentheses instead of square brackets,
# and that tuples, like strings, are immutable. Tuples cannot have their values modified, appended, or removed. Tuples can be used when you
# want to convey to anyone reading your code that you don't intend for that sequence of values to ever change. If you need an ordered 
# sequence of values that never changes, use a tuple. Secondly, since they don't change, Python can implement some optimizations that make
# code using tuples slightly faster than code using lists. Tuples are the default sequence type in Python, so if you assign a sequence of numbers
# to a variable, Python will automatically give it a type of tuple.

myTuple = ('hello', 42, 0.05)

myTuple[0] # result is 'hello'

# If you have only one value in your tuple, use a trailing comma, otherwise Python will think you've just typed a value inside regular parentheses.
type(('hello',)) # result is <class 'tuple'>
type(('hello')) # result is <class 'str'>

# You can convert lists and tuples using the list() and tuple() functions

myList = ['cat', 'bat', 'rat', 'dog']
myTuple = ('hello', 42, 0.05)

myList # result is ['cat', 'bat', 'rat', 'dog']
tuple(myList) # result is ('cat', 'bat', 'rat', 'dog')

myTuple # result is ('hello', 42, 0.05)
list(myTuple) # result is ['hello', 42, 0.05]

# tuples can have nested values
myTuple = 4, 5, (8, 21)
myTuple # result is (4, 5, (8, 21))

# tuples are immutable, however, if an object in a tuple is mutable like a list, you can change it
myTuple = tuple(['cat', [1, 2], True])
myTuple # result is ('cat', [1, 2], True)
myTuple[1].append(3) # index position 1 is the mutable list
myTuple # result is ('cat', [1, 2, 3], True)

# the count method can be used to count the number of instances of a value
myTuple = (1, 2, 2, 2, 3, 4 ,2)
myTuple.count(2) # result is 4

# Functions can usually only return a single value, but a they can return a tuple which itself can contain multiple
# values.

def square_info(x):
  A = x ** 2
  P = 4 * x
  print('Area and Perimeter:')
  return A,P # return separated by comma will return a tuple containing the value of A and P
  
square_info(3) # result is tuple (9, 12)

################################
### LIST-LIKE TYPES: SETS ###
################################

# a set is an unorder collection of unique elements. They are like dictionaries, but only have keys. They use curly brackets.
mySet = set([2, 2, 2, 1, 3, 3])
mySet # result is {1, 2, 3}

# you can compare different sets, like the two example sets below:
Set1 = {1, 2, 3, 4, 5}
Set2 = {3, 4, 5, 8, 9}

# union shows all unique elements in both sets
Set1.union(Set2) # result is {1, 2, 3, 4, 5, 8, 9}

# intersection shows elements occurring in both sets. You can use intersection or the & operator
Set1.intersection(Set2) # result is {3, 4, 5}
Set1 & Set2 # result is {3, 4, 5}

# difference shows what is in the first set but not the second
Set1.difference(Set2) # result is {1, 2}

# you can add and remove values from the set, or clear it
Set1.add(12)
Set1 # result is {1, 2, 3, 4, 5, 12}

Set1.remove(4)
Set1 # result is {1, 2, 3, 5, 12}

Set1.clear()
Set # result is {}

################################
### LIST COMPREHENSION AND SET COMPREHENSION ###
################################

# comprehension allows you concisely form a new list, set, or dictionary by filtering the elements of a collection
# A list comprehension has a format of [expr for value in collection if condition]
animals = ['bat', 'cat', 'really big snake', 'parrot']

myList = [item for item in animals]
myList # result is ['bat', 'cat', 'really big snake', 'parrot']
# example with an optional filter
myList = [item for item in animals if len(item) > 3] 
myList # resulting list is ['really big snake', 'parrot']

# a set comprehension has a format of {expr for value in collection if condition}
uniqueLengthsSet = {len(item) for item in animals}
uniqueLengthsSet # resulting set has value of {16, 3, 6}

################################
### REFERENCES ###
################################

# Python uses references whenever a variable must store values of mutable data types, such as lists or dictionaries. For values with immutable
# data types such as strings, integers, or tuples, Python will store the value itself into the variable.

# When you set one variable equal to another, it duplicates the value into the second variable, but the variables remain separate entities.

firstVariable = 42
secondVariable = firstVariable # set secondVariable using value from firstVariable
firstVariable = 100 # change value of firstVariable (doesn't affect secondVariable)

firstVariable # result is 100, since this was changed
secondVariable # result is 42, still 42 since this wasn't changed when firstVariable was changed

# However, lists don't function in this way. When you assign a list to a variable, the variable holds a *reference* to the list, 
# but doesn't actually copy the list value over. If you change the list, it'll change all of the variables that reference that list.

firstVariable = [0, 1, 2, 3, 4]
secondVariable = firstVariable # place firstVariable *reference* into secondVariable, but doesn't actually copy over values
firstVariable[3] = 100000 # change firstVarable, will also affect secondVariable since that variable references the same list

firstVariable # result is [0, 1, 2, 100000, 4]
secondVariable # result is [0, 1, 2, 100000, 4]

################################
### PASSING REFERENCES ###
################################

# References are important for understanding how arguments get passed to functions. When a function is called, the values of the arguments
# are copied to the parameter variables. For lists and dictionaryies, this means a copy of the references is used in the parameter.

def myFunction(someParameter):            # define function eggs with argument someParameter
    someParameter.append('hello')   # when function is called, append value of 'hello'

myList = [1, 2, 3]      # set myList to this list
myFunction(myList)      # call myFunction, passing in myList as the argument, at which point the function will append 'hello'
print(myList)             # result is [1, 2, 3, 'hello'], note that 'hello' has been appended to myList, since a reference (and not the actual values) was passed to the function

# While passing around references is often the handiest way to deal with lists and dictionaries, if the function modifies the list or
# dicionary that is passed, you may not want these changes in the original list or dictionary value. For this, Python provides a module
# called copy that provides both the copy() and deepcopy() functions. The first of these, copy.copy(), can be used to make a duplicate
# copy of a mutable value like a list or dictionary, and not just a copy of a reference.

# Remember that this happened when a reference was passed:

firstVariable = [0, 1, 2, 3, 4]
secondVariable = firstVariable # place firstVariable *reference* into secondVariable, but doesn't actually copy over values
firstVariable[3] = 100000 # change firstVarable, will also affect secondVariable since that variable references the same list

firstVariable # result is [0, 1, 2, 100000, 4]
secondVariable # result is [0, 1, 2, 100000, 4]

# However, if you use copy.copy():

import copy # import the copy library
firstVariable = [0, 1, 2, 3, 4]
secondVariable = copy.copy(firstVariable) # copy the list from firstVariable into secondVariable (i.e. don't pass the reference)
firstVariable[3] = 100000 # change firstVarable, will will *not* affect secondVariable since that variable used copy.copy()

firstVariable # result is [0, 1, 2, 100000, 4]
secondVariable # result is [0, 1, 2, 3, 4], since secondVariable was copied instead of a reference being passed, and therefore it wasn't changed when firstVariable was changed

################################
### ZIP ###
################################

# zip pairs up elements in a number of lists, tuples, or other sequences to create a list of tuples

sequence1 = ['cat', 'mouse', 'parrot']
sequence2 = ['one', 'two', 'three', 'four']
sequence3 = ['big', 'small', 'medium']

zipped = zip(sequence1, sequence2, sequence3)
list(zipped) # result is [('cat', 'one', 'big'), ('mouse', 'two', 'small'), ('parrot', 'three', 'medium')]

# you can also use zip to take two sequences and create a dictionary with the sequences as key-value pairs
sequence1 = ['cat', 'mouse', 'parrot']
sequence2 = ['one', 'two', 'three', 'four']

myDictionary = {}
myDictionary = dict(zip(sequence2, sequence1))
myDictionary # result is {'one': 'cat', 'two': 'mouse', 'three': 'parrot'}


以上是关于python 列表,字符串,元组和集合的主要内容,如果未能解决你的问题,请参考以下文章

python元组和集合

Python基础(3) - 去掉列表或元组中的重复元素

python第三节列表元组和字典

Python第四课 列表,元组和字符串的使用

Python第四课 列表,元组和字符串的使用

Python列表,元组,集合,字典的区别和相互