Why does this list swap fail?

2016-11-14 Thread 380162267qq
L=[2,1]
L[0],L[L[0]-1]=L[L[0]-1],L[0]

The L doesn't change. Can someone provide me the detail procedure of this 
expression?
-- 
https://mail.python.org/mailman/listinfo/python-list


How make the judge with for loop?

2016-10-15 Thread 380162267qq
c="abcdefghijk" 
len=len(c)
n is a int
sb=[[] for i in range(n)]

while (i < len) {
for (int j = 0; j < n && i < len; j++)
sb[j].append(c[i++]);
for (int j = n-2; j >= 1 && i < len; j--) //
sb[j].append(c[i++]);
}

How to translate to python? I tried but my python code is really stupid
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to sort this without 'cmp=' in python 3?

2016-10-15 Thread 380162267qq
在 2016年10月14日星期五 UTC-4下午7:35:08,38016...@gmail.com写道:
> nums=['3','30','34','32','9','5']
> I need to sort the list in order to get the largest number string: '953433230'
> 
> nums.sort(cmp=lambda a,b: cmp(a+b, b+a), reverse=True)
> 
> But how to do this in python 3?
> 
> Thank you

!I learn more new tricks in Python. Thank you all of you guys.
You are all very kind helpful and knowledgeable.
-- 
https://mail.python.org/mailman/listinfo/python-list


How to sort this without 'cmp=' in python 3?

2016-10-14 Thread 380162267qq
nums=['3','30','34','32','9','5']
I need to sort the list in order to get the largest number string: '953433230'

nums.sort(cmp=lambda a,b: cmp(a+b, b+a), reverse=True)

But how to do this in python 3?

Thank you
-- 
https://mail.python.org/mailman/listinfo/python-list


I am comfused about the name behavior of Python in recursion

2016-10-05 Thread 380162267qq
Google told me Python name is a label attaching to the object.
But in this recursive function,the name 'a' will point to different number 
object.

def rec(a):
a+=1
if a<10:
rec(a)
print(a)

rec(0) gives me 101 normally.Why it works? Because of the stack memory 
management?

Thank you
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Need help for the print() function with a better order

2016-10-02 Thread 380162267qq
在 2016年10月2日星期日 UTC-4下午1:05:58,Peter Pearson写道:
> On Sat, 1 Oct 2016 18:12:29 -0700 (PDT), 38016226...@gmail.com wrote:
> > I am trying to print a simple decision tree for my homework.
> > The answer must keep in this format:
> >
> > Top 7,4,0.95
> > career gain = 100
> > 1.Management 2, 3, 0.9709505944546686
> > 2.Service 5, 1, 0.6500224216483541
> > location gain = 100
> > 1.Oregon 4, 1, 0.7219280948873623
> > 2.California 3, 3, 1.0
> > edu_level gain = 100
> > 1.High School 5, 1, 0.6500224216483541
> > 2.College 2, 3, 0.9709505944546686
> > years_exp gain = 100
> > 1.Less than 3 3, 1, 0.8112781244591328
> > 2.3 to 10 2, 1, 0.9182958340544896
> > 3.More than 10 2, 2, 1.0
> >
> > Here is my code:
> > features={'edu_level':['High School',
>  'College'],
> 'career':['Management',
>   'Service'],
> 'years_exp':['Less than 3',
>  '3 to 10',
>  'More than 10'],
> 'location':['Oregon',
> 'California']}
> >
> > print('Top 7,4,0.95')
> > for key in features:
> > print('{} gain = {}'.format(key,100))
> > attributes_list=features[key]
> > kargs={}
> > for i in range(len(attributes_list)):
> > kargs[key]=attributes_list[i]
> > low=table.count('Low',**kargs)
> > high=table.count('High',**kargs)
> > print('\t{}.{} {}, {}, {}'.format(
> i+1,attributes_list[i],low,high,entropy(low,high)))
> >
> > I set all the gain as 100 now.But actually the gain must calculate
> > with the data below.  For example, the career gain need the data of
> > 'Management' and 'Service'.  I don't know how to do.  or Anyone can
> > provide me a better logic?
> 
> I interpret your question as meaning that the value that you 
> print after "gain =" should depend on features[key].  To do that,
> you'll need to insert a line resembling
>   gain = gain_from_features(features[key])
> before the print statement.  You'll have to write the gain_from_features
> function, and provide it with the numbers from which it will
> compute the gain.
> 
> As a stylistic suggestion, note that Python allows you to break
> your "features=" line into a more readable format, as I have done
> above.
> 
> Another stylistic suggestions:
>   for key, attributes_list in features.iteritems():
> 
> -- 
> To email me, substitute nowhere->runbox, invalid->com.

