コード例 #1
0
def main():
    #if the argument to a tuple is a (string, list or tuple), the result is a tuple w/ elements of the sequence
    t = tuple('lupins')
    print t
    #('l', 'u', 'p', 'i', 'n', 's')

    a = "hi"
    b = 'pam'
    #tuple assignment is very elegant - SWAP EXAMPLE. ALL EXPRESSIONS ON RIGHT ARE EVALUATED BEFORE ASSIGNMENT
    a, b = b, a
    print a, b

    #USE SPLIT AND TUPLEs together for getting data. tuple assignment is GREAT for getting KEYS from list of data
    addr = '*****@*****.**'
    uname, domain = addr.split('@')

    print "Example of MULTIPLE ARGUMENTS for a function: " + str(
        sumall(1, 2, 3, 4))

    #ENUMERATE will give you the index of the for loop
    for index, element in enumerate('abc'):
        print "Get the Index of a LIST with ENUMERATE " + str(index), str(
            element)

    print structshape(t)
コード例 #2
0
ファイル: structshape_test.py プロジェクト: miodrags/Swampy
    def test_lumpy(self):
        t = [1, 2, 3]
        self.assertEqual(structshape(t), 'list of 3 int')

        t2 = [[1, 2], [3, 4], [5, 6]]
        self.assertEqual(structshape(t2), 'list of 3 list of 2 int')

        t3 = [1, 2, 3, 4.0, '5', '6', [7], [8], 9]
        self.assertEqual(structshape(t3),
                         'list of (3 int, float, 2 str, 2 list of int, int)')

        class Point:
            """trivial object type"""

        t4 = [Point(), Point()]
        self.assertEqual(structshape(t4), 'list of 2 Point')

        s = set('abc')
        self.assertEqual(structshape(s), 'set of 3 str')

        lt = list(zip(t, s))
        self.assertEqual(structshape(lt), 'list of 3 tuple of (int, str)')

        d = dict(lt)
        self.assertEqual(structshape(d), 'dict of 3 int->str')

        it = iter('abc')
        self.assertEqual(structshape(it), 'iterator of 3 str')
コード例 #3
0
    def test_lumpy(self):
        t = [1,2,3]
        self.assertEqual(structshape(t), 'list of 3 int')

        t2 = [[1,2], [3,4], [5,6]]
        self.assertEqual(structshape(t2), 'list of 3 list of 2 int')

        t3 = [1, 2, 3, 4.0, '5', '6', [7], [8], 9]
        self.assertEqual(structshape(t3), 'list of (3 int, float, 2 str, 2 list of int, int)')

        class Point:
            """trivial object type"""

        t4 = [Point(), Point()]
        self.assertEqual(structshape(t4), 'list of 2 Point')

        s = set('abc')
        self.assertEqual(structshape(s), 'set of 3 str')

        lt = zip(t, s)
        self.assertEqual(structshape(lt), 'list of 3 tuple of (int, str)')

        d = dict(lt)        
        self.assertEqual(structshape(d), 'dict of 3 int->str')

        it = iter('abc')
        self.assertEqual(structshape(it), 'iterator of 3 str')
コード例 #4
0
ファイル: exampleTuples.py プロジェクト: yuqiaoyan/Python
def main():
    #if the argument to a tuple is a (string, list or tuple), the result is a tuple w/ elements of the sequence
    t = tuple('lupins')
    print t
    #('l', 'u', 'p', 'i', 'n', 's')

    a="hi"
    b='pam'
    #tuple assignment is very elegant - SWAP EXAMPLE. ALL EXPRESSIONS ON RIGHT ARE EVALUATED BEFORE ASSIGNMENT
    a, b = b, a
    print a, b

    #USE SPLIT AND TUPLEs together for getting data. tuple assignment is GREAT for getting KEYS from list of data
    addr = '*****@*****.**'
    uname, domain = addr.split('@')

    print "Example of MULTIPLE ARGUMENTS for a function: " + str(sumall(1,2,3,4))

    #ENUMERATE will give you the index of the for loop
    for index, element in enumerate('abc'):
        print "Get the Index of a LIST with ENUMERATE " + str(index), str(element)

    print structshape(t)
コード例 #5
0
def most_frequent(listin):
    ''' Analyze histogram of words usage'''
    # initialize totalHis with empty dict
    totalHis = dict()
    for string in listin:
        # Produce 1 dict of str -> int every string in words list:
        onetextHist = hist_text(string)
        # extend totalHis with onetextHist:
        for char, count in onetextHist.items():
            totalHis[char] = totalHis.setdefault(char, 0) + count
    # sorting totalHis with decreasing freq order:
    charlist = list(totalHis)
    freq = list(totalHis.values())
    listHisTuple = list(zip(freq, charlist))
    listHisTuple.sort()
    print(structshape(listHisTuple))
    return totalHis
コード例 #6
0
from structshape import structshape
t = [1, 2, 3]
print(structshape(t))
t2 = [[1, 2, ['a', (1, 'b', {
    'a': 1,
    ('a', 'b'): [
        1,
        2,
    ]
})]], [3, 4, 'a'], (5, 6, 7)]

print(structshape(t2))
コード例 #7
0
ファイル: ch12.py プロジェクト: victortheleon/python_classes
from structshape import structshape

x = [(4,3),2]
print(structshape(x))
コード例 #8
0
from structshape import structshape
t = [1,2,3]
structshape(t)
list of 3 int

t2 = [[1,2]], [3,4], [5,6]]
structshape(t2)
'list of 3 list of 2 int'

>>> t3 = [1,2,3, 4.0, '5', '6', [7], [8], 9]
structshape(t3)
'list of (3 int, float, 2 str, 2 list of int, int)'

コード例 #9
0
t1 = 'a',
t2 = ('a')

print(type(t1))  # <class 'tuple'>
print(type(t2))  # <class 'str'>

# 列表和元组

s = 'abc'
t = [0, 1, 2]
zip(s, t)
for pair in zip(s, t):
    print(pair)

# 如果需要遍历一个序列的元素以及其索引号,您可以使用内建函数 enumerate :
for index, element in enumerate('abc'):
    print(index, element)

# 字典和元组

d = {'a': 0, 'b': 1, 'c': 2}
t = d.items()
#  print(t) ==>  dict_items([('c', 2), ('a', 0), ('b', 1)])
from structshape import structshape

print(structshape(d))
コード例 #10
0
ファイル: grid.py プロジェクト: Wag2323/ThinkPython
    a_len, meshsize: some positive number
    """
    axis_loc = [0]
    axis_div = int(a_len / meshsize)

    for i in range(0, axis_div):
        axis_loc.append(axis_loc[-1] + meshsize)

    # adds or modifies last point to allow it to meet up with the end length
    if axis_loc[-1] < a_len:
        axis_loc.append(a_len)

    if axis_loc[-1] > a_len:
        axis_loc[-1] = a_len

    return axis_loc
    
    
def distance(x1, y1, x2, y2):
    dx = x1 - x2
    dy = y1 - y2
    return math.sqrt(dx**2 + dy**2)
       

if __name__ == '__main__':

    p = create_points(10, 10.6, .25)
    for item in p:
        print item
    print structshape(p)
コード例 #11
0
d = {'a':0, 'b':1, 'c':2}
t = d.items()
print(t)
for key, value in d.items():
    print(key,value)

t = [('a', 0), ('c', 2), ('b', 1)]
d = dict(t)
print(d)
d = dict(zip('abc',range(3)))
print(d)
"""
directory[last, first] = number
for last,first in directory:
    print(last,first,directory[last,first])
    
"""
from structshape import  structshape
t = [1,2,3]
structshape(t)
t2 = [[1,2], [3,4], [5,6]]
structshape(t2)
t3 = [1,2,3,4.0,'5','6',[7],[8],9]
structshape(t3)
s = 'abc'
lt = list(zip(t,s))
structshape(lt)
d = dict(lt)
structshape(d)
コード例 #12
0
    res=[]
    for i in range(len(word)):
        child=word[:i]+word[i+1:]
        if child in worddict:
            res.append(child)
    return res
def all_reducible(worddict):
    res=[]
    for word in worddict:
        t=is_reducible(word,worddict)
        if t!=[]:
            res.append(word)
    return res
def findlongest(worddict):
    words=all_reducible(worddict)
    t=[]
    for word in words:
        t.append((len(word),word))
    t.sort(reverse=True)
    for length,word in t[0:5]:
        print(word)
worddict=word_dict_create('words.txt')
print(worddict)
print(structshape(worddict))
memo={}
memo['']=['']
print(is_reducible('sprite',worddict))
print(memo)
findlongest(worddict)

コード例 #13
0
    res = []
    for freq, x in t:
        res.append(x)

    return res


def make_histogram(s):
    """Make a map from letters to number of times they appear in s.

    s: string

    Returns: map from letter to frequency
    """
    hist = {}
    for x in s:
        hist[x] = hist.get(x, 0) + 1
    return hist


def read_file(filename):
    """Returns the contents of a file as a string."""
    return open(filename).read()


from structshape import structshape
if __name__ == '__main__':
    s = read_file('words.txt')
    print(structshape(s))
    t = most_frequent(s)
    print(t)
コード例 #14
0
from structshape import structshape

t = range(3)
s = 'allen'
t.append(s)

d = []
# d[1,2] = s
# d[2,3] = t

d=[d,1]

print d
print structshape(d)