Thank you for your suggestion.I was so tired last night printing the structure 
of decision tree crushed me.
I am not familiar with python and now I will try to modify my code.
-- 
https://mail.python.org/mailman/listinfo/python-list


Need help for the print() function with a better order

2016-10-01 Thread 380162267qq
I am trying to print a simple decision tree for my homework.
The answer must keep in this format:

Top 7,4,0.95
career gain = 100
1.Management 2, 3, 0.9709505944546686
2.Service 5, 1, 0.6500224216483541
location gain = 100
1.Oregon 4, 1, 0.7219280948873623
2.California 3, 3, 1.0
edu_level gain = 100
1.High School 5, 1, 0.6500224216483541
2.College 2, 3, 0.9709505944546686
years_exp gain = 100
1.Less than 3 3, 1, 0.8112781244591328
2.3 to 10 2, 1, 0.9182958340544896
3.More than 10 2, 2, 1.0

Here is my code:
features={'edu_level':['High School','College'],'career':
['Management','Service'],'years_exp':['Less than 3','3 to 10','More than 
10'],'location':['Oregon','California']}

print('Top 7,4,0.95')
for key in features:
print('{} gain = {}'.format(key,100))
attributes_list=features[key]
kargs={}
for i in range(len(attributes_list)):
kargs[key]=attributes_list[i]
low=table.count('Low',**kargs)
high=table.count('High',**kargs)
print('\t{}.{} {}, {}, 
{}'.format(i+1,attributes_list[i],low,high,entropy(low,high)))





I set all the gain as 100 now.But actually the gain must calculate with the 
data below.
For example, the career gain need the data of 'Management' and 'Service'.
I don't know how to do.
or Anyone can provide me a better logic?



-- 
https://mail.python.org/mailman/listinfo/python-list


Is 'learning python 5th edition' a good book to beginner?

2016-09-25 Thread 380162267qq
I want to find a python book like C++ primer which provides me details to 
understand the language.
-- 
https://mail.python.org/mailman/listinfo/python-list


Why does the insert after list function fail?

2016-09-22 Thread 380162267qq
A=["1","2","3"]
print(list(map(float,A)).insert(0,1))

I want to insert 1 at the head of the list but this gives me a surprise
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: I am newbie who can explain this code to me?

2016-09-22 Thread 380162267qq
在 2016年9月20日星期二 UTC-4下午3:11:27,Terry Reedy写道:
> On 9/20/2016 9:12 AM, Peter Otten wrote:
> 
> >> 在 2016年9月20日星期二 UTC-4上午8:17:13,BartC写道:
> >>> On 20/09/2016 13:12, 38016226...@gmail.com wrote:
> >>> d = {}
> >>> keys = range(256)
> >>> vals = map(chr, keys)
> >>> map(operator.setitem, [d]*len(keys), keys, vals)
> 
>  It is from python library. What does [d]*len(keys) mean?
>  d is the name of dict but put d in [] really confused me.
> 
> Where in which 'python library?  I cannot findI the above in 2.7 or 3.6 
> stdlib.  The code should be replaced by
> 
> > It should be noted that the code above is really bad Python.
> > Better alternatives are the simple loop
> >
> > d = {}
> > for i in range(256):
> >  d[i] = chr(i)
> >
> > or the dict comprehension
> >
> > d = {i: chr(i) for i in range(256)}
> 
> this.
> 
> -- 
> Terry Jan Reedy

I read an old 3.2 library and didn't notice at that time.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What does this zip() code mean?

2016-09-20 Thread 380162267qq
在 2016年9月20日星期二 UTC-4上午9:35:50,Random832写道:
> On Tue, Sep 20, 2016, at 09:19, 38016226...@gmail.com wrote:
> > >>> x = [1, 2, 3]
> > >>> y = [4, 5, 6]
> > >>> zipped = zip(x, y)
> > >>> list(zipped)
> > [(1, 4), (2, 5), (3, 6)]
> > >>> x2, y2 = zip(*zip(x, y))
> > >>> x == list(x2) and y == list(y2)
> > True
> > 
> > My problem is >>> x2, y2 = zip(*zip(x, y)).
> > zip return an iterator but x2 and y2 are different?
> > I really need detail explanation about this line.
> 
> Well, as you've seen, zip(x, y) is (1, 4), (2, 5), (3, 6)
> 
> This means that zip(*...) is zip((1, 4), (2, 5), (3, 6)).  It takes the
> first element of each argument (1, 2, and 3), and then the next element
> of each argument (4, 5, and 6).

thank you!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: I am newbie who can explain this code to me?

2016-09-20 Thread 380162267qq
在 2016年9月20日星期二 UTC-4上午9:13:35,Peter Otten写道:
> 38016226...@gmail.com wrote:
> 
> > 在 2016年9月20日星期二 UTC-4上午8:17:13,BartC写道:
> >> On 20/09/2016 13:12, 38016226...@gmail.com wrote:
> >>  d = {}
> >>  keys = range(256)
> >>  vals = map(chr, keys)
> >>  map(operator.setitem, [d]*len(keys), keys, vals)
> >> >
> >> > It is from python library. What does [d]*len(keys) mean?
> >> > d is the name of dict but put d in [] really confused me.
> >> >
> >> 
> >> if len(keys) is 5 then [d]*5 is:
> >> 
> >> [d,d,d,d,d]
> >> 
> >> [d] is a list, containing one item, a dict if that is what it is.
> >> 
> >> --
> >> Bartc
> > 
> > Thank you. I understand now
> 
> It should be noted that the code above is really bad Python.
> Better alternatives are the simple loop
> 
> d = {}
> for i in range(256):
>  d[i] = chr(i)
> 
> or the dict comprehension
> 
> d = {i: chr(i) for i in range(256)}
> 
> and even
> 
> keys = range(256)
> d = dict(zip(keys, map(chr, keys)))
> 
> because they don't build lists only to throw them away. 
> 
> 
> Also, creating a list of dicts or lists is a common gotcha because after
> 
> outer = [[]] * 3
> 
> the outer list contains *the* *same* list three times, not three empty 
> lists. Try
> 
> outer[0].append("surprise")
> print(outer)
> 
> in the interactive interpreter to see why the difference matters.
> 
> 
> Finally, if you are just starting you might consider picking Python 3 
> instead of Python 2.

Thank you.I learn more!
-- 
https://mail.python.org/mailman/listinfo/python-list


What does this zip() code mean?

2016-09-20 Thread 380162267qq
>>> x = [1, 2, 3]
>>> y = [4, 5, 6]
>>> zipped = zip(x, y)
>>> list(zipped)
[(1, 4), (2, 5), (3, 6)]
>>> x2, y2 = zip(*zip(x, y))
>>> x == list(x2) and y == list(y2)
True

My problem is >>> x2, y2 = zip(*zip(x, y)).
zip return an iterator but x2 and y2 are different?
I really need detail explanation about this line.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: I am newbie who can explain this code to me?

2016-09-20 Thread 380162267qq
在 2016年9月20日星期二 UTC-4上午8:17:13,BartC写道:
> On 20/09/2016 13:12, 38016226...@gmail.com wrote:
>  d = {}
>  keys = range(256)
>  vals = map(chr, keys)
>  map(operator.setitem, [d]*len(keys), keys, vals)
> >
> > It is from python library. What does [d]*len(keys) mean?
> > d is the name of dict but put d in [] really confused me.
> >
> 
> if len(keys) is 5 then [d]*5 is:
> 
> [d,d,d,d,d]
> 
> [d] is a list, containing one item, a dict if that is what it is.
> 
> -- 
> Bartc

Thank you. I understand now
-- 
https://mail.python.org/mailman/listinfo/python-list


I am newbie who can explain this code to me?

2016-09-20 Thread 380162267qq
>>> d = {}
>>> keys = range(256)
>>> vals = map(chr, keys)
>>> map(operator.setitem, [d]*len(keys), keys, vals)

It is from python library. What does [d]*len(keys) mean?
d is the name of dict but put d in [] really confused me.
-- 
https://mail.python.org/mailman/listinfo/python-